Another problem with C++

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 620 Views - Last Post: 01 July 2011 - 05:55 AM Rate Topic: -----

#1 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Another problem with C++

Posted 30 June 2011 - 01:03 PM

I have another problem. I can't really explain it, but I'll try.
I want the player to be inserted into the Inn and that Town. I think I got the town to work, but whenever I try to do the Inn it comes up with errors.
The part with the problems (I think) is the Inn(&player) part, or the void Inn PLAYER; part.

Town code:
#include "Library.h"

//TOWN

void Inn(PLAYER);

bool Town(PLAYER player)
{
	
	int choice = 0;

	while (choice != 5)
	{
		cout << "You enter the Kokiri Town.\n";
		cout << "You see many children milling around and playing.\n";
		cout << "NAVI: This looks like a good place to buy items!  There's even an Inn where you can stay to replenish your HP and MP.\n";
		cout << "To your right there is an Inn.  To your left you see the Armory and the Kokiri \nShop.  Near you is a reflective pool.\n\n";
		cout << "NAVI: See that pool?  You can check your stats there!\n";

		Wait();

		cout << "Where will you go?\n";
		cout << "1: Inn\n";
		cout << "2: Armory\n";
		cout << "3: Kokiri Shop\n";
		cout << "4: Check Stats\n";
		cout << "5: Exit town\n";

		cout << ">";
		
		cin >> choice;

		cout << "\n";

		switch (choice)
		{
		case 1:
			{
				Inn(player);
				break;
			}
		case 2: 
			{
				//Armory();
				break;
			}
		case 3:
			{
				//ItemShop();
				break;
			}
		case 4:
			{
				//Stats();
				break;
			}
		default: break;
		}
	}

	return true;
}

void Inn(PLAYER* player)
{
	cout << "You enter the inn.  You can restore your Health and Mana here.\n";
	cout << "NAVI: This is a useful place!  You should talk to the owner; she may be able to help you.\n";
	cout << "You walk over to the owner.  She smiles.\n";
	cout << "OWNER: How may I help you?  I'm Selara.  I own this Inn.\n";
	cout << "It will only cost you 10 Rupees to stay for the night.\n";

	char answer [5];
	answer[0] = 'm';

	while (answer[0] != 'y' ||answer[0] != 'n')
	{
		cout << "Stay for 10 Rupees? y/n\n";
		cin >> answer;
	}

	cout << endl;

	if (answer[0] == 'y')
	{
		if (player-> GetGold() >= 10)
		{
			player->LoseGold(10);
			cout << "You choose a room, sit on the bed, and fall into a deep slumber...\n";
			Wait();
			cout << "\nHealth and Mana fully restored...\n\n";
			player->SetHealth(player->GetMaxHealth());
			player->SetMana(player->GetMaxMana());
			Wait();
		}
		else
		{
			cout << "You don't have enough Rupees.\n";
			Wait();
			answer[0] = 'n';
		}
	}

	if (answer[0] = 'n')
	{
		cout <<"You exit the Inn.\n";
		Wait();
	}
}



