By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Problem using static library

P: 5
Hi, I'm having trouble executing the following line of code:
Tokeniser tokeniser(cin);

The compiler complains thats i have a undefined reference to Tokeniser::Tokeniser(std::basic_istream(char, std::char_traits(char))&)

The line Test tester(cin) works as since it was written by myself.
However, whenever i try executing Tokeniser tokeniser(cin) i get the compilation error.

I am given Tokeniser.h with various other header files and a libparser.a as part of the assignment

My makefile includes:
//START OF MAKE FILE

HEADER_PATH = ass3/parser/
LIB_PATH = ass3/parser/
LIB_NAME = parser

search.exe: main.o test.o
g++ -g -Wall -o search.exe main.o test.o -L$(LIB_PATH) -l$(LIB_NAME) -static
rm main.o
rm test.o

main.o: main.cc
g++ -c -O -Wall -I$(HEADER_PATH) main.cc

test.o: test.cc
g++ -c -O -Wall test.cc
// END OF MAKEFILE

//main.cc start
#include <iostream>
#include "Tokeniser.h"
#include "Test.h"

using namespace std;

int main(){
//Test tester(cin); //this line works
Tokeniser tokeniser(cin);
return 0;
}
//main.cc end

//parts of tokeniser header file given
class Tokeniser {
// Since our queries are short, internally the Tokeniser pretokenises
// the whole stream. In other situations this may not be wise.
std::vector<std::string> tokens;
std::vector<std::string>::iterator tokenIndex;
public:
// This is not a Singleton because each tokeniser wraps an istream
// and must have its own state: the number of tokens consumed so far
Tokeniser(std::istream&);

// Routines to peek ahead at and extract tokens
std::pair<Token, std::string> lookahead(bool phrase = false);
std::pair<Token, std::string> next(bool phrase = false);

// Defining this means we can use constructs like 'while (tokeniser)'
// It is defined to return NULL (effectively false) when there are
// no tokens left and this otherwise.
operator void *();
};

Thank you in advance
Jun 3 '07 #1
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The compiler complains thats i have a undefined reference to Tokeniser::Tokeniser(std::basic_istream(char, std::char_traits(char))&)

The line Test tester(cin) works as since it was written by myself.
However, whenever i try executing Tokeniser tokeniser(cin) i get the compilation error.
I'm a little confused here.

First, you don't get a compilation error when you execute a function. I suspect your wording is not accurate.

Second, I do see the Tokeniser(instream&) in the class definition, but I do not see that you ever coded the function. Could you supply the test.cc??

I expect you have a link error rather than a compile error since it is linkers that usually produce the "unresolved external reference" errors.
Jun 3 '07 #2

P: 5
the implementation of the Tokeniser was never given
All that was given was the header file Tokeniser.h and libparser.a which is a static library that contains the Tokeniser class...thats how i have interpreted it.

//START OF TEST CLASS
#include <string>
#include <iostream>
#include "Test.h"

using namespace std;

Test::Test(istream& in)
{
string test;
in >> test;
cout << test;
}
//END OF TEST CLASS

g++ -c -O -Wall -I/usr/cs3/cs3/soft3301/ass3/parser/ main.cc
g++ -c -O -Wall test.cc
g++ -g -Wall -o search.exe main.o test.o -L/usr/cs3/cs3/soft3301/ass3/parser/ -l
parser -static
main.o:main.cc:(.text+0x102): undefined reference to `Tokeniser::Tokeniser(std::
basic_istream<char, std::char_traits<char> >&)'
collect2: ld returned 1 exit status
make: *** [search.exe] Error 1
Jun 3 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I think I found your problem right here:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Tokeniser.h"
  3. #include "Test.h"
  4.  
  5. using namespace std;
  6.  
At the time Tokeniser.h is included the using namespace std has not occurred. That means there is no std::istream.

Change you code to:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "Tokeniser.h"
  5. #include "Test.h"
  6.  
  7.  
and it should work.
Jun 3 '07 #4

P: 5
I think I found your problem right here:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Tokeniser.h"
  3. #include "Test.h"
  4.  
  5. using namespace std;
  6.  
At the time Tokeniser.h is included the using namespace std has not occurred. That means there is no std::istream.

Change you code to:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "Tokeniser.h"
  5. #include "Test.h"
  6.  
  7.  
and it should work.
nope...not working..I'm still getting the same error
Jun 3 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
The using namespace std was outof place.

However, I am not really versed in makefiles but in the one below I don't where libparser.a is ever used.

g++ -g -Wall -o search.exe main.o test.o -L/usr/cs3/cs3/soft3301/ass3/parser/ -l
parser -static
Jun 3 '07 #6

P: 5
The using namespace std was outof place.

However, I am not really versed in makefiles but in the one below I don't where libparser.a is ever used.
I have tried placing using namespace std above Tokeniser.h but it does not seem to work

libparser.a is called in -lparser

lib and the extension .a is dropped according to the syntax given on other websites

library location is given in -L[directory]
Jun 3 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Then you are down to Tokeniser::Tokeniser(std::istream&) is not in your library.
Jun 3 '07 #8

P: 5
Then you are down to Tokeniser::Tokeniser(std::istream&) is not in your library.
It should be according to the assignment specs below =/
Or have i misinterpreted it

You will be provided with a parser to build a query expression tree from input. The parser is
provided in the directory ~cs3/soft3301/ass3/parser which contains header files and the file
libparser.a which you should link against. Do not copy the header files contained in this
directory or libparser.a into your project but instead make sure you compile with (and
your Makefile includes) the appropriate I L and l options.
The parser suite is comprised of a Tokeniser class, a Parser class and a NodeFactory class. The
Tokeniser class takes an istream and produces a token stream. The Parser take a Tokeniser and
returns a Node which is the root of the expression tree. The Parser creates the Node elements of
the query tree using the NodeFactory. You must implement a NodeFactory and the various
different types of Node subclasses.
A tokeniser/parser pair should be constructed and used as follows.
Tokeniser tokeniser(cin);
Parser& parser = Parser::getParser(NodeFactory::getNodeFactory());
Node *query = parser.query(tokeniser);
Thank you for your help so far...I really appreciate it =)
Jun 3 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.