【发布时间】:2011-08-20 04:55:38
【问题描述】:
我已经开始写一个很简单的类了,各种类的方法似乎都给我带来了麻烦。我希望问题出在我身上,解决方法很简单。
命令 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
【问题讨论】:
-
+1 用于包含错误、您用于编译的命令和一个最小示例。一个很好的问题。
-
我认为,您应该学习编写 Makefile 或使用 automake/autoconf 或 cmake 或 qmake 或使用可以为您做任何事情的 IDE。
标签: c++ class compiler-construction methods