I have a solution called IncludeFileHelp which uses SampleClass.h and SampleClass2.cpp.
#include <SampleClass2.h>
void main() {
SampleClass2 sampleClass2Obj;
sampleClass2Obj.printHello();
}
SampleClass.h
#pragma
#include <iostream>
class SampleClass2 {
public:
SampleClass2() {}
void printHello();
};
SampleClass2.cpp
#include <SampleClass2.h>
void SampleClass2::printHello() {
std::cout << "Hello from SampleClass2!" << std::endl;
}
I added to correct directory path of SampleClass2.h and SampleClass.cpp to the Additional Include Directories.
When I add SampleClass2.h to the Header Files in Solution Explorer and add SampleClass2.cpp to the Source Files in Solution Explorer, the program runs without any problems.
However, if I remove SampleClass2.h and SampleClass2.cpp from the Solution Explorer I get this error:
unresolved external symbol "public: void __thiscall SampleClass2::printHello(void)" (?printHello@SampleClass2@@QAEXXZ) referenced in function _main
The only way I know how to resolve this error is to drag and drop the SampleClass2.h and SampleClass2.cpp files back to the Solution Explorer. This becomes a tedious problem when copying or cloning the solution, as I have to manually add all the included files even though Visual Studio recognizes the class and it's functions.
What I can do so that my program can run in Visual Studio 2019 without having to have the included class header files and.cpp files in Solution Explorer?
The only thing I can think of is changing #pragma to:
#ifndef SAMPLECLASS2
#define SAMPLECLASS2
...
#endif /*SAMPLECLASS2*/
but that does not work.
The most common method is to create a lib or dll. Then link the lib or dll to the project to meet your needs. You could refer the following Microsoft Documents for more information.
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.