Here is the Field code ( if there's a problem with that town option, tell me):

#include "Library.h"

//HYRULEFIELD

// GLOBAL VARIABLES

bool grcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

PLAYER player;

bool HyruleField()
{
	int choice = 0;

	while (choice != 10)

	{
		cout << "You stand in the middle of a field with roads going many directions.\n";
		cout << "There is a town behind you full of useful shops and many people.\n\n";

		cout << "NAVI: Which path will you take?\n";
		cout << "1: Kokiri Town\n";
		cout << "2: Monster Portal\n";
	
		if (!grcomplete)
			cout << "3: Grasslands Road (QUEST)\n";

		if (grcomplete && !fcomplete)
			cout << "4: Lost Woods Road (QUEST)\n";

		if (fcomplete && !scomplete)
			cout << "5: Wasteland Road (QUEST)\n";

		if (scomplete && !gcomplete)
			cout << "6: Lifeless Road (QUEST)\n";

		if (gcomplete && !ccomplete)
			cout << "7: Void Castle Road (QUEST)\n";

		cout << "10: Exit Game\n";

		cout << ">";

		cin >>choice;

		cout << "\n";

		cout << endl;

		switch(choice)
		{
			case 1:
				{
					Town(&player);
					break;
				}
			case 2:
				{
					//Monster Portal();
					break;
				}
			case 3:
				{
					//if (!grcomplete)
						//Grasslands();
					break;
				}
			case 4:
				{
					//if (grcomplete && !fcomplete)
						//Lost Woods();
					break;
				}
			case 5:
				{
					//if (fcomplete && !scomplete)
						//Wasteland();
					break;
				}
			case 6: 
				{
					//if (scomplete && !gcomplete)
						//Lifeless Field();
					break;
				}
			case 7:
				{
					// if (gcomplete && !ccomplete)
						//Void Castle();
					break;
				}

		default: break;
		}

	}

	return true;

}



The errors are:
1>Hyrule Field.obj : error LNK2019: unresolved external symbol "bool __cdecl Town(class PLAYER *)" (?Town@@YA_NPAVPLAYER@@@Z) referenced in function "bool __cdecl HyruleField(void)" (?HyruleField@@YA_NXZ)
1>Town.obj : error LNK2019: unresolved external symbol "void __cdecl Inn(class PLAYER)" (?Inn@@YAXVPLAYER@@@Z) referenced in function "bool __cdecl Town(class PLAYER)" (?Town@@YA_NVPLAYER@@@Z)
1>C:\Users\Connor\Documents\Visual Studio 2010\Projects\The Legend of Zelda Scroll of Worlds\Debug\The Legend of Zelda Scroll of Worlds.exe : fatal error LNK1120: 2 unresolved externals

Thanks.

This post has been edited by RedAria: 30 June 2011 - 01:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Another problem with C++

#2 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Another problem with C++

Posted 30 June 2011 - 01:21 PM

The first error is from line 58 at HyruleField function definition file.
Town(&player);


But your class definition is Town(PLAYER) and not Town(PLAYER*).

Similar is the second error. (I would like to see you solve it :). Such skills are essential to become a good programmer.)

EDIT: And why so many global variables? Especially that PLAYER player concerns me.

This post has been edited by Munawwar: 30 June 2011 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 01:45 PM

I'm not sure on the global variables. I'm following a walkthrough on making a text-based game, and that is what it said to do.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Another problem with C++

Posted 30 June 2011 - 01:57 PM

Just know this, global variables are a sign of a bad design. If it's a small trivial program, you can usually get away with it, but in larger programs, it becomes really messy and hard to maintain.
Was This Post Helpful? 0
  • +
  • -

#5 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 02:15 PM

I'm not sure how big the program is.

I am still having problems with it. It has a lot of errors or only a few depending on what kind of "player" I put (all caps, with a '*', all lowercase). I'm very confused.
Also, sorry if I'm being an annoyance, I just started c++. I don't know a lot about it.

Here I have the code versions (with different 'player's) with the least errors (just one).
Field:
#include "Library.h"

//HYRULEFIELD

// GLOBAL VARIABLES

bool grcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

PLAYER player;

bool HyruleField()
{
	int choice = 0;

	while (choice != 10)

	{
		cout << "You stand in the middle of a field with roads going many directions.\n";
		cout << "There is a town behind you full of useful shops and many people.\n\n";

		cout << "NAVI: Which path will you take?\n";
		cout << "1: Kokiri Town\n";
		cout << "2: Monster Portal\n";
	
		if (!grcomplete)
			cout << "3: Grasslands Road (QUEST)\n";

		if (grcomplete && !fcomplete)
			cout << "4: Lost Woods Road (QUEST)\n";

		if (fcomplete && !scomplete)
			cout << "5: Wasteland Road (QUEST)\n";

		if (scomplete && !gcomplete)
			cout << "6: Lifeless Road (QUEST)\n";

		if (gcomplete && !ccomplete)
			cout << "7: Void Castle Road (QUEST)\n";

		cout << "10: Exit Game\n";

		cout << ">";

		cin >>choice;

		cout << "\n";

		cout << endl;

		switch(choice)
		{
			case 1:
				{
					Town(&player);
					break;
				}
			case 2:
				{
					//Monster Portal();
					break;
				}
			case 3:
				{
					//if (!grcomplete)
						//Grasslands();
					break;
				}
			case 4:
				{
					//if (grcomplete && !fcomplete)
						//Lost Woods();
					break;
				}
			case 5:
				{
					//if (fcomplete && !scomplete)
						//Wasteland();
					break;
				}
			case 6: 
				{
					//if (scomplete && !gcomplete)
						//Lifeless Field();
					break;
				}
			case 7:
				{
					// if (gcomplete && !ccomplete)
						//Void Castle();
					break;
				}

		default: break;
		}

	}

	return true;

}


