c++ 'undefined reference to' error -
c++ 'undefined reference to' error -
i'm having 1 of "undefined reference " errors when compiling c++ program. know mutual pitfall, far unable figure out i'm doing wrong.
here's relevant code. ex1two_sum.h:
#ifndef ex1two_sum_h #define ex1two_sum_h #include <vector> using namespace std; namespace ddc { class ex1two_sum { public: void f(); protected: private: }; } #endif
ex1two_sum.cpp:
#include <vector> #include <cstddef> #include <iostream> using namespace std; namespace ddc { class ex1two_sum { public: void f(){ cout << "works" << endl; } }; }
and finally, main.cpp:
#include <iostream> #include "ex1two_sum.h" using namespace std; using namespace ddc; int main() { ex1two_sum ex1; ex1.f(); homecoming 0; }
i compile follows:
g++ -std=c++11 -c ex1two_sum.cpp g++ -std=c++11 -c main.cpp g++ ex1two_sum.o main.o
yielding next message:
main.o: in function `main': main.cpp:(.text+0x2c): undefined reference `ddc::ex1two_sum::f()' collect2: error: ld returned 1 exit status
your source file redefines whole class, inline function definition, when needs provide non-inline function definition.
#include "ex1two_sum.h" void ddc::ex1two_sum::f() { std::cout << "should work\n"; }
also, please don't set using namespace std;
in header. not wants global namespace polluted in potentially surprising ways.
c++ compiler-errors
Comments
Post a Comment