[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

A module problem

bashill.zhu@gmail.com

9/28/2008 2:17:00 PM

Administrator@HILL /d/Users/Administrator/Desktop/tcplex/ch8/ex3_test
$ cat lexer.h
namespace Lexer
{
char get_char();
void foo();
}
$ cat lexer.cpp
#include "lexer.h"
#include <iostream>
namespace Lexer
{
extern std::istream* input;
int g_i;
}
char Lexer::get_char()
{
char ch;
g_i = 100;
input->get(ch);
return ch;
}

void Lexer::foo()
{
g_i = 200;
char c;
input->get(c);
return;
}
$ cat main.cpp
#include "lexer.h"
#include <iostream>
using namespace std;
istream* input;
int main()
{
input = &cin;
char ch = Lexer::get_char();
}
g++ -g -Wall -c lexer.cpp
g++ -g -Wall -c main.cpp
main.cpp: In function `int main()':
main.cpp:8: warning: unused variable 'ch'
g++ -o test.exe lexer.o main.o
lexer.o: In function `ZN5Lexer8get_charEv':
d:/Users/Administrator/Desktop/tcplex/ch8/ex3_test/lexer.cpp:12:
undefined reference to `Lexer::input'
lexer.o: In function `ZN5Lexer3fooEv':
d:/Users/Administrator/Desktop/tcplex/ch8/ex3_test/lexer.cpp:20:
undefined reference to `Lexer::input'
collect2: ld returned 1 exit status
make: *** [test.exe] Error 1


Could some one give me some help about this issue?
4 Answers

Rolf Magnus

9/28/2008 3:53:00 PM

0

bashill.zhu@gmail.com wrote:

> namespace Lexer
> {
> extern std::istream* input;
> int g_i;
> }

> #include "lexer.h"
> #include <iostream>
> using namespace std;
> istream* input;
> int main()
> {
> input = &cin;
> char ch = Lexer::get_char();
> }

> undefined reference to `Lexer::input'
> undefined reference to `Lexer::input'

> Could some one give me some help about this issue?

Well, the linker is right. There is no definition of an object named input
in namespace Lexer. There is only one in the 'global' namespace.

Obnoxious User

9/28/2008 3:55:00 PM

0

On Sun, 28 Sep 2008 07:17:04 -0700, bashill.zhu@gmail.com wrote:

> Administrator@HILL /d/Users/Administrator/Desktop/tcplex/ch8/ex3_test $
> cat lexer.h
> namespace Lexer
> {
> char get_char();
> void foo();
> }
> $ cat lexer.cpp
> #include "lexer.h"
> #include <iostream>
> namespace Lexer
> {
> extern std::istream* input;
> int g_i;
> }
> char Lexer::get_char()
> {
> char ch;
> g_i = 100;
> input->get(ch);
> return ch;
> }
>
> void Lexer::foo()
> {
> g_i = 200;
> char c;
> input->get(c);
> return;
> }
> $ cat main.cpp
> #include "lexer.h"
> #include <iostream>
> using namespace std;

namespace Lexer {

> istream* input;

}

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

bashill.zhu@gmail.com

9/28/2008 10:56:00 PM

0

On 9?28?, ??11?53?, Rolf Magnus <ramag...@t-online.de> wrote:

>
> Well, the linker is right. There is no definition of an object named input
> in namespace Lexer. There is only one in the 'global' namespace.
>
Ok I got the point . Thanks a lot .
When we declare a varible , we need care its namespace limitation.

bashill.zhu@gmail.com

9/28/2008 11:07:00 PM

0

On 9?28?, ??11?54?, Obnoxious User <O...@127.0.0.1> wrote:
> On Sun, 28 Sep 2008 07:17:04 -0700, bashill....@gmail.com wrote:
> > Administrator@HILL /d/Users/Administrator/Desktop/tcplex/ch8/ex3_test $
> > cat lexer.h
> > namespace Lexer
> > {
> > char get_char();
> > void foo();
> > }
> > $ cat lexer.cpp
> > #include "lexer.h"
> > #include <iostream>
> > namespace Lexer
> > {
> > extern std::istream* input;
> > int g_i;
> > }
> > char Lexer::get_char()
> > {
> > char ch;
> > g_i = 100;
> > input->get(ch);
> > return ch;
> > }
>
> > void Lexer::foo()
> > {
> > g_i = 200;
> > char c;
> > input->get(c);
> > return;
> > }
> > $ cat main.cpp
> > #include "lexer.h"
> > #include <iostream>
> > using namespace std;
>
> namespace Lexer {
>
> > istream* input;
> }
>
> --
> OU
> Remember 18th of June 2008, Democracy died that afternoon.http://frapedia..se/wiki/Information_in_English- ??????? -
>
> - ??????? -

I got the point.
When do this i should initialize the input pointer oursize the module
to let it points to a object .
Thanks in advance.