微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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中声明的函数的注释.

原文地址:https://www.jb51.cc/c/112767.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