Whenever I compile something that # includes a custom class, I get the following compilation errors: main.cpp: undefined reference to Complex::Complex(double, double)
I reduced the problem to a set of three extremely simple files: main.cpp and, for example, Complex.h and Complex.cpp. I am still getting undefined help errors. I am developing in Code :: Blocks on Windows, but I am getting the same thing using g ++ in Ubuntu. Why is this happening? I tried to build Complex.cpp before main.cpp in Code :: Blocks, and I tried g++ main.cpp Complex.cpp as much as I tried only g++ main.cpp . The same mistakes every time.
#include "Complex.h" int main() { Complex A(1.0, 1.0); return 0; } #ifndef _COMPLEX_H #define _COMPLEX_H class Complex { public: double x, y; Complex(double real, double imag); }; #endif #include "Complex.h" Complex::Complex(double real, double imag) { x = real; y = imag; }
ed: I now have different errors, so I have to do something completely wrong. Using the same code as above, I get:
main.cpp: in function 'int main()': main.cpp:5:5: error: 'Complex' was not declared in this scope main.cpp:5:13: error: expected ';' before 'A'
This is strange. Everything worked before when I had a class in a .cpp file, but this is “bad practice”, so I moved the class definitions to .h files and saved the implementation in .cpp files, and now nothing works.
source share