1

I checked multiple other posts and this must work. This code below must be correct.

I am simply declaring the class in .h and .cpp with a constructor that sets one parameter in the same line.

Why the compiler complains in the error below?

.h

#ifndef WOOD_ELEMENT_H
#define WOOD_ELEMENT_H

class element
{
int id = -1;
public:
element(int _id);
};

#endif

cpp

#include "wood_element.h"


element::element(int _id) : id(_id)
{
    // ...
}

Error:

 3>wood_main.obj : error LNK2019: unresolved external symbol "public: __cdecl element::element(int)" (??0element@@QEAA@H@Z) referenced in function "public: class element * __cdecl std::vector<clas
   s element,class std::allocator<class element> >::_Emplace_reallocate<int &>(class element * const,int &)" (??$_Emplace_reallocate@AEAH@?$vector@Velement@@V?$allocator@Velement@@@std@@@std@@QEAA 
   PEAVelement@@QEAV2@AEAH@Z) [C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\compas_wood\build_win\wood.vcxproj]
 3>C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\compas_wood\build_win\Release\wood.exe : fatal error LNK1120: 1 unresolved externals [C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\compas_wood 
   \build_win\wood.vcxproj]
  • 2
    You are not actually linking with the .cpp file that contains the definition of the constructor. Check your build configuration. – Igor Tandetnik Oct 29 '22 at 22:51
  • A semicolon is missing after the closing brace in `class element { ... }` – Igor Tandetnik Oct 29 '22 at 22:52
  • @IgorTandetnik Thank you, it is the solution what you just said. The mistake was in CMakeLists. Could you post it as a answer? I will accept it. How can you understand this from the error? – PetrasVestartasEPFL Oct 29 '22 at 22:53
  • Elementary, Watson. The linker says that the definition is missing, but it's clearly present in the code shown. The natural conclusion is that, while you've shown this code to us, you haven't shown it to the linker. "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth." – Igor Tandetnik Oct 29 '22 at 23:03

0 Answers0