-1

I'm having trouble solving a linker error in my code. I do not understand why do I get this. I tried to rewrite it, make the same parameters, but nothing changed. Any idea?

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

The linker error, what I get when I build the code:

g++ -o "/Volumes/1TB HDD/Coding/op/masodikbead/main" "/Volumes/1TB HDD/Coding/op/masodikbead/main.o"
Undefined symbols for architecture x86_64: "ChampshipEnor::ChampshipEnor(std::__1::basic_string, std::__1::allocator > const&)", referenced from: first_max(std::__1::basic_string, std::__1::allocator > const&, Champship&) in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Process terminated with status 1 (0 minute(s), 0 second(s)) 0 error(s), 1 warning(s) (0 minute(s), 0 second(s))

Checking for existence: /Volumes/1TB HDD/Coding/op/masodikbead/main

Benedek
  • 41
  • 4

1 Answers1

0

You have to build the code something like this:

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

Refer this question.

You can also refer this article.

Remember, you have to include all the .cpp files (if your are using) during the compilation. In your case, you are using ChampshipEnor object, in your main function and the class is defined in champship.cpp.

R4444
  • 2,016
  • 2
  • 19
  • 30