Town:
#include "Library.h"

//TOWN

void Inn(PLAYER);

bool Town(PLAYER *player)
{
	
	int choice = 0;

	while (choice != 5)
	{
		cout << "You enter the Kokiri Town.\n";
		cout << "You see many children milling around and playing.\n";
		cout << "NAVI: This looks like a good place to buy items!  There's even an Inn where you can stay to replenish your HP and MP.\n";
		cout << "To your right there is an Inn.  To your left you see the Armory and the Kokiri \nShop.  Near you is a reflective pool.\n\n";
		cout << "NAVI: See that pool?  You can check your stats there!\n";

		Wait();

		cout << "Where will you go?\n";
		cout << "1: Inn\n";
		cout << "2: Armory\n";
		cout << "3: Kokiri Shop\n";
		cout << "4: Check Stats\n";
		cout << "5: Exit town\n";

		cout << ">";
		
		cin >> choice;

		cout << "\n";

		switch (choice)
		{
		case 1:
			{
				Inn(player);
				break;
			}
		case 2: 
			{
				//Armory();
				break;
			}
		case 3:
			{
				//ItemShop();
				break;
			}
		case 4:
			{
				//Stats();
				break;
			}
		default: break;
		}
	}

	return true;
}

void Inn(PLAYER *player)
{
	cout << "You enter the inn.  You can restore your Health and Mana here.\n";
	cout << "NAVI: This is a useful place!  You should talk to the owner; she may be able to help you.\n";
	cout << "You walk over to the owner.  She smiles.\n";
	cout << "OWNER: How may I help you?  I'm Selara.  I own this Inn.\n";
	cout << "It will only cost you 10 Rupees to stay for the night.\n";

	char answer [5];
	answer[0] = 'm';

	while (answer[0] != 'y' ||answer[0] != 'n')
	{
		cout << "Stay for 10 Rupees? y/n\n";
		cin >> answer;
	}

	cout << endl;

	if (answer[0] == 'y')
	{
		if (player-> GetGold() >= 10)
		{
			player->LoseGold(10);
			cout << "You choose a room, sit on the bed, and fall into a deep slumber...\n";
			Wait();
			cout << "\nHealth and Mana fully restored...\n\n";
			player->SetHealth(player->GetMaxHealth());
			player->SetMana(player->GetMaxMana());
			Wait();
		}
		else
		{
			cout << "You don't have enough Rupees.\n";
			Wait();
			answer[0] = 'n';
		}
	}

	if (answer[0] = 'n')
	{
		cout <<"You exit the Inn.\n";
		Wait();
	}
}


This post has been edited by RedAria: 30 June 2011 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Another problem with C++

Posted 30 June 2011 - 02:21 PM

You declared in the function prototype that it takes a PLAYER variable by VALUE:
bool Town(PLAYER player)

You then go on to call the Town() function but you pass it the address of a PLAYER:
Town(&player);

The '&' is the address-of operator, you have to decide if you can pass it by-value or by a pointer. Once you decide the solution is simple, either get rid of the '&' in the function call, or change the function to take a pointer. If you don't know about pointers, then you should just get rid of the '&' in the function call.

This post has been edited by vividexstance: 30 June 2011 - 02:21 PM

Was This Post Helpful? 1
  • +
  • -

