kutuup 25 Junior Poster in Training

Can we see the client.h and client.cpp code?

Here you go:

Client.h:

//===================================
//==== Assignment 3 =================
//==== Damien Parsons - 20830730 ====
//==== Date - 25/03/2011 ============
//==== Client.h =====================
//===================================

#pragma once
#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include "iostream"
#include "windows.h"
#include "Comms.h"

//======================================================
class ClientConnectException: public exception {
public:
  virtual const char* what() const throw() {
    return "Client: connect() - Failed to connect.";
  }
};
//======================================================
class ClientSocketException: public exception {
public:
  virtual const char* what() const throw() {
    return "Error at socket()";
  }
};
//======================================================
class ClientException: public exception {
public:
  virtual const char* what() const throw() {
    return "Client: error %ld. ";
  }
};
//======================================================
class Client:
		public Comms
{
public:
	Client(void);
	~Client(void);
	char Enter();
	int Connect();

	char receiveBuffer[200];

	SOCKET clientSocket;
	
	ClientConnectException noConnection;
	ClientSocketException noSocket;
	ClientException cException;
};
//======================================================

and client.cpp:

//===================================
//==== Assignment 3 =================
//==== Damien Parsons - 20830730 ====
//==== Date - 25/03/2011 ============
//==== Client.cpp ===================
//===================================

