C编译问题;类方法

前端之家收集整理的这篇文章主要介绍了C编译问题;类方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经开始写一个非常简单的类,并且各种类的方法似乎给我的问题.我希望问题是我,解决方案很简单.

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

@H_502_4@/usr/bin/ld: Undefined symbols: Lexer::ConsoleWriteTokens() collect2: ld returned 1 exit status

main.cpp中:

@H_502_4@#include<iostream> #include"lexer.h" int main(){ Lexer lexhnd = Lexer(); std::cout << "RAWR\n"; lexhnd.ConsoleWriteTokens(); std::cout << "\n\n"; return 0; }

lexer.h:

@H_502_4@#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:

@H_502_4@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.

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

编辑:

@H_502_4@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代码.

尝试

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

作为您的编译命令.

lexer.cpp中的问题

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

@H_502_4@#include "lexer.h"

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

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

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

@H_502_4@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&C++相关文章