简体   繁体   中英

calling a function defined in a header file from a Class constructor in C++

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?

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.

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.

您可能会错过将myFunc提供给链接器的库/目标文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM