# When syntax errors arent enough

## Recommended Posts

Ok, so here I finally get to this point in my life that I can write pretty much syntax error free code, so I'm like, ‘that’s cool, now I can do anything’ well, just as things were looking up, here comes Link errors, to wreck my party. So long story short, I'm writing a 2D battle tank game, (the one were you aim, set bullet force, etc. sorta like worms) in C++ using SDL. Anyway, I'm on VC++ 6 and my code is in 4 parts: Helper.cpp Pixelwar.cpp Terrain.cpp Pixelwar.h I’ll give you the includes/declarations of the files: helper.cpp
#include "sdl.h"
#include <iostream>
#include <memory.h>


Pixelwar.cpp
#include "sdl.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <windows.h>
#include "pixelwar.h"
#include "helper.cpp"
#include "terrain.cpp"

// Functions
int init();
int frame();

//Variables
SDL_Surface* screen;
SDL_Event event;
Uint8* keys;
SDL_Color k;

cTerrain terrain;


terrain.cpp
#include "sdl.h"
#include <iostream>
#include <cmath>
#include "pixelwar.h"

class cTerrain {
public:
void InitTerrain(SDL_Color k);
void PixelOn(int x, int y);
void PixelOff(int x, int y);
void SetColor(SDL_Color k);
SDL_Color GetColor();
void draw(SDL_Surface* screen);
private:
bool PixArray[SCREEN_WIDTH][SCREEN_HEIGHT];
SDL_Color drawcolor;
};

void cTerrain::InitTerrain(SDL_Color k) {
int highY = 0;
for (int x = 0; x < SCREEN_WIDTH; x++) {
highY = ((rand() % SCREEN_HEIGHT) * -1) + SCREEN_HEIGHT;
for (int y = SCREEN_HEIGHT; y > 0; y--){
if (y > highY) PixArray[x][y] = true;
else PixArray[x][y] = false;
}
}
drawcolor = k;
}


pixelwar.h (whole file)
#include "sdl.h"
#include <iostream>

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 32

#ifndef _pixelwar_h_
#define _pixelwar_h_ 1
void SetPixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b);
SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y );
SDL_Color SDL_MakeColor (int r, int g, int b);

#endif



##### Share on other sites
If you are using VC++ 6.0 then you should change the Code Generation in C/C++ tab from Debug Single Threaded to Debug Multithreaded DLL.

If you have already done that then I am not sure why you are getting these errors.

For other compilers I suggest you read the Docs that come with SDL.

##### Share on other sites
You should only ever #include headers (.h, .hpp), never source files (.c, .cc, .cpp). The problem is that you define the same function more than once because each .cpp file (by virtue of being included and compiled by itself) gets compiled multiple times.

##### Share on other sites
I just want to give up and die when I get these. The only solution I have found so far is to not include the files and try rearranging the code in all of the files and just hope things work...
So I'm very interested in how to solve this problem.

##### Share on other sites
yeah man, does anyone know of a really good tutorial on beating these things, i found one once, but i cant find it any more

##### Share on other sites
Kylotan wrote a guide. The important thing, however, is keeping in mind that you must define everything that you use once—and only once. This is why you only place declarations in headers (it doesn't matter how many times you declare symbols), never definitions; definitions go in source files, which shall never be compiled more than once (that is, no two object files should ever contain the same source file, and you should never ever #include a source file).

##### Share on other sites
Never include a file which includes a variable definition or a non-inline function definition. Otherwise, the compiler will dutifully create multiple copies of those functions and variable, and the linker will get confused over which to pick (the fact that they are code-identical is inconsequential, the linker only sees multiple symbols with the same name). For things like classes and inline functions, which can be multiply defined across multiple translations units (cpp files + every thing they #include), the definitions must be identical (otherwise Bad Things™ happen).

A header file should only include declarations (learn to use the extern keyword[attention]), constants and enums, templates, class definitions (including struct, union...) and inline functions.

##### Share on other sites
People don't read documentation or try to understand processes. Fortunately, technology is gradually obviating the need for such diligence.

In C (and therefore in C++), all entities must be declared before they are referenced, and then defined only once for a given scope. Traditionally, declarations are placed in header files (.h, .hpp, .hxx) while definitions are placed in implementation files (.c, .cpp, .cxx, .cc). If an entity is defined twice within a given scope, you get a redefinition error, which is shade13's problem. We conventionally don't include implementation files because they get compiled on their own if they're part of the project workspace.

It is important to clearly understand the difference between a declaration and a definition. int i; is a definition without initialization; extern int i; is a declaration, and it requires that there still be a definition - and only one definition. For functions, the process is simplified and doesn't require the use of the extern keyword: int function(void); is a declaration; replace the statement terminator (;) with a statement block to yield a definition.

Don't know what a statement block is? Look it up. I'm not going to teach you everything, after all!

##### Share on other sites
Yay, i think i fixed it.
for the sake of anyone else confused, i read that guide miserable recomended, then i made a new header file for helper.cpp, called helper.h, then i put all the 'helper' function declerations into it, it seems to be working, cause now i just include that one wherever i need

--Jake

##### Share on other sites
Petewood, that's some very interesting reading, but next time please read the post; while the topic does rather deceptively suggest a discussion of static vs. dynamic typing, the OP's problem is with linker errors. O_O

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978704

• 21
• 14
• 12
• 10
• 12