user13088490
user13088490

Reputation:

Undefined reference using namespace C++

When I want to run the code the compiler says undefined reference to math::calc

I read questions and answers about this problem at StackOverflow and it do not help me solve my problem.

Comp.h

#include <utility>
namespace math {
    typedef std::pair<double,double> Comp;
    double getFirst(Comp a);
    ...
}

Comp.cpp

#include "comp.h"
namespace math {
    double getFirst(Comp a) {
        return a.first;
    }
    ...
}

Comp file: every function return Comp or double. I call function from cal.cpp file several times

cal.h

#include "comp.h"
#include "helper.h"
namespace math {
    Comp calc(const string& str);
    ...
}

cal.cpp

#include "eval.h"
namespace math {
    Comp calc(const string& str) {
        ...
    }
}

Cal file: Some functions return with comp type, not just the cal function.

helper.h

namespace math {
    ...
}

helper.cpp

#include "helper.h"
namespace math {
    ...
}

helper file just contains few function that I calling from cal.cpp file. Each function is calling several times.

main.cpp

#include "calc.h"
int main() {
    string str = "  ";
    pair<double,double> res = math::calc(str);
    cout << res.first << " " << res.second << endl;
    return 0;
}

In the whole project, I do not use any classes.

I included every file that I calling except the c++ original file.

I use the std namespace in each file but I do not write it here.

I absolutely no idea what could be the problem with my code.

If I also include cal.cpp in the main.cpp the code editor says undefined reference to each file that I calling from helper.h. I do not want to include cal.cpp in the main file I just mentioned it

Upvotes: 1

Views: 2002

Answers (1)

Eric
Eric

Reputation: 1775

You have to compile all your project's *.cpp files and then link them (results of compilation) properly - the linking process depends on the environment/IDE you're using, just look it up. If you're not linking it properly the final executable wont have all the functions definitions that requires, hence getting undefined reference.

Upvotes: 1

Related Questions