gpt4 book ai didi

c++ - 从多个文件编译得到 “undefined reference”

转载 作者:行者123 更新时间:2023-12-02 10:59:38 43 4
gpt4 key购买 nike

我需要在单独的文件中提供CFG类,但是我不确定如何将其与相关的.h和主程序一起编译。

我已经将.h文件#include编码了,并且我已经在命令行中请求了这两个文件,但是我不确定为什么将它们一起编译是错误的。

有什么想法吗?

CFG.cpp:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
public:
string code[25];
char startNT;
//private:

CFG(string inCode[], int stringLen)
{
for (int a = 0; a < stringLen; a++)
{
//cout << inCode[a] << endl;
this->code[a] = inCode[a];
}
for (int a = 0; a < stringLen; a++)
{
cout << this->code[a] << endl;
}
}

char getStartNT()
{
return startNT;
}

void setStartNT(char stNT)
{
startNT = stNT;
}

bool processData(string inString, string wkString)
{
//Our recursive function
return true;
}

void garbage()
{
return;
}
};

CFG.h:
#ifndef _cfg_h_
#define _cfg_h_

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
public:
string code[25];
char startNT;

CFG(string inCode[], int stringLen);
char getStartNT();
void setStartNT(char stNT);
bool ProcessData(string inString, string wkString);
void garbage();
};

#endif

cfg_entry.cpp:
#include <stdio.h>
#include <iostream>
#include "cfg.h"

using namespace std;

int main()
{
string inArray[5];
inArray[0] = "test0";
inArray[1] = "test1";
inArray[2] = "test2";
inArray[3] = "test3";
inArray[4] = "test4";
CFG * cfg1 = new CFG(inArray, 5);
cfg1->garbage();
return 0;
}

编译错误:
art@tv:~/Dropbox/Weber/CS 4110/Individual Assignment 2$ g++ -g -std=c++11 -Wall -o cfg_entry cfg.cpp cfg_entry.cpp
/tmp/ccICQEd0.o: In function `main':
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:15: undefined reference to `CFG::CFG(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)'
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:16: undefined reference to `CFG::garbage()'
collect2: error: ld returned 1 exit status

最佳答案

我发现了我的问题。在我的情况下,头文件定义了类,.cpp文件再次重新定义了它,试图创建2个CFG类实例。 .h需要处理类声明和变量实例化,而.cpp仅处理函数定义。

cfg.h:

#ifndef _cfg_h_
#define _cfg_h_

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
private:
string code[25];
char startNT;

public:
CFG(string inCode[], int stringLen);
char getStartNT();
void setStartNT(char stNT);
bool processData(string inString, string wkString);
void garbage();
};

#endif

cfg.cpp:
#include <iostream>
#include <stdio.h>
#include <string>
#include "cfg.h"

using namespace std;

CFG::CFG(string inCode[], int stringLen)
{
for (int a = 0; a < stringLen; a++)
{
//cout << inCode[a] << endl;
this->code[a] = inCode[a];
}
for (int a = 0; a < stringLen; a++)
{
cout << this->code[a] << endl;
}
}

char CFG::getStartNT()
{
return startNT;
}

void CFG::setStartNT(char stNT)
{
startNT = stNT;
}

bool CFG::processData(string inString, string wkString)
{
//Our recursive function
return true;
}

void CFG::garbage()
{
return;
}

关于c++ - 从多个文件编译得到 “undefined reference”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46781192/

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