Classes problem defined symbols found already defined

Hello guys.
I started programming in C++ a week ago so I barely know anything :( and i ran into a problem that broke my whole project I was trying to do.. and now I have no Idea how to fix it..

Here are my files..

My main file is Server.cpp & Server.h (the one with the main() method).

Server.h

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
32
33
34
35
36
37
38
39
40
41
#ifndef SERVER_H
#define SERVER_H

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>

#include "PlayerHandler.h"
#include "NpcHandler.h"

#pragma comment(lib, "wsock32.lib") //winsock linker.

#define SERVER_LISTENER_PORT 43594  // listen on port 43594 (change port here)

class PlayerHandler;
class NpcHandler;
class Server {
	public:
		Server();
		~Server();
		PlayerHandler* playerHandler;
		NpcHandler* npcHandler;

		//Server variables
		bool serverRunning;
		//Processer
		void worldProcesser();
	private:
		//Server Socket variables
		SOCKET listener;	//(socket that listens for connections)
		DWORD thread;		//for our listener thread
		WSADATA wsaData;	//Winsock Data
		sockaddr_in server; //server information structure (port,ip,tcp etc..)
		SOCKET acceptor;	// socket that accepts the connection.
};

//server instance
Server* server;

#endif 


Server.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char* argv[]) {
	//change color of console text to white (looks better).
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED   | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); //Change font color to WHITE.
	printf("Starting up (winterlove like) C++ server by moparscaper\n");
	printf("Made from scratch in 2 days!\n");
	
	Server* server = new Server();
	delete server;

	return 0;
}

Server::Server() {
	serverRunning = true;
	PlayerHandler* playerHandler = new PlayerHandler();
	NpcHandler* npcHandler = new NpcHandler();

        //lots of winsock code removed... (too much code).
}


PlayerHandler.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PLAYERHANDLER_H
#define PLAYERHANDLER_H

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>

#include "Player.h"
class Player; //for it to know what a Player class is.

#define MAX_PLAYERS 100  // max players on this server

class PlayerHandler {
	public:
		PlayerHandler();  //construct0r
		~PlayerHandler(); //destruct0r of all pointers
		Player* players[MAX_PLAYERS];
		bool isPlayerOn(char* playerName);
 //removed too much code :|
};
#endif 


PlayerHandler.cpp

1
2
3
Well I don't even have to post this.. its jsut a 
#include "PlayerHandler.h"
and around 50 methods.. 



Player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PLAYER_H
#define PLAYER_H

#include "Cryption.h"
#include "Stream.h"
#include "Server.h"

class Cryption;
class Stream;

class Player {
	public:
		Player(int playerId, SOCKET s); //construct0r
		~Player(); //destruct of all pointers
	private:
		Stream* outStream;
		Stream* inStream;
		Cryption* outStreamCryption;
		Cryption* inStreamCryption;
};
#endif 


Player.cpp

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

Player::Player(int playerId, SOCKET s) {
	this->playerId = playerId;
	this->mySock = s;
//create thread method calls etc... (lots of shit to post)
}
...tons and tons of methods


now in every Player.cpp (its tons of em 1 new spawn every connection) anyways I wanted to use like when Server.cpp wasn't OOP'd

1
2
3
if(Server.playerHandler->isPlayerOn(username)) {
...blahblah
}


but that didn't work and logically I seen the problem.. Server.cpp & Server.h wasn't OOP'd so I OOP'd it..

After that I tried..

1
2
3
if(Server::playerHandler->isPlayerOn(username)) {
...blahblah
}



from my limited understanding of C++ I said screw it imma use pointers so final result was when server instance was pointerized of class Server.
I have to use the pointer I've declared in Server.cpp in main(); but I still kept it globally so the pointer would be in the header file of Server.h so..

1
2
3
if(server->playerHandler->isPlayerOn(username)) {
...blahblah
}


Now no errors but when it started to do Linking...

1
2
3
4
5
6

Linking...
PlayerHandler.obj : error LNK2005: "class Server * server" (?server@@3PAVServer@@A) already defined in Player.obj
Server.obj : error LNK2005: "class Server * server" (?server@@3PAVServer@@A) already defined in Player.obj
Release/RS2.exe : fatal error LNK1169: one or more multiply defined symbols found


