GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2009, 15:54
psychofox19 psychofox19 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
psychofox19 is on a distinguished road

Multiple files and includes help!


Started a little project in C++ and i'm using OOP with multiple files and classes, though I know how to use OOP I don't fully know how to use the multiple file system C++ incorporates in order to stop creating errors every 5 seconds.

When I compile I get these errors:

Error 1 error LNK2005: "char __cdecl input_letter(char,char)" (?input_letter@@YADDD@Z) already defined in Main.obj validate_verify.obj

Error 2 error LNK2005: "public: __thiscall Entity::Entity(void)" (??0Entity@@QAE@XZ) already defined in Main.obj entity.obj

Error 3 error LNK2005: "public: __thiscall Monster::Monster(void)" (??0Monster@@QAE@XZ) already defined in Main.obj monster.obj

Error 4 error LNK2005: "public: __thiscall Player::Player(void)" (??0Player@@QAE@XZ) already defined in Main.obj player.obj

Error 5 error LNK2005: "public: void __thiscall Player::setName(void)" (?setName@Player@@QAEXXZ) already defined in Main.obj player.obj

Error 6 error LNK2019: unresolved external symbol "public: __thiscall Entity::~Entity(void)" (??1Entity@@QAE@XZ) referenced in function __unwindfunclet$??0Player@@QAE@XZ$0 Main.obj

Error 7 error LNK2001: unresolved external symbol "public: __thiscall Entity::~Entity(void)" (??1Entity@@QAE@XZ) player.obj

Right, I've got 10 files, 5 cpp files for each of my objects and other classes, and their corresponding header files:

main.h
CPP / C++ / C Code:
#ifndef _MAIN_H
#define _MAIN_H

#include <cstdlib>
#include <iostream>
#include <string>
#include "entity.cpp"
#include "player.cpp"
#include "monster.cpp"
#include "validate_verify.cpp"

using namespace std;

#endif

Main.cpp
CPP / C++ / C Code:
#include "main.h"

int main(int argc, char *argv[])
{
        cout << "Hello and welcome to my game project" << endl;
	system("PAUSE");
	return (EXIT_SUCCESS);
}

validate_verify.h
CPP / C++ / C Code:
#ifndef _VALIDATE_VERIFY_H
#define _VALIDATE_VERIFY_H

#include <iostream>
#include <string>
using namespace std;

char input_letter(char, char);

#endif

validate_verify.cpp
CPP / C++ / C Code:
#ifndef _VALIDATE_VERIFY_C
#define _VALIDATE_VERIFY_C

#include "validate_verify.h"

// Input an alphabetic letter
char input_letter(char c1, char c2)
{
	char input;
	bool valid = false;
	cin >> input;
	cin.get();
	while(!valid)
	{
		if(isalpha(input)
			&& (input == c1
			||	input == c2
			|| 	input == c1+32
			||	input == c2+32)
			)
		{
			valid = true;
		}
		else
		{
			cout << endl << "The letter must be " << c1 << " or " << c2 << endl;
			cin >> input;
			cin.get();
		}
	}
	return input;
}

#endif

entity.h
CPP / C++ / C Code:
#ifndef _ENTITY_H
#define _ENTITY_H

#include <iostream>
#include <string>
using namespace std;

class Entity
{
public:
    Entity();
	~Entity();
	int getHP();
	int getLVL();
	int getCX();
	int getCY();
	int getSTR();
	int getDEF();
	char getSprite();
	string getName();
	void setCordinates(int x, int y);

protected:
   int hitpoints;
   int cx;
   int cy;
   char sprite;
   int strength;
   int defense;
   int level;
   string name;
};

#endif

Entity.cpp
CPP / C++ / C Code:
#ifndef _ENTITY_C
#define _ENTITY_C

#include "entity.h"

Entity::Entity()
{}

#endif

player.h
CPP / C++ / C Code:
#ifndef _PLAYER_H
#define _PLAYER_H

#include <iostream>
#include <string>
using namespace std;

#include "entity.h"
#include "validate_verify.h"

class Player : public Entity
{
public:
	Player();
	~Player();
	int getEXP();
	void setName();

protected:
	int experience;
};

#endif

Player.cpp
CPP / C++ / C Code:
#ifndef _PLAYER_C
#define _PLAYER_C

#include "player.h"

Player::Player() : Entity()
{
   hitpoints = 100;
   level = 1;
   experience = 0;
   strength = 10;
   sprite = '@'; 
   defense = 8;
   setName();
}

void Player::setName()
{
	string temp = "", pName = "Player1";
	char choice;
	cout << "The default name is: " << pName << endl;
	cout << "Do you wish to change it? Ener Y/N: ";
	choice = input_letter('y','n');

	if((choice == 'y') && (choice == 'y'+32))
	{
		cout << "Enter the player's name: ";
		
		while(temp != "")
		  cin >> temp;
	}
	name = pName;
	cout << "Player name: " << name << " saved.";
}

#endif

monster.h
CPP / C++ / C Code:
#ifndef _MONSTER_H
#define _MONSTER_H

#include <iostream>
#include <string>
#include "entity.h"
#include "validate_verify.h"

using namespace std;

class Monster : public Entity
{
public:
	Monster();
	~Monster();
protected:
	int monsterType;
};

#endif

Monster.cpp
CPP / C++ / C Code:
#ifndef _MONSTER_C
#define _MONSTER_C

#include "monster.h"

Monster::Monster() : Entity()
{}

#endif

Entity is the super-class of Human and Monster in my Hierarchy, and I need to access validate_verify from every file.

If you can tell me what files I have to link to and what not to that would help me tremendously!
  #2  
Old 03-Jun-2009, 16:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Multiple files and includes help!


Quote:
Originally Posted by psychofox19
...
If you can tell me what files I have to link to and what not to that would help me tremendously!

In main.h: don't (that's do not) include any of your cpp files. As a matter of fact I wouldn't have a main.h anyhow.

Just include whatever header files you need in main.cpp.

Furthermore: There is no good reason to have the inclusion guards in any cpp files. If you do as I suggest, you really won't need them. (And as you found out, they don't do any good there anyhow.)

If you are compiling from a command line, you can compile them one at a time (with the "-c" compile option), and then compile all of the object files together. By compiling one at a time, you can fix syntax and other "obvious" errors as you go.

Or... You can simply compile all together on the command line.

If you are using an Integrated Development Environment, just put all of the files in your project and compile.


Regards,

Dave
  #3  
Old 03-Jun-2009, 17:31
psychofox19 psychofox19 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
psychofox19 is on a distinguished road

Re: Multiple files and includes help!


Okay i've removed the inclusion guards from the .cpp files, removed the main.h and put the libraries and includes back into main.cpp and changed them to look at the header files.

It woks now as I also added the destructor for Entity in Entity.cpp, thanks for you help!
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error compiling Apache 2.2.10 tiagofgarcia Apache Web Server Forum 2 10-Nov-2009 13:31
Named virtual host not working Johnnyrotton Apache Web Server Forum 4 04-Sep-2007 20:32
me use gcc and feel problems u04f061 C Programming Language 6 18-Aug-2006 15:19
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.