开发者

C++ compiling problem; class methods

I have started writing a very simple class, and all kinds of class methods seem to give me problems. I hope the problem is me and the solution is simple.

The command g++ -o main main.cpp gives the folowing output:

/usr/bin/ld: Undefined symbols:
Lexer::ConsoleWriteTokens()
collect2: ld 开发者_如何学Pythonreturned 1 exit status

main.cpp:

#include<iostream>
#include"lexer.h"


int main(){

   Lexer lexhnd = Lexer();
    std::cout << "RAWR\n";
    lexhnd.ConsoleWriteTokens();
   std::cout << "\n\n";

return 0;
 }

lexer.h:

#ifndef __SCRIPTLEXER
#define __SCRIPTLEXER

#include <iostream>
#include <string>
#include <vector>

#define DEF_TOKEN_KEYWORD 0

struct token{
 int flag;
 std::string data;
};

class Lexer
{
public:
//  bool IsTrue();
//  bool AddLine(char * line);
    void ConsoleWriteTokens(void);

private:
std::vector<token> TOK_list;

};


#endif

lexer.cpp:

bool Lexer::IsTrue(){
return true;
};


 bool Lexer::AddLine(char * line){

token cool;
cool.data = line;

TOK_list.push_back(cool);
string = line;
return true;
};

void Lexer::ConsoleWriteTokens(void){

for (int i = 0; i < TOK_list.size(); i++){
    std::cout << "TOKEN! " << i;
}

return 0;
};

I am using g++ in xcode btw.

Thankyou very much in advance, I have been on this problem for a few hours.

EDIT:

g++ -o main lexer.h main.cpp
or
g++ -o main lexer.cpp main.cpp
or
g++ -o main main.cpp lexer.cpp

do NOT work either. -Hyperzap


Your not compiling the lexer.cpp code.

Try

g++ -o main main.cpp lexer.cpp

as your compilation command.

PROBLEMS IN THE lexer.cpp

You probably want to include the lexer header in the lexer.cpp file

#include "lexer.h"

Also, you don't want to return an integer from void functions.

void Lexer::ConsoleWriteTokens(void){
  for (int i = 0; i < TOK_list.size(); i++){
    std::cout << "TOKEN! " << i;
  }
  //This function is void - it shouldn't return something
  //return 0;
};

Finally, you have some problems withs this function

bool Lexer::AddLine(char * line){

  token cool;
  cool.data = line;

  TOK_list.push_back(cool);
  //what is this next line trying to achieve?  
  //string = line;
  return true;
};

I'm not sure what you are trying to achieve with the line I commented out, it doesn't seem to do anything and string isn't defined (did you mean std::string mystring = line;)

Finally, don't forget to uncomment the functions declaired in lexer.h that you are defining in lexer.cpp.


Include all the .cpp files in the command line, like this:

g++ -o main main.cpp lexer.cpp

When your project grows, it becomes wise to manage your project in some automatic way: Makefiles, ant, or some IDE-integrated project file.


Well g++ -o main main.cpp lexer.cpp sould do the trick. However I suggest making makefile files. When having a multiple amount of file they come in handy. I would also suggest adding some optimization to your compilation like -O3 or -O2 (O is a letter o not zero digit!). The difference in execution speed is very noticable. Also if you are goig to make libraries out of your files, why not using --shared option that will create a liked library. I find making shared libraries very useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