#include "StdAfx.h"
#include "Client.h"
//*************************************************
Client::Client(void){
	char receiveBuffer[200] = "";
}
//*************************************************
Client::~Client(void){
}
//*************************************************
int Client::Connect() throw(ClientConnectException,ClientSocketException){
	
	cout<<"\n\n"<<endl;
	cout<<"\t\t=================="<<endl;
	cout<<"\t\t------CLIENT------"<<endl;
	cout<<"\t\t=================="<<endl;
	cout<<"\n\n"<<endl;

	this->createMySocket();

	clientSocket = INVALID_SOCKET;
	clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (clientSocket== INVALID_SOCKET){
		throw noSocket;
		cout <<  WSAGetLastError() << endl;
		WSACleanup();
		return 0;
	}
	else{
		cout << "socket() is OK!" << endl;
	}
	sockaddr_in clientService;
	clientService.sin_family = AF_INET;
	clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
	clientService.sin_port = htons(port);
	
	if (connect(clientSocket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR) {
		throw noConnection;
        WSACleanup();
        return 0;
	}
	else{
       cout << "Client: connect() is OK." << endl;
       cout << …
kutuup 25 Junior Poster in Training

It appears linker is not seeing the definitions for Client. Is Client.cpp compiled and linked with application?

Yes as far as I can see it is.

kutuup 25 Junior Poster in Training

Hello,

I'm working on a simple winsock2 messaging program with a client and a server.

The server, which uses the same code as the client, save for some preprocessor directives, compiles fine, but the client throws up 3 unresovled external symbol errors:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Client::~Client(void)" (??1Client@@QAE@XZ) referenced in function _wmain Assignment 3 AIP 2 Client.obj

Error 2 error LNK2019: unresolved external symbol "public: int __thiscall Client::Connect(void)" (?Connect@Client@@QAEHXZ) referenced in function _wmain Assignment 3 AIP 2 Client.obj

Error 3 error LNK2019: unresolved external symbol "public: __thiscall Client::Client(void)" (??0Client@@QAE@XZ) referenced in function _wmain Assignment 3 AIP 2 Client.obj


I have linked the winsock2 library in both of the projects, but the client still throws up the errors. (Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies->ws2_32.lib).

Any ideas as to why this might be?

Here is the code for the main function of the client:

#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include "windows.h"
#include "Client.h"
#include "Comms.h"
#include "Server.h"
#include <exception>
#include "Server.h"


#define CLIENT
//#define SERVER

char mutexName[] = "MUTEX1";

//*******************************************************
int _tmain(int argc, _TCHAR* argv[]){

#ifdef CLIENT

	try{
		Client myClient;
		myClient.Connect();
	}

	catch( NoDllException& e){
		cout << "Error 1 : " << e.what() << endl;
	}
	catch( ClientConnectException& e){
		cout << "Error 2 : " << e.what() << endl;
	}
	catch( ClientSocketException& e){
		cout << "Error 3 : " << e.what() << endl;
	}
	catch( ClientException& e){
		cout << "Error 4 : " << e.what() << …
kutuup 25 Junior Poster in Training

An unresolved external error is typically telling you that you've declared but not defined a function. In this case, the constructor for your Tree class. I notice that your destructor isn't implemented either, unless you're defining them in a separate file.

Yeah, turns out the problem was I was declaring them in a .cpp file, which apparently is not allowed for template classes.

Thanks for your help!

kutuup 25 Junior Poster in Training

I'm trying to implement a template class called "Tree", here is the code for the main function:

#include "Stack.h"
#include <iostream>
#include <time.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    
	int gameObjects = 0;
	Tree<Player>* PlayerTree = new Tree<Player>;
	Player** players;
	players = new Player* [5];

	Player* p1 = new Player(0);
	players[0] = p1;
	Player* p2 = new Player(2);
	players[1] = p2;
	Player* p3 = new Player(4);
	players[2] = p3;
	Player* p4 = new Player(1);
	players[3] = p4;
	Player* p5 = new Player(3);
	players[4] = p5;

	PlayerTree->Insert(players[0]);
	PlayerTree->Insert(players[3]);
	PlayerTree->Insert(players[2]);
	PlayerTree->Insert(players[4]);
	PlayerTree->Insert(players[1]);


	players[0]->weapons->Insert(new Weapon(8,5));
	players[0]->weapons->Find(5)->magazine->Push(new Round(20));
	players[0]->weapons->Find(5)->magazine->Push(new Round(21));
	players[0]->weapons->Find(5)->magazine->Push(new Round(22));
	players[0]->weapons->Find(5)->magazine->Push(new Round(23));
	players[0]->weapons->Find(5)->magazine->Push(new Round(24));
	players[1]->weapons->Insert(new Weapon(8,6));
	players[1]->weapons->Find(6)->magazine->Push(new Round(25));
	players[1]->weapons->Find(6)->magazine->Push(new Round(26));
	players[1]->weapons->Find(6)->magazine->Push(new Round(27));
	players[1]->weapons->Find(6)->magazine->Push(new Round(28));
	players[1]->weapons->Find(6)->magazine->Push(new Round(29));
	players[1]->weapons->Insert(new Weapon(8,7));
	players[1]->weapons->Find(7)->magazine->Push(new Round(30));
	players[1]->weapons->Find(7)->magazine->Push(new Round(31));
	players[1]->weapons->Find(7)->magazine->Push(new Round(32));
	players[1]->weapons->Find(7)->magazine->Push(new Round(33));
	players[2]->weapons->Insert(new Weapon(8,8));
	players[2]->weapons->Find(8)->magazine->Push(new Round(34));
	players[2]->weapons->Find(8)->magazine->Push(new Round(35));
	players[2]->weapons->Find(8)->magazine->Push(new Round(36));
	players[2]->weapons->Find(8)->magazine->Push(new Round(37));
	players[2]->weapons->Find(8)->magazine->Push(new Round(38));
	players[2]->weapons->Insert(new Weapon(8,9));
	players[2]->weapons->Find(9)->magazine->Push(new Round(39));
	players[2]->weapons->Find(9)->magazine->Push(new Round(40));
	players[2]->weapons->Find(9)->magazine->Push(new Round(41));
	players[2]->weapons->Find(9)->magazine->Push(new Round(42));
	players[2]->weapons->Insert(new Weapon(8,10));
	players[2]->weapons->Find(10)->magazine->Push(new Round(43));
	players[2]->weapons->Find(10)->magazine->Push(new Round(44));
	players[2]->weapons->Find(10)->magazine->Push(new Round(45));
	players[3]->weapons->Insert(new Weapon(8,11));
	players[3]->weapons->Find(11)->magazine->Push(new Round(46));
	players[3]->weapons->Find(11)->magazine->Push(new Round(47));
	players[3]->weapons->Find(11)->magazine->Push(new Round(48));
	players[3]->weapons->Find(11)->magazine->Push(new Round(49));
	players[3]->weapons->Find(11)->magazine->Push(new Round(50));
	players[3]->weapons->Insert(new Weapon(8,12));
	players[3]->weapons->Find(12)->magazine->Push(new Round(51));
	players[3]->weapons->Find(12)->magazine->Push(new Round(52));
	players[3]->weapons->Find(12)->magazine->Push(new Round(53));
	players[3]->weapons->Find(12)->magazine->Push(new Round(54));
	players[3]->weapons->Insert(new Weapon(8,13));
	players[3]->weapons->Find(13)->magazine->Push(new Round(55));
	players[3]->weapons->Find(13)->magazine->Push(new Round(56));
	players[3]->weapons->Find(13)->magazine->Push(new Round(57));
	players[3]->weapons->Insert(new Weapon(8,14));
	players[3]->weapons->Find(14)->magazine->Push(new Round(58));
	players[3]->weapons->Find(14)->magazine->Push(new Round(59));
	players[4]->weapons->Insert(new Weapon(8,15));
	players[4]->weapons->Find(15)->magazine->Push(new Round(60));
	players[4]->weapons->Find(15)->magazine->Push(new Round(61));
	players[4]->weapons->Find(15)->magazine->Push(new Round(62));
	players[4]->weapons->Find(15)->magazine->Push(new Round(63));
	players[4]->weapons->Find(15)->magazine->Push(new Round(64));
	players[4]->weapons->Insert(new Weapon(8,16));
	players[4]->weapons->Find(16)->magazine->Push(new Round(65));
	players[4]->weapons->Find(16)->magazine->Push(new Round(66));
	players[4]->weapons->Find(16)->magazine->Push(new Round(67));
	players[4]->weapons->Find(16)->magazine->Push(new Round(68));
	players[4]->weapons->Insert(new Weapon(8,17));
	players[4]->weapons->Find(17)->magazine->Push(new Round(69));
	players[4]->weapons->Find(17)->magazine->Push(new Round(70));
	players[4]->weapons->Find(17)->magazine->Push(new Round(71));
	players[4]->weapons->Insert(new Weapon(8,18));
	players[4]->weapons->Find(18)->magazine->Push(new Round(72));
	players[4]->weapons->Find(18)->magazine->Push(new Round(73));
	players[4]->weapons->Insert(new Weapon(8,19));
	players[4]->weapons->Find(19)->magazine->Push(new Round(74));


	
	

    PlayerTree->DisplayInOrder(PlayerTree->root);
	PlayerTree->Find(players[0]);


    delete PlayerTree;




	system("PAUSE");
	return 0;
}

And the code for the Tree class:


       
kutuup 25 Junior Poster in Training

Maybe you're comparing a GameObject with a Player, but I don't think so...

Have you deferenced BOTH pointers? (current AND insertedPlayer)

Note that you put overrided operators PRIVATE, so this is hidden... Try to put them PUBLIC.

You nailed it, I needed to dereference both operands.

I think it's because the overloaded operator is expecting a dereferenced right operand.

I don't think it matters if the overloaded operators are private since they are technically a member function, but I don't know lol

Thanks!

James

kutuup 25 Junior Poster in Training

Can you show your implementation of the overloaded operators you mentioned?

I already did but here it is again:

#pragma once
#include "GameObject.h"
#include "LinkedList.h"


class Player: public GameObject
{

private:

	bool operator!=(const Player& ent)
	{
		if(this->id != ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}

	}
	bool operator<(const Player& ent)
	{
		if(this->id < ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	

public:
    LinkedList* weapons;

	Player* leftChild;
	Player* rightChild; 

	Player(int id);
	~Player(void);

	void Display()
	{
		cout << "Player ID: " << this->id << endl;
	}
};
kutuup 25 Junior Poster in Training

You need to deference you pointers

That throws up a build error:

Error 2 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'Player' (or there is no acceptable conversion) c:\users\james\documents\visual studio 2008\projects\assignment 2 (aip)\assignment 2 (aip)\tree.h 58

kutuup 25 Junior Poster in Training

>if(insertedPlayer < current)
You're comparing pointers, not the "id" data member.

I have overloaded the < and != operator for the Player class, which both operators are pointers to. Does it not work that way?

Here are the overloaded operators in the Player class:

#pragma once
#include "GameObject.h"
#include "LinkedList.h"


class Player: public GameObject
{

private:

	bool operator!=(const Player& ent)
	{
		if(this->id != ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}

	}
	bool operator<(const Player& ent)
	{
		if(this->id < ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	

public:
    LinkedList* weapons;

	Player* leftChild;
	Player* rightChild; 

	Player(int id);
	~Player(void);

	void Display()
	{
		cout << "Player ID: " << this->id << endl;
	}
};
kutuup 25 Junior Poster in Training

I have a Tree class that holds pointers to Player objects.

Here is the code:

#pragma once
#include "Player.h"

class Tree
{

private:
	
public:
	Player* root;
	Tree(void);
	~Tree(void);

	void DisplayInOrder(Player* localRoot)
	{
		        if (localRoot != 0) {
                DisplayInOrder(localRoot->leftChild);
				localRoot->Display();
                DisplayInOrder(localRoot->rightChild);
				}
	}

	Player* Find(const Player* key)
	{
		            Player * current = root;
            while (current != key) {
				if (key < current)
                    current = current->leftChild;
                else
                    current = current->rightChild;
                if (current == 0)
				{
				    cout << endl << "Player Not Found!" << endl << endl;
                    return 0;

				}
            }
			cout << endl << endl << "Found Player! ";
			current->Display();
			cout << endl << endl;
            return current;
	}

	void Insert(Player* insertedPlayer)
	{

		if(root == 0)
	    {
		     root = insertedPlayer;
	     }
	     else
	     {
		     Player * current = root;
		     Player * parent;
		     while(true)
		     {
			     parent = current;
				 if(insertedPlayer < current)
			      {
				     current = current->leftChild;
					 if(current == 0)
					     {
						     parent->leftChild = insertedPlayer;
						     return;
					     }
				  }
			      else
			      {
			          current = current->rightChild;
				      if(current == 0)
				      {
							     parent->rightChild = insertedPlayer;
							     return;
				      }
			
			       }
		     
		     }
	     }

	}

};

The problem is, if I insert 5 pointers to Player objects using the Insert function, then call the displayInOrder function, passing it the root as the argument, it outputs the objects in the same order I put them in, rather than in order of their "id" data member that the Insert function takes into account using overloaded operators < and != for the Player class.

I have a working example of a Tree structure that I have literally …

kutuup 25 Junior Poster in Training

Post the code here?? Like the entire thing.. or the .cpp file or both.

I have now solved the problem, when I was adding pointers to objects to the various data structures, I was actually passing them a pointer to the SAME object!

What I had done was similar to this:

Class* name = new Class;

Structure->insert(name);
Structure->insert(name);
Structure->insert(name);

Thinking that each time I added it to the structure I was passing it a new instance of Class.

Turns out I was wrong, and when I deleted one, I was deleting the same pointer from every location in every structure.

Rookie mistake I suppose, you live and learn!

Narue commented: Grats for finding the problem. +25
kutuup 25 Junior Poster in Training

It means buffer overflow. Check your pointers and verify your indexes.

I've had a check through and can't seem to find the problem.

All the pointers appear to be correct and I'm not sure how to verify indexes.

Can you offer any suggestions on action to take?

kutuup 25 Junior Poster in Training

It means buffer overflow. Check your pointers and verify your indexes.

Ah ok that makes sense lol I'll have a check and post back.

kutuup 25 Junior Poster in Training

You wrote past the end (or beginning) of your dynamic memory and corrupted any bookkeeping data required by the heap manager.

Yeah... I don't know what that means. Any ideas on how I could not do that? lol

kutuup 25 Junior Poster in Training

Hi,

I'm trying to run a program I have coded that has the following setup:

There is a tree containing "player" objects.

Each "player" object has a linked list containing "weapon" objects.

Each "weapon" object has a stack containing "round" objects.

I need to set it up so that of you delete the tree object, all the players within the tree are deleted, which in turn deletes all the weapons in each player's linked list, which in turn deletes all the rounds in each weapons stack in a cascade effect.

The destructor for the tree looks like this:

Tree::~Tree(void)
{
	cout << "Tree Deleted" << endl << endl;
	delete this->root;
}

The destructor for the player class:

Player::~Player(void)
{
	cout << "Player Deleted" << endl << endl;

	delete this->weapons;

	delete this->leftChild;
	delete this->rightChild;
}

The destructor for the linked list class:

LinkedList::~LinkedList(void)
{
	while(!isEmpty())
	{
		delete Delete();
	}

}

The destructor for the weapon class:

Weapon::~Weapon(void)
{
	cout << "Weapon Deleted" << endl << endl;
	delete this->magazine;
}

The destructor for the stack class:

Stack::~Stack(void)
{
	while(this->top != -1)
	{
		delete this->Pop();
	}
}

The destructor for the round class:

Round::~Round(void)
{
	cout << "Round Deleted" << endl << endl;
}

When I run the program, the cascade begins and the output shows:

Tree Deleted

Player Deleted

Weapon Deleted

Round Deleted

Round Deleted

And then BOOM, this error message pops up and …

kutuup 25 Junior Poster in Training

The code for the tree was provided by my tutor and look like this:

#pragma once
#include "Player.h"

class Tree
{

private:
	
public:
	Player* root;
	Tree(void);
	~Tree(void);

	void DisplayInOrder(Player* localRoot)
	{
		        if (localRoot != 0) {
                DisplayInOrder(localRoot->leftChild);
				localRoot->Display();
                DisplayInOrder(localRoot->rightChild);
				}
	}

	Player* Find(const Player* key)
	{
		            Player * current = root;
            while (current != key) {
				if (key < current)
                    current = current->leftChild;
                else
                    current = current->rightChild;
                if (current == 0)
				{
				    cout << endl << "Player Not Found!" << endl << endl;
                    return 0;

				}
            }
			cout << endl << endl << "Found Player! ";
			current->Display();
			cout << endl << endl;
            return current;
	}

	void Insert(Player* insertedPlayer)
	{

		if(root == 0)
	    {
		     root = insertedPlayer;
	     }
	     else
	     {
		     Player * current = root;
		     Player * parent;
		     while(true)
		     {
			     parent = current;
				 if(insertedPlayer < current)
			      {
				     current = current->leftChild;
					 if(current == 0)
					     {
						     parent->leftChild = insertedPlayer;
						     return;
					     }
			     }
			      else
			     {
			       current = current->rightChild;
				   if(current == 0)
				     {
							     parent->rightChild = insertedPlayer;
							     return;
				     }
			
			     }
		     }
	     }

	}

};

I have overloaded the < and != operators for the Player class and set them to compare instanced based on their "id" data member which is randomly generated in the constructor.

I can't see why they output in the order they were put in rather than in order of id since the displayInOrder function seems to be fine.

kutuup 25 Junior Poster in Training

Ah, that's sorted it! Thanks!

But now the display doesn't show the instances in order like it did before.

If I change the constructor to accept an integer then assign the id data member that number then pass a different number each instance, they display in order even if I input them in a random order.

for example

Player* p1 = new Player(0);
	players[0] = p1;
	Player* p2 = new Player(3);
	players[1] = p2;
	Player* p3 = new Player(1);
	players[2] = p3;
	Player* p4 = new Player(7);
	players[3] = p4;
	Player* p5 = new Player(9);
	players[4] = p5;

(sorry if i messed up the code tags.)

If i do that they are put in the right order when they output.

Why might this be?

kutuup 25 Junior Poster in Training

Hey all

I have a dynamic array of pointers to instances of a class ("Player").

The constructor for the Player class looks like this:

Player::Player(void)
{
    this->leftChild = 0;
    this->rightChild = 0;

    srand (time(NULL));
    this->id = rand() % 1000;


}

The dynamic array of pointers looks like this:

Tree* PlayerTree = new Tree; //declaring a tree here for use later
Player** players;
players = new Player* [5];

Player* p1 = new Player(1);
players[0] = p1;
Player* p2 = new Player(2);
players[1] = p2;
Player* p3 = new Player(3);
players[2] = p3;
Player* p4 = new Player(4);
players[3] = p4;
Player* p5 = new Player(5);
players[4] = p5;

Then I insert the instances of Player from the array into the tree I declared above using a member function of my Tree class:

PlayerTree->Insert(players[1]);
PlayerTree->Insert(players[0]);
PlayerTree->Insert(players[3]);
PlayerTree->Insert(players[2]);
PlayerTree->Insert(players[4]);

I then call a function of Tree which displays the "id" data member of each node in order (the "id" should be random based on the constructor).

However, I get this:

Player ID: 500 (random number)
Player ID: 500
Player ID: 500
Player ID: 500
Player ID: 500 (all the SAME random number!)

Why might this be? Surely the constructor is called each time a new instance is created, and in the constructor I re-seed the rand() function so it should re-seed each time it is called.

I should point out that the Player class is a child of a class called GameObject whose constructor is …