#7 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 02:28 PM

If I remove the '&', it tells me that:
'no suitible conversion function from "PLAYER" to "PLAYER *" exists'.
Also, the error says that the problem is in Inn.
Error:
1>c:\users\connor\documents\visual studio 2010\projects\the legend of zelda scroll of worlds\the legend of zelda scroll of worlds\town.cpp(41): error C2664: 'Inn' : cannot convert parameter 1 from 'PLAYER *' to 'PLAYER'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

This post has been edited by RedAria: 30 June 2011 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Another problem with C++

Posted 30 June 2011 - 02:39 PM

I see what the problem is, the definition of Inn() is
Inn(PLAYER* player);

you still have it as a pointer to PLAYER. Do you need the function to take a pointer, or can it be pass-by-value? If it can, then get rid of the '*' after PLAYER, because that makes the argument a pointer. If your function looks like this:
void Inn(PLAYER player) { .... }

and when you call the function it looks like this:
Inn(player);

then you should be all set. Whenever you write functions, the function declaration(prototype) and the function definition both need to be the same. Also when you call the function, the arguments passed to it, need to match the function definition. That's what your problem is.
Was This Post Helpful? 0
  • +
  • -

#9 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 02:49 PM

If I remove the '*', it cannot define all of the 'player's below it in this part:
if (answer[0] == 'y')  

     {  

         if (player-> GetGold() >= 10)  

         {  

             player->LoseGold(10);  

             cout << "You choose a room, sit on the bed, and fall into a deep slumber...\n";  

             Wait();  

             cout << "\nHealth and Mana fully restored...\n\n";  

             player->SetHealth(player->GetMaxHealth());  

             player->SetMana(player->GetMaxMana());  



say that the
'expression must have pointer type'. It almost seems like it's trying to contradict you.
Was This Post Helpful? 0
  • +
  • -

#10 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Another problem with C++

Posted 30 June 2011 - 04:04 PM

Do you know what pointers are? It looks to me like you're just copying/pasting code and don't know what it does. I didn't look at the definition of the function, but since the function uses pointers, then you need to leave the '*' in the argument list, and where you call the function you need to pass the address of the player, and not just the player. Can you post "Library.h"?

If you put the code back the way it was, and then where you call Inn():
Inn(player);

Your passing by-value there, but the function definition needs a pointer, so you need to either pass a pointer or the address to a PLAYER variable:
Inn(&player);


Answer me this, are you writing this code yourself?

This post has been edited by vividexstance: 30 June 2011 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#11 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 04:07 PM

No, I'm not writing this myself, I'm using a walkthrough for it since I'm new to c++. I have Library.h here:
#include <iostream>
using namespace std;

#include "Player.h"

#include "Globals.h"


Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Another problem with C++

Posted 30 June 2011 - 04:12 PM

Well, you might want to read up on the basics of C++ first. Like pointers, functions, passing-by-value, passing-by-reference, and passing pointers. Either way, to fix your current problem, you just need to change how you call the Inn() function. You have it defined to take a pointer, so you need to pass either a pointer to a player, or the address of a player variable.
Was This Post Helpful? 0
  • +
  • -

#13 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 04:57 PM

Alright.
So I read up on pointers and it may have helped, but I'm STILL having a problem.
*facepalms*
Everything works, except that the Inn still won't. It says that
'no instance of overloaded function "Inn" matches the argument list.'
I don't know if thsi has to do with the pointer or what.
Sorry for my low knoledge of c++.
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,506
  • Joined: 19-February 09

Re: Another problem with C++

Posted 30 June 2011 - 05:11 PM

When player is a pointer to an object of a structure or a class, you use the arrow operator.

  player->GetGold();



When player is an object of a structure or a class, you use the dot operator.

  player.GetGold();


Was This Post Helpful? 0
  • +
  • -

#15 RedAria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-June 11

Re: Another problem with C++

Posted 30 June 2011 - 05:20 PM

I forgot to specify the area of code that has a problem:
Inn(&player);
break;


It's the 'Inn' part.

This post has been edited by RedAria: 30 June 2011 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2