Linker Errors

Hi, I'm trying to create a snake game. For that I created some classes (Coordinates, Snake and Board).The classes and their data fields are something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 //main.cpp
 #include "Board.h"
 
 int main()

 //Board.h
 #include "Snake.h"
  
 class Board {
 Snake snake;
 Coordinates food;
 std::deque< std::deque<char> > cells;
 };
 //Snake.h
 #include <deque>
 #include "Coordinates.h" 

 class Snake {
 std::deque<Coordinates> body;
 };
 //class Coordinates only has 2 ints, so nothing #included 

I tried to follow http://www.cplusplus.com/forum/articles/10627/ so each .cpp #includes its own header and nothing more and each .h is "guarded" with #ifndef, #define and #endif. Even though I did all that, I'm still getting some linker errors. Can anyone help me doing the right #includes?

[EDIT] I also have other #includes, could that be the problem?
The others are:
iostream
conio.h
cstdlib
time.h
Last edited on
I'm confused what's going on here O.o...

Maybe this might help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Board.h"
#include "Snake.h"
#include "Coordinates.h"
#include <deque>

class Board 
{
	Snake snake;
	Coordinates food;
	std::deque<std::deque<char>> cells;
};

class Snake {
std::deque<Coordinates> body;
};

int main()
{
	// Do something here?
	return 0;
}


Also note that class is private by default.
Not completely sure what you're trying to accomplish though.. Sorry ^^

Also on your Header files, at the top try using:
 
#pragma once 

This will keep the compiler from re-linking the same headers over and over again.
Last edited on
If you could say what errors are you getting, that will be great
Also, look at http://www.cplusplus.com/forum/general/113904/

> each .cpp #includes its own header and nothing more
include what you need.
I'm sorry I wasn't clear. What I wrote above isn't the actual whole code, just the really important things. So I actually have the main() defined.
Since the compiler reports linker errors, I assume the cause is somewhere in the #includes. I even though it was because I #included every header file twice (Board.h in Main.cpp and Board.cpp; Snake.h in Board.h and Snake.cpp; Coordinates.h in Snake.h and Coordinates.cpp). But there's no way around that, I think.

[EDIT] The errors are the following:
error LNK2019: unresolved external symbol "public: __thiscall Coordinates::Coordinates(void)" (??0Coordinates@@QAE@XZ) referenced in function "public: __thiscall Board::Board(class Board const &)" (??0Board@@QAE@ABV0@@Z) C:\...\visual studio 2013\Projects\Snake\Snake\Board.obj

error LNK2019: unresolved external symbol "public: __thiscall Snake::Snake(void)" (??0Snake@@QAE@XZ) referenced in function "public: __thiscall Board::Board(class Board const &)" (??0Board@@QAE@ABV0@@Z) C:\...\visual studio 2013\Projects\Snake\Snake\Board.obj

error LNK2019: unresolved external symbol "public: __thiscall Board::Board(void)" (??0Board@@QAE@XZ) referenced in function _main C:\...\visual studio 2013\Projects\Snake\Snake\Source.obj

error LNK1120: 3 unresolved externals C:\...\visual studio 2013\Projects\Snake\Debug\Snake.exe

Now that I think about it, I only declared the default constructors, didn't implement them. Is that it?
Last edited on
Well the "unresolved externals" means it's not being able to find what you're asking it to find in your code.

So it seems that you've possibly written a function in .cpp file that you thought was in the header file but it isn't..

Example of Error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Header File.h
class TestOne
{
private:
public:
    TestOne();
    ~TestOne();

    void myTestFunc();
};

// My CPP File.cpp
#include "Header File.h"
TestOne::TestOne()
{
}
TestOne::~TestOne()
{
}

void myTestFunc() // <--- This it the Error here
{
}


Would change that error line to this..
1
2
3
void TestOne::myTestFunc()
{
}


Reason being, is that myTestFunc is inside the scope of TestOne class.

Do you see something like this inside your code?
Yeah, something like that. I called (explicitly) the default constructors which were declared but not defined.
Thank you both.
Topic archived. No new replies allowed.