undefined error
I get undefined error and I haven't got a clue how to fix it. Any help would be greatly appreciated. Important parts of codes are below:
nr.h
1 2 3 4 5 6
|
#include <vector>
using namespace std;
namespace NR {
void polint(const std::vector<double> &xa, const std::vector<double> &ya, const double x, double &y, double &dy);
}
|
nr.cpp
1 2 3 4 5 6 7 8
|
#include "nr.h"
void polint(const std::vector<double> &xa, const std::vector<double> &ya, const double x, double &y, double &dy) {
vector<int>::size_type n = xa.size();
... Implementation ...
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include "nr.h"
using namespace std;
int main(void) {
unsigned int n;
double x,y,dy;
vector<double> xa(n);
vector<double> ya(n);
NR::polint(xa,ya,x,y,dy);
...
}
|
I cannot get it to compile.
main.cpp:(.text+0x509): undefined reference to `NR::polint(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, double, double&, double&)'
collect2: ld returned 1 exit status
|
It is compiling. You cannot get it to link. Have you compiled nr.cpp and instructed the linker to link it as well as main.o?
Last edited on
All three files are in the same directory. I compiled them as
g++ main.cpp nr.cpp -o main
with an output showed above.
And yes, separately main.cpp and nr.cpp are compiled well but not linked after.
Try this in nr.cpp
1 2 3 4 5 6 7 8
|
#include "nr.h"
void NR::polint(const std::vector<double> &xa, const std::vector<double> &ya, const double x, double &y, double &dy) {
vector<int>::size_type n = xa.size();
... Implementation ...
}
|
You're missing the namespace qualifier on your implementation
1 2 3 4 5 6 7 8
|
#include "nr.h"
void NR::polint(const std::vector<double> &xa, const std::vector<double> &ya, const double x, double &y, double &dy) {
vector<int>::size_type n = xa.size();
... Implementation ...
}
|
*edit* ninja'd!
Last edited on
Solved! Thank you so much for your time!
I cannot get it to link again. I was trying to make a template function from NR::polint as the following
nr.h
1 2 3 4 5 6 7
|
#include <vector>
using namespace std;
namespace NR {
template <class T>
void polint(const std::vector<T> &xa, const std::vector<T> &ya, const T x, T &y, T &dy);
}
|
nr.cpp
1 2 3 4 5 6 7
|
template <class T>
void NR::polint(const std::vector<T> &xa, const std::vector<T> &ya, const T x, T &y, T &dy) {
typename vector<T>::size_type n = xa.size();
... Implementation ...
}
|
main.cpp was not changed at all.
|
NR::polint(xa,ya,x,y,dy);
|
or
|
NR::polint<double>(xa,ya,x,y,dy);
|
lead to the same output:
undefined reference to `int NR::polint<double>(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, double, double&, double&)'
|
Any suggestions?!
Last edited on
Templates has to be defined in the the same translation unit they are used so put the definition of NR::polint in the header file.
Thank you.
Topic archived. No new replies allowed.