C编译问题;类方法
发布时间:2020-12-16 02:59:25 所属栏目:百科 来源:网络整理
导读:我已经开始写一个非常简单的类,并且各种类的方法似乎给我的问题.我希望问题是我,解决方案很简单. 命令g -o main main.cpp给出了以下输出: /usr/bin/ld: Undefined symbols:Lexer::ConsoleWriteTokens()collect2: ld returned 1 exit status main.cpp中: #i
我已经开始写一个非常简单的类,并且各种类的方法似乎给我的问题.我希望问题是我,解决方案很简单.
命令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 << "RAWRn"; lexhnd.ConsoleWriteTokens(); std::cout << "nn"; 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 也不要工作. 解决方法
你没有编译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; }; 我不知道你试图用我注释出来的那行来实现, 最后,不要忘记取消在lexer.cpp中定义的lexer.h中声明的函数的注释. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |