嗨,我需要你的帮助! 当我编译运行时错误是:
/tmp/ccSOgpjn.o:在函数Collection::evaluate()':
fitnessTest.cpp:(.text._ZN10Collection8evaluateEv[_ZN10Collection8evaluateEv]+0x45): undefined reference to
中,fitnessFunction :: doEvaluation(std :: __ cxx11 :: basic_string,std :: allocator>)'
/tmp/ccSOgpjn.o:在函数Collection::writeIndividual(char**, int)':
fitnessTest.cpp:(.text._ZN10Collection15writeIndividualEPPci[_ZN10Collection15writeIndividualEPPci]+0x3a): undefined reference to
中readIndividual :: read [abi:cxx11](char **,int)'
collect2:错误:ld返回1退出状态
我不明白为什么?
我的代码是:
class fitnessFunction
{
public:
virtual int doEvaluation(string x);
};
class OneMax: public fitnessFunction
{
public:
virtual int doEvaluation(string x) {
int count = 0;
for (int i = 0; i < x.size(); i++)
if (x[i] == '1') count = count + 1;
return count;
}
};
class readIndividual
{
public:
string read(char * argv[], int i);
};
class OneMaxIndividual: public readIndividual
{
public:
virtual string read(char * argv[], int i) {
string inputFile = argv[i+1];
ifstream input(inputFile.c_str());
string x;
input >> x;
input.close();
return x;
}
};
class Collection
{
public:
fitnessFunction* m_function;
readIndividual* m_individual;
string individual;
public:
Collection(){}
void set_function(fitnessFunction* s){
m_function = s;
}
void set_individual(readIndividual* s){
m_individual = s;
}
int evaluate() {
m_function->doEvaluation(individual);
}
void writeIndividual(char* argv[], int i) {
individual = m_individual->read(argv,i);
}
};
int main(int argc, char* argv[])
{
int result = 0;
string outputFile = "fitness.out";
ofstream output( outputFile.c_str() );
OneMax fitnessFunction;
OneMaxIndividual individualObj;
Collection collection;
collection.set_function(&fitnessFunction);
collection.set_individual(&individualObj);
for(int i = 0; i < argc/2; i++){
//lettura individo
collection.writeIndividual(argv,i);
result = collection.evaluate();
output << result << endl;
}
for(int i = argc/2; i < argc-1; i++){
collection.writeIndividual(argv,i);
result = collection.evaluate();
output << result << endl;
}
output.close();
return 0;
}
我尝试了一些像我这样的问题,但没有找到解决问题的答案。以上代码全部在一页中。
答案 0 :(得分:0)
fitFunction类中的doEvaluation(string)函数没有实现。
您有两种选择: - 通过在末尾附加= 0或使其成为纯虚拟 - 创建虚拟实现
我认为它是纯虚拟的,所以编译器强迫你在派生类中创建实现。