Calling functions from other files

Apr 16, 2013 at 3:51am
Hello, thank you for your time. I've ran into an odd error on a project I'm trying to compile. The error is:

 
1>MAIN.obj : error LNK2019: unresolved external symbol "public: void __thiscall CLASS::FUNCTION(void)" (?FUNCTION@CLASS@@QAEXXZ) referenced in function _main


I don't understand what's wrong. Here are some similar examples of what the files look like:

MAIN.cpp
1
2
3
4
5
6
#include "CLASS.h"
int main()
{
CLASS a;
a.FUNCTION();
}


CLASS.h
1
2
3
4
5
class CLASS{
public:
int VARIABLE;
void FUNCTION();
};


CLASS.cpp
1
2
3
4
5
#include "CLASS.h"
void CLASS::FUNCTION()
{
VARIABLE = 5;
}



Does anyone know what's wrong? Is it a compiler error, or is there something I need to add to my code?
Apr 16, 2013 at 3:56am
My guess is that you aren't including CLASS.cpp in your project if you're using an IDE, or specifying it on the command line if you aren't.

If you're using an IDE, it must be in the project. Simply having the file open in the editor doesn't place it there.

Btw, what's with the capital letters?
Apr 16, 2013 at 12:59pm
We are using Visual Studio 2010, the CLASS.cpp is included in the project. Is there something in the project properties that is causing this, perhaps something we need to add or modify?

The capital letters was just as an example. The actual code has a lot of meaningless things in it, and I'm just trying to focus on the problem at hand, not everything else.

Also, sorry for the late response, fell asleep at the monitor.
Last edited on Apr 16, 2013 at 12:59pm
Apr 16, 2013 at 1:28pm
Does your solution explorer look like this?

http://s21.postimg.org/r2qf0sg6f/Project_Files.png
Apr 16, 2013 at 1:33pm
i know you posted this:

1
2
3
4
5
6
#include "CLASS.h"
void CLASS::FUNCTION()
{
VARIABLE = 5;
}


but check in real/actual code that you've prepended the function name with the name of your class and scope resolution operator.
Last edited on Apr 16, 2013 at 1:33pm
Apr 16, 2013 at 1:57pm
cire, Yes, my solution explorer looks exactly like that (of course, altered appropriately). P.S. Titling the project as 'Junk' made for a good chuckle around the project staff. Thank you :)

Mutexe, yes I have.

For the sake of showing the problem, I'm adding this:

Grid.h
1
2
3
4
5
class Grid{
public:
	int DEMO[5][5];
	void RESET();
};


Grid.cpp
1
2
3
4
5
6
7
8
#include "Grid.h"

void Grid::RESET()
{
	for (int a = 0; a < 5; a++)
		for (int b = 0; b < 5; b++)
			DEMO[a][b] = 0;
}


main.cpp
1
2
3
4
5
6
7
#include "Grid.h"

int main()
{
	Grid Game;
	Game.RESET();
}


Error:
 
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Grid::RESET(void)" (?RESET@Grid@@QAEXXZ) referenced in function _main
Last edited on Apr 16, 2013 at 2:05pm
Apr 16, 2013 at 2:10pm
If you do something that generates an error in Grid.cpp, does it still compile and give that linker error? If so, that means it's not even compiling Grid.cpp
Simple test: put #error as the first line in Grid.cpp
Apr 16, 2013 at 2:18pm
L B, this was the following error:
 
1>c:\...\grid.cpp(1): fatal error C1189: #error : 


P.S. Understand that I have the intelligence of a grapefruit, so if it's something so simple that you're not even suggesting it as a possible problem, please suggest it as a possible problem.
Last edited on Apr 16, 2013 at 2:24pm
Apr 16, 2013 at 2:27pm
That's really confusing. So, it is compiling both cpp files, but it isn't linking both - it seems to be only linking main.cpp.

Have you tried looking around your project settings to make sure you didn't somehow exclude Grid.cpp from the linker tool?
Apr 16, 2013 at 2:35pm
I don't see anything that specifically excludes Grid.cpp in Linker or C/C++ under Project Properties.

Also, this is my last post for the next few hours, must rush to school! I apologize for the delay.
Last edited on Apr 16, 2013 at 2:36pm
Apr 16, 2013 at 2:38pm
My only advice would be to try removing it from the project (not deleting it) and then re-add it (Add Existing File) to see if that resets any broken settings.
Apr 16, 2013 at 2:40pm
that example is compiling fine for me, so it's either down to project settings like LB suggested, or your supplied example code isn't a 100% reflection of your actual code.
Apr 16, 2013 at 3:50pm
I'm not at the computer with the project, but I will try as LB suggested, and report back with a success of failure. Thank you.

mutexe, the second set of samples were copies from my code, I just removed a majority of what was inside.
Apr 17, 2013 at 2:37am
L B, your plan worked. I excluded all files, headers included, from the project. I then added them all back in, and it linked them! Thank you very much!
Topic archived. No new replies allowed.