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.

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:

Re: Another problem with C++

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.

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.

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:

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.

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++.