A module problem

B

bashill.zhu

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?
 
R

Rolf Magnus

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.
 
B

bashill.zhu

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top