【问题标题】:Why do I get linker command fail error in my c++ code? [duplicate]为什么我的 C++ 代码中出现链接器命令失败错误? [复制]
【发布时间】:2019-05-03 00:28:39
【问题描述】:

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

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 "/Volumes/1TB HDD/Coding/op/masodikbead/main" "/Volumes/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

【问题讨论】:

    标签: c++ linker-errors undefined-symbol


    【解决方案1】:

    你必须像这样构建代码:

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

    参考this问题。

    也可以参考this的文章。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多