C++编译问题;类方法

发布于 2024-11-05 23:47:42 字数 1448 浏览 1 评论 0原文

我已经开始编写一个非常简单的类,各种类方法似乎给我带来了问题。我希望问题出在我身上,并且解决方案很简单。

命令 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2024-11-12 23:47:43

您没有编译 lexer.cpp 代码。

尝试

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

作为您的编译命令。

lexer.cpp 中的问题

您可能希望在 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;
};

我不确定你想用我注释掉的行来实现什么,
它似乎没有做任何事情,并且 string 没有定义(您的意思是 std::string mystring = line;)

最后,不要忘记取消注释 lexer 中声明的函数您在 lexer.cpp 中定义的 .h。

Your not compiling the lexer.cpp code.

Try

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

as your compilation command.

PROBLEMS IN THE lexer.cpp

You probably want to include the lexer header in the lexer.cpp file

#include "lexer.h"

Also, you don't want to return an integer from void functions.

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

Finally, you have some problems withs this function

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

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 in lexer.cpp.

玩世 2024-11-12 23:47:43

在命令行中包含所有 .cpp 文件,如下所示:

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

当项目增长时,以某种自动方式管理项目变得明智:Makefile、ant 或某些 IDE 集成的项目文件。

Include all the .cpp files in the command line, like this:

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

When your project grows, it becomes wise to manage your project in some automatic way: Makefiles, ant, or some IDE-integrated project file.

莫言歌 2024-11-12 23:47:43

那么 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文