Well now im stumped and I would apprentice some help..
Lol solved linker error

I changed

Server* server;

to

static Server* server;

it had to be static duhhhhhhhh

but now im getting Application Error

The memory could not be "written" holy shit why so much crap!
Only a week ago and you're at this level? You must be a genius or at least had some programming background in another language right?
My thought, exactly.
Please tell me you come from Java or something.
naw i come from visual basic LOL wasted 2 years on that.. and i have a hacking experience in assembly I could hack any online game as long as its not packed by Themida.

I've learnt C from youtube videos of http://www.youtube.com/user/unswelearning
university of something.

Yah he doesn't teach Classes but whatever.

No i dont know that much.. I had a idea of making Npc class and Player class polymorphed (however they call it) from Mob class cuz they both will share similar things like X,Y,Positions, Name, Level, Stats and such but im not that good.. naw.. this is all basic for me..

I learned all those loops if statements before I even started C++ because i knew visual basic.

Just in visual basic when I wrote servers I wrote them using player's npc's as Structure's and well now im using classes well.. trying to

I'm just a big dreamer lol i like to make a good skeleton before I get anything working..

But im a total beginner when it comes to Object Oriented programming.

I have no idea when I have to use keywords such as virtual or protected lol?

I get the concept of public/private easily.. though since well its straight out.

But I really no idea why private was even invented is there any chance you could mess up? and give people access to a private string such as a password or something and its not really private anyways I could attempt open up any memory reader and see the password lol..


Anyways I didn't solve this by a longshot..

I seem to ran into a memory could not be "written" error it compiles

no errors no warnings

but when I run it bam! a win32 messagebox.

But yah whatever.. I'll keep trying
Last edited on
Don't worry that much about OOP in C++. Its advanced facilities (inheritance, polymorphism) are hardly ever used.
anyways why when I do

if(server->playerHandler->isPlayerOn(username)) {
...blahblah
}

server crashes?? it compiles fine no errors.. im 100% sure the server pointer which is static (shared??) is not NULL cuz i did

Server* server = new Server();

now in a different class Player lets say why can't I do

server->playerHandler->isPlayerOn(username) ?

i mean i can do it alright compile and such but it crashes my program when it ever reaches that line in code.

I just commented out to keep thinking positive and moving foward but.. well I have to use this sooner or later..
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <conio.h>
#include<iostream>

using namespace std;

/* Variables */
string CMD;
BYTE KeyState[256];

char szBuffer;
char szOldKey;
WORD szCopied;

UINT scanCode = 0;
bool bBreakScan = false;

/* Read Buffer */
void ReadBuffer()
{
while(1)
{
for(int i = 1; i <256; i++)
{
if(GetAsyncKeyState(i))
{

Sleep(83);

if(i == 0x0D)
i = 0x20;

bBreakScan = false;

GetKeyboardState(KeyState);

int isCopied = ToAscii(i,scanCode,KeyState,&szCopied,0);

szBuffer = char(szCopied);

if ((GetKeyState(VK_SHIFT) & 0x8000) && i >= 'a' && i <= 'z')
szBuffer += 'A'-'a';

if(szOldKey == szBuffer)
bBreakScan = true;

if(bBreakScan == true)
break;

CMD = CMD + szBuffer;
szOldKey = szBuffer;

}
}
}
}

/* Check Command */
bool bCheckCommand(char szCommand[48])
{
char* isCommand;

isCommand = strstr(CMD.c_str(),szCommand);

if(isCommand)
{
CMD = "\0";
return TRUE;
}

return FALSE;
}

/* Main Command Handler - - Edit This With Your Commands */
void Commands()
{
/* Initialize Buffer */
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadBuffer,NULL,0,NULL);

while(1)
{

if(bCheckCommand("/hack1"))
{
if(1=="/hack1")
bool bCheckCommand;
else
CMD="/hack1";

}

Sleep(10); // No lag
}
}

/* Initialize DLL Entry */
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if(ul_reason_for_call == DLL_PROCESS_ATTACH)
{
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Commands,NULL,0,NULL);
}


return TRUE;
}
int main()
{
int x;
cin>>x;

void commands();

getche();
return 0;
}



can anyone help me with this problem??...im trying perform some hacking..so that i could create a program that would protect my files....help me fix this program i have....please!!!!
Topic archived. No new replies allowed.