0

I have a header file, let's call it header1.h with the definition of a function, let's say myFunc() in it. My C++ project has a source file, let's call it main.cpp and a header file main.h. I have included header1.h in the main.h and then included main.h in main.cpp.

In the main.cpp I have a class constructor let's call it MyClass and I have this code:

MyClass:MyClass(...)
.
.
{
  .
  .
  f = myFunc(...);
  .
}

when I compile the code I get this error:

error LNK2019: unresolved external symbol _myFunc referenced in function  

What is the reason for this error?

TJ1
  • 7,578
  • 19
  • 76
  • 119

3 Answers3

1

That is a linker error. The file which contains the definition of myFunc is not being compiled, or you are not linking to the library which exported it.

justin
  • 104,054
  • 14
  • 179
  • 226
1

Do you have an implementation for myFunc? Have you only declared myFunc() in the header and not defined it?

You can fix this by defining your function.

void myFunc(); // Declaration
void myFunc() {} // Definition

This error is caused because the the symbol that is myFunc has no definition and therefore cannot be resolved by the linker.

Aesthete
  • 18,622
  • 6
  • 36
  • 45
1

You propably miss to provide the library / object file with myFunc to your linker.

Matthias
  • 8,018
  • 2
  • 27
  • 53