Hey, I am making a small network game and I suddenly got some problems with my player class, specially when trying to call the getName function and using the second constructor I get this error:

1>------ Build started: Project: RPG, Configuration: Release Win32 ------
2>------ Build started: Project: Server, Configuration: Release Win32 ------
1>Compiling...
2>Compiling...
1>playerHandler.cpp
2>playerHandler.cpp
1>player.cpp
2>main.cpp
1>main.cpp
2>Linking...
2>playerHandler.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall player::getName(void)" (?getName@player@@QAEAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
2>playerHandler.obj : error LNK2001: unresolved external symbol "public: __thiscall player::player(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float,float)" (??0player@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@MM@Z)
2>C:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\Release\Server.exe : fatal error LNK1120: 2 unresolved externals
2>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\Server\Release\BuildLog.htm"
2>Server - 3 error(s), 0 warning(s)
1>Linking...
1>Generating code
1>Finished generating code
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
1>RPG - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Code for player class
player.h

#ifndef __PLAYER_H_
#define __PLAYER_H_

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{

private:
	std::string charName;
	float health;
	float mana;
	float X;
	float Y;
	int level;
	float expirience;
	bool isAlive;
	sf::Sprite model;

public:
	player(std::string name);
	player(std::string name, float x, float y);
	sf::Sprite & getModel();
	void move(const sf::Input & keyboard, sf::RenderWindow & window1);
	float & getX();
	float & getY();
	float & getHealth();
	float & getMana();
	bool & getStatus();
	int & getLevel();
	float & getExpirience();
	std::string & getName();

	friend sf::Packet & operator << (sf::Packet & Packet, player& p);
	friend sf::Packet & operator >> (sf::Packet & Packet, player& p);
};

#endif

sf::Packet & operator << (sf::Packet & Packet, player& p);
sf::Packet & operator >> (sf::Packet & Packet, player& p);

Player.cpp

#include "player.h"


player::player(std::string name):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), charName(name)
{
	model.SetColor(sf::Color(0, 123, 123, 128));
	model.SetPosition(X,Y);
	model.SetScaleX(40.f);
	model.SetScaleY(40.f);
}

player::player(std::string name, float x, float y):
health(100), mana(100), X(x), Y(y), level(1), expirience(0), isAlive(true), charName(name)
{
	model.SetColor(sf::Color(0, 123, 123, 128));
	model.SetPosition(X,Y);
	model.SetScaleX(40.f);
	model.SetScaleY(40.f);
}

sf::Sprite & player::getModel()
{
	return model;
}


void player::move(const sf::Input & keyboard, sf::RenderWindow & window1)
{
	if (keyboard.IsKeyDown(sf::Key::Down))
		Y += 0.1f;

	else if(keyboard.IsKeyDown(sf::Key::Up))
		Y -= 0.1f;

	else if(keyboard.IsKeyDown(sf::Key::Right))
		X += 0.1f;

	else if(keyboard.IsKeyDown(sf::Key::Left))
		X -= 0.1f;

	model.SetPosition(X,Y);
	window1.Draw(model);
}

float & player::getExpirience()
{
	return expirience;
}

float & player::getX()
{
	return X;
}

float & player::getY()
{
	return Y;
}

float & player::getHealth()
{
	return health;
}

float & player::getMana()
{
	return mana;
}

bool & player::getStatus()
{
	return isAlive;
}

int & player::getLevel()
{
	return level;
}

std::string & player::getName()
{
	return charName;
}


// Pakettien omat ylikuormitetut operaattorit


sf::Packet & operator << (sf::Packet & Packet, player& p)
{
	return Packet << p.X << p.Y << p.charName;
	//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
	return Packet >> p.X >> p.Y >> p.charName;
}

Other for the server and other for the client, both has their own main files and playerHandlers which differ. This problem comes if I try to work with the string charName

Sure
Client playerhandler.h

