C编译问题;类方法

我已经开始写一个非常简单的类,并且各种类的方法似乎给我的问题.我希望问题是我,解决方案很简单.

命令g -o main main.cpp给出了以下输出

/usr/bin/ld: Undefined symbols:
Lexer::ConsoleWritetokens()
collect2: ld returned 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;
};

我在xcode btw中使用g.

非常提前谢谢,我一直在这个问题上几个小时.

编辑:

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

也不要工作.
-Hyperzap

解决方法

你没有编译lexer.cpp代码.

尝试

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

作为您的编译命令.

lexer.cpp中的问题

您可能希望将lexer标题包含在lexer.cpp文件

#include "lexer.h"

另外,你不想从void函数返回一个整数.

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;
};

最后,你有这个功能的一些问题

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;
};

我不知道你试图用我注释出来的那行来实现,
它似乎没有做任何事情,字符串没有定义(你的意思是std :: string mystring = line;)

最后,不要忘记取消在lexer.cpp中定义的lexer.h中声明的函数的注释.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...