Visual studio 2015 unresolved external symbol

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.
I got some error: error LNK2019, error LNK2001

Do you honestly believe we've memorised all the error codes from Visual Studio?

Do you honestly believe everyone here even uses Visual Studio?

If you have error messages, then show us the complete messages.
Last edited on
Hello kakaducsy,

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.

Hope that helps,

Andy
Hello kakaducsy,

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.

Andy
Lets make a simple example, project with two translation units:

Translation unit AA contains:

AA.cpp
1
2
3
4
5
6
#include <BB.h>

int main() {
  BB foo;
  foo.bar();
}


Translation unit BB contains:

BB.h
1
2
3
4
5
6
7
8
#ifndef BBh
#define BBh

struct BB
  void bar();
};

#endif 


BB.cpp
1
2
3
4
5
#include <BB.h>

void BB::bar() {
  // implementation
}



Use your VS to build that and report possible errors.

For the record, I know nothing about VS and like to keep it that way thank you very much.
hi @MikeyBoy , @Handy Andy, @keskiverto

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.
=> How to solve it?

Thank all for your help.

You really aren’t giving us anything to work with.

https://www.google.com/search?q=error+lnk2019
https://www.google.com/search?q=error+lnk2001

Microsoft is pretty good about documenting these things and explaining what may have caused it.
I can't show my code, I'm sorry

Why not?

And you still haven't shown us the full error messages, even though you've been explicitly asked to do so.
Hello kakaducsy,

=> How to solve it?

Not being able to see the complete code and not having the error message this is only a guess at what might work.

The file is "Classes.hpp".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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.

Hope that helps,

Andy

Edit: corrected line 25.
Last edited on
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?
Hello kakaducsy,

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,

Andy
Topic archived. No new replies allowed.