#ifndef __PLAYERHANDLER_H_
#define __PLAYERHANDLER_H_

#include <iostream>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <vector>
#include "player.h"

class playerHandler : public sf::Thread
{
private:
	std::vector<player> allPlayers;
	player & myPlayer;
	const sf::Input & keyboard;
	sf::RenderWindow & window;

	virtual void Run();


public:
	playerHandler(player & p, sf::RenderWindow & window1);
	void handlePlayer();
	void updateMyPlayer();
};

#endif

client playerhandler.cpp

#include "playerHandler.h"

playerHandler::playerHandler(player & p, sf::RenderWindow & window1)
:myPlayer(p), window(window1), keyboard(window1.GetInput())
{

}

void playerHandler::updateMyPlayer()
{
	sf::IPAddress serverAddress("127.0.0.1");

	sf::SocketTCP socket;

	socket.Connect(1000, serverAddress);

	bool connected = true; // Testing only, will add checkings later..

	while(connected)
	{
		sf::Packet packet;
		packet << myPlayer;
		connected = (socket.Send(packet) == sf::Socket::Done);
	}

	socket.Close();
}

void playerHandler::handlePlayer()
{
	myPlayer.move(keyboard, window);
}

void playerHandler::Run()
{
	this->updateMyPlayer();
}

Server playerhandler.h

#ifndef __PLAYERHANDLER_H_
#define __PLAYERHANDLER_H_

#include <iostream>
#include <SFML/Network.hpp>
#include <vector>
#include "../RPG/player.h"

class playerHandler
{
private:
	sf::SelectorTCP selector;
	std::vector<player> players;

public:
	playerHandler();
	void checkForConnections();

};

#endif

server playerhandler.cpp

#include "playerHandler.h"

playerHandler::playerHandler()
{
}

void playerHandler::checkForConnections()
{
	std::cout << "Checking for connections! \n";
	sf::SelectorTCP selector;

	sf::SocketTCP listener;

	if(!listener.Listen(1000))
	{
		std::cout << "Can't listen the port! \n";
	}

	selector.Add(listener);

	while(true)
	{
		unsigned int NbSockets = selector.Wait();

		for(unsigned int i = 0; i < NbSockets; i++)
		{
			sf::SocketTCP socket = selector.GetSocketReady(i);

			if (socket == listener)
			{
				sf::IPAddress address;
				sf::SocketTCP client;
				listener.Accept(client, &address);
				std::cout << "Client connected ! (" << address << ")" << std::endl;

				selector.Add(client);
			}
			else
			{
				sf::Packet packet;
				if(socket.Receive(packet) == sf::Socket::Done)
				{
					float X;
					float Y;
					std::string name;
					packet >> X >> Y >> name;
					player p(name, X, Y);

					std::vector<player>::iterator it = players.begin();

					while(it != players.end())
					{
						if ( it->getName() == name )
						{
							players.erase(it);
							players.push_back(p);
						}
						else
							players.push_back(p);
					}
							


					// std::cout << "Character named: " << name << "X = " << X << " Y = " << Y << "\n";

				}
				else
				{
					selector.Remove(socket);
				}
			}
		}
	}
}

It looks like you include for player.h inn your server playerHandaler is wrong. Why are you using / for the directory switches? Shouldn't it be a \?

Nvm, my friend didn't compile the server so the error isn't a bug... Can't understand have been trying to solve this all day.

Edited 6 Years Ago by Silvershaft: n/a

Would you feel comfortable attaching all of your code so I can try and compile it on my machine? I would also need the SFML/Graphics.hpp and SFML/Network.hpp or a link to get the library.

Well I am having some problems getting it to link with the SFML but I wasn't able to get your error. I changed your include to #include <c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\player.h> . That might help you.

I am that friend Silvershaft mentioned. I just made new project and copied the code and it works now.

This article has been dead for over six months. Start a new discussion instead.