gpt4 book ai didi

C++编译问题;类方法

转载 作者:可可西里 更新时间:2023-11-01 16:05:54 24 4
gpt4 key购买 nike

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

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

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

主要.cpp:

#include<iostream>
#include"lexer.h"


int main(){

Lexer lexhnd = Lexer();
std::cout << "RAWR\n";
lexhnd.ConsoleWriteTokens();
std::cout << "\n\n";

return 0;
}

词法分析器.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

词法分析器.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

也不工作。- super 电击

最佳答案

你没有编译 lexer.cpp 代码。

尝试

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

作为你的编译命令。

lexer.cpp 中的问题

您可能希望在 lexer.cpp 文件中包含词法分析器 header

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

我不确定你想用我注释掉的行实现什么,它似乎什么也没做,字符串也没有定义(你是说 std::string mystring = line;)

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

关于C++编译问题;类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com