in my project, I have A.cpp and B.cpp,
A.cpp call public function from B.cpp
eg:
1 2 3 4 5
//B.cpp
class B{
public:
void msg();
}
1 2 3 4 5 6 7
//A.cpp
void msg_mn(){
B *objB;
if(findObject(objB)){
objB->msg();
}
}
Step 1. I set Rebuild
I got some error: error LNK2019, error LNK2001
Step 2. I try deleted A.obj and set build ( not Rebuild)
A.obj created again and I got some error( same Step 1)
Step 3. I try deleted B.obj and set build ( not Rebuild)
=>B.obj created again and Build success
Step 4. I try set Rebuild
=> Error( same Step 1)
Can you help me to explain? and How to solve it?
Thanks.
Along with what MikeyBoy said it is most often better to post the whole code that can be compiled and not just what you think is the problem because it may start somewhere else that is not shown.
You have defined B *objB;, but where has this variable been given an address? As is it just contains a garbage value, the best I can tell. If that is the case then it is point to something that is not class "B".
Sometimes these linker errors, (the "LNK" in the error code), come from something being misspelled. Which would mean that the compiler can not find what it is looking for. I does not look like that case here.
Another possibility is that class "A" is compiled before class "B" and the compiler does not know about class "B" yet.
For now I am thinking that you defined a pointer and did not give it a proper address to use.
I tried to duplicate your problem in VS 2017, but I do not have all the proper code that you are using to do this.
Your bit of code for class "B" defines a class. This should be in a ".h" file and the function in a ".cpp" file or all in one file. But that is a guess not seeing all the code.
I can't show my code, I'm sorry,
I'm using cmd (windows) to build the project.
my project can run on Linux OK.
I'm trying to run on Windows.
The problem is If I try deleted B.obj ( keep A.obj ) and set build again => Build success (Step 3)
So, I think( as @Handy Andy comment ):
Sometimes these linker errors, (the "LNK" in the error code), come from something being misspelled
=> maybe NOT correct
Another possibility is that class "A" is compiled before class "B" and the compiler does not know about class "B" yet.
#ifndef CLASSES_HPP
#define CLASSES_HPP
class B
{
public:
void msg();
private:
};
class A
{
public:
bool findObject(B* objB); // <--- Needed for line 23.
void msg_mn()
{
B objB;
B *ptrObjB{&objB};
if (findObject(ptrObjB)) // <--- Where is this function defined?
{
ptrObjB->msg();
}
}
private:
};
#endif // !CLASSES_HPP
This worked in my VS 2017. At least it compiled without errors and did run somewhat. Although the functions I defined are empty for now.
Since I do not know what the function "findObject" does I am not sure what to do with lines 20 and 20 along with the if statement.
If you need a separate file for "A" and "B" this is easily to separate.
The only possible thought I have for the linker error is that class "A" calls the function "findObject", but this is not defined anywhere or something is misspelled. Remember the C++ is case sensitive. Since you can not show the full code this is a guess for now.
The links that Duthomhas has posted are a good place to start. I know that sometimes Microsoftss's explainations are a bit hard to understand at first.
Thank all for your support,
I want to close thread, I resolved.
I reject all file, and put again. ( The order is different from old) and Rebuild OK
I dont know Why?
It has been a long time since I have done this, but I believe that if you edit the OP you can change the icon to a green check to tell everyont that you are done,