C++ 为什么在我的c++;密码?
我在解决代码中的链接器错误时遇到问题。我不明白为什么我会得到这个。我试图重写它,使用相同的参数,但没有任何改变。有什么想法吗 main.cppC++ 为什么在我的c++;密码?,c++,linker-errors,undefined-symbol,C++,Linker Errors,Undefined Symbol,我在解决代码中的链接器错误时遇到问题。我不明白为什么我会得到这个。我试图重写它,使用相同的参数,但没有任何改变。有什么想法吗 main.cpp int first_max(const string &name, Champship& e) { ChampshipEnor t(name); bool l = false; int _max = -1; while(!t.end()){ if(!t.current().isHigh
int first_max(const string &name, Champship& e)
{
ChampshipEnor t(name);
bool l = false;
int _max = -1;
while(!t.end()){
if(!t.current().isHigh == true){
}else if (l){
if(t.current().point > _max){
_max = t.current().point;
e = t.current();
}
}else{
l = true;
_max = t.current().point;
e = t.current();
}
}
return _max;
}
int main(){
string filename;
cout<<"Enter the name of the input file, please:"; cin>>filename;
//First task
cout<<"First task\n";
try{
Champship e;
if(first_max(filename, e)){
cout<<e.racer<<" has scored the most point ("<<e.point<<") in "<<e.year<<endl;
}else{
cout<<"There is no racer matching our search criteria.\n";
}
}catch(ChampshipEnor::FileError err)
{
cerr<<"Can't find the input file:"<<filename<<endl;
}
return 0;
}
链接器错误,生成代码时得到的信息:
g++-o“/Volumes/1TB硬盘/编码/op/masodikbead/main”“/Volumes/1TB硬盘/编码/op/masodikbead/main.o”架构x86_64的未定义符号: “ChampshipEnor::ChampshipEnor(std::u 1::basic_string,std::u 1::allocator>const&)”,引用自: main.o中的第一个_max(std::_1::basic_字符串,std::_1::allocator>const&,Champship&) ld:找不到架构x86_64的符号 叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用) 进程以状态1终止(0分钟,0秒)) 0个错误、1个警告(0分钟、0秒)
检查是否存在:/Volumes/1TB HDD/Coding/op/masodikbead/main您必须构建如下代码:
g++“主.cpp路径”“champship.cpp路径”-oa.out
参考问题
你们也可以参考这篇文章
请记住,在编译过程中必须包含所有.cpp
文件(如果您正在使用)。在您的例子中,您正在主函数中使用ChampshipEnor
对象,并且类在champship.cpp
中定义
//#pragma once
//#include <fstream>
//#include <sstream>
//#include <string>
struct Champship {
std::string racer;
int year;
int point;
bool isHigh = false;
};
class ChampshipEnor{
private:
std::ifstream _f;
Champship _cur;
bool _end;
public:
enum FileError{MissingInputFile};
ChampshipEnor(const std::string &str) throw (FileError);
void first() {next();}
void next();
Champship current() const { return _cur;}
bool end() const { return _end;}
};
#include "champship.h"
ChampshipEnor::ChampshipEnor(const std::string &str) throw (FileError)
{
_f.open(str);
if(_f.fail())throw MissingInputFile;
}
void ChampshipEnor::next()
{
std::string line;
getline(_f , line);
if( !(_end = _f.fail()) ){
istringstream is(line);
is >> _cur.racer >> _cur.year;
_cur.point = 0;
std::string category;
int pos;
for( is >> category >> pos ; !is.fail(); is >> category >> pos ){
if(category == "magasugras"){
_cur.isHigh = true;
}
switch(pos) {
case 1 :
_cur.point += 12;
break;
case 2 :
_cur.point += 10;
break;
case 3 :
_cur.point += 8;
break;
case 4 :
_cur.point += 6;
break;
case 5 :
_cur.point += 4;
break;
case 6 :
_cur.point += 2;
break;
}
}
//if (_cur.high == true && _cur.point > _max) _max = _cur.point;
}
}