为什么我在我的C 代码中获得链接命令失败错误

Why do I get linker command fail error in my c++ code?

本文关键字:链接 命令 失败 错误 我的 代码 为什么      更新时间:2023-10-16

我在我的代码中求解链接错误时遇到了困难。我不明白为什么我会得到这个。我试图重写它,制作相同的参数,但没有任何改变。有什么想法吗?

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 == 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  taskn";
    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;
}

冠军

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

链接器错误,构建代码时得到的:

g -o"/卷/1TB HDD/coding/op/masodikbead/main"/oblumes/1tb hdd/coding/op/masodikbead/main.o"
架构X86_64的未定义符号: "冠军::冠军(STD :: __ 1 :: BASIC_STRING,STD :: __ 1 :: Artocator> const&amp;(",从: first_max(std :: __ 1 :: basic_string,std :: __ 1 ::分配器> const&amp; ,,冠军&amp; amp;(LD:符号(s(架构x86_64找不到符号clang:错误:Linker命令因出口代码1失败(使用-V查看调用(以状态1(0分钟,0秒(终止过程0错误(S(,1个警告(S((0分钟,0秒(

检查存在:/卷/1TB HDD/CODING/OP/MASODIKBEAD/MAIN

您必须构建类似的代码:

g++ "path to your main.cpp" "path to your champship.cpp" -o a.out

请参阅此问题。

您也可以参考本文。

请记住,您必须在编译过程中包括所有.cpp文件(如果您使用的话(。在您的情况下,您使用的是ChampshipEnor对象,在您的主函数中,并且类在champship.cpp中定义。