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 中使用 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
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 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;
};
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有编译 lexer.cpp 代码。
尝试
作为您的编译命令。
lexer.cpp 中的问题
您可能希望在 lexer.cpp 文件中包含词法分析器头。
此外,您不希望从 void 函数返回整数。
最后,你对这个函数有一些问题,
我不确定你想用我注释掉的行来实现什么,
它似乎没有做任何事情,并且 string 没有定义(您的意思是 std::string mystring = line;)
最后,不要忘记取消注释 lexer 中声明的函数您在 lexer.cpp 中定义的 .h。
Your not compiling the lexer.cpp code.
Try
as your compilation command.
PROBLEMS IN THE lexer.cpp
You probably want to include the lexer header in the lexer.cpp file
Also, you don't want to return an integer from void functions.
Finally, you have some problems withs this function
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 inlexer.cpp
.在命令行中包含所有 .cpp 文件,如下所示:
当项目增长时,以某种自动方式管理项目变得明智:Makefile、ant 或某些 IDE 集成的项目文件。
Include all the .cpp files in the command line, like this:
When your project grows, it becomes wise to manage your project in some automatic way: Makefiles, ant, or some IDE-integrated project file.
那么 g++ -o main main.cpp lexer.cpp 应该可以解决问题。不过我建议制作 makefile 文件。当有多个文件时,它们会派上用场。
我还建议在编译中添加一些优化,例如 -O3 或 -O2 (O 是字母 o 而不是零数字!)。执行速度的差异非常明显。另外,如果您打算从文件中创建库,为什么不使用 --shared 选项来创建喜欢的库。我发现共享库非常有用。
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.