为什么我的C ++代码中出现链接器命令失败错误?

时间:2019-05-03 00:28:39

标签: 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 == 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;
}

champship.h

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


champship.cpp

#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”“ /卷/ 1TB HDD / Coding / op / masodikbead / main.o”
架构x86_64的未定义符号:   “ ChampshipEnor :: ChampshipEnor(std :: __ 1 :: basic_string,std :: __ 1 :: allocator> const&)”,引用自:       main.o中的first_max(std :: __ 1 :: basic_string,std :: __ 1 :: allocator> const&,Champship&) ld:找不到架构x86_64的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用) 进程以状态1(0分钟,0秒)终止 0个错误,1个警告(0分钟,0秒)

检查是否存在:/ Volumes / 1TB HDD / Coding / op / masodikbead / main

1 个答案:

答案 0 :(得分:0)

您必须构建如下代码:

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

请参阅this问题。

您也可以参阅this文章。

请记住,编译期间必须包括所有.cpp文件(如果正在使用)。就您而言,您在主函数中使用ChampshipEnor对象,并且该类在champship.cpp中定义。