File inclusion errors (C ++): undefined link to ______

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.

 /*======== main.cpp ========*/ #include "Complex.h" int main() { Complex A(1.0, 1.0); return 0; } /*======== Complex.h ========*/ #ifndef _COMPLEX_H #define _COMPLEX_H class Complex { public: double x, y; Complex(double real, double imag); }; #endif /*======== Complex.cpp ========*/ #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.

+6
source share
2 answers

This is not a compilation error, this is a link error. You must definitely connect all your objects together. You can do this in several ways:

 g++ main.cpp Complex.cpp 

Should work fine (and does this when I tried with your example). You can also do this step by step:

 g++ -c main.cpp g++ -c Complex.cpp g++ main.o Complex.o 
+6
source

While we are in the dark, is this the actual code (maybe not the way it works for several others), give a comment to the code itself ... (this will not affect the linker error)

  • Names starting with an underscore followed by a capital letter, for example. _COMPLEX_H, reserved for the implementation of the C ++ compiler and the standard C ++ library. Do not use .
  • Member variables are best made private . It is rarely necessary to make the actual data element public (sometimes it is reasonable to create non-functional members of the public , for example, an event class in which users can subscribe to callbacks, but they usually behave like functions, although they are technically objects).
  • Initialization is best done on the member initializer list. That is, the constructor will look something like this:

    Complex :: Complex (double real, double image): x (real), y (capacitive) {}

Finally, to risk a few guesses about what is happening with the real code in order to cause a binding problem:

  • The constructor is defined as inline . Obviously, this will not work if the definition is not visible when the constructor is used.
  • The Complex declaration somehow turned it into an unnamed namespace, and thus the definition defines a different class than the one considered by main.cpp.
+1
source

Source: https://habr.com/ru/post/905153/


All Articles