Ok so I am trying to manipulate an array of game charaters with this little function. Here are the problmes I am trying to set a name to the charater.with the void setName() function which I cant get to work. And also and the comipler says that
Error 3 error C3861: 'swapChar': identifier not found c:\users\kenshin\documents\visual studio 2008\projects\arraytest\arraytest\arraytest.cpp 69 ArrayTest
when I try to call the swapChar method. What am I doing wrong here?
Thanks :)
// ArrayTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//good guy ie player
class Player
{
public:
int hp;
int sp;
int mp;
void setName(String x);
};
int main()
{
int i = 0; //set to defalut Main character Maverick
//build player
Player Maverick;
Maverick.hp = 1220;
Maverick.mp = 220;
Maverick.sp = 100;
Player Cap;
Cap.hp = 50;
Cap.mp = 100;
Cap.sp = 155;
Player Jen;
Jen.hp = 1555;
Jen.mp = 555;
Jen.sp = 111;
//array to hold characters
//This is for characters
Player players[3];
players[0] = Maverick;
players[1] = Cap;
players[2] = Jen;
cout << players[i].name <<endl; //print name of charater
cout << "Press s to change charater!" << endl;
char button; //input for button pressed
cin >> button;
//switch function
if(button == 's')
{
swapChar();
}
cout << players[i].name <<endl; //print name of charater
return 0;
}
void swapChar()
{
if(i < 3)
{
i++; // i goes to
player[i];//switch to new charater
}
}
The parameter should be string x; case matters. Identifier name (used in lines 59 and 70) is not defined anywhere, also. I don't see a definition for setName() either.
Now to swapChar() and array manipulation. You need to declare a function before you use it. Either put the whole definition of swapChar() before main() or forward declare it. Even so, it shouldn't compile because you don't define what identifier i is. The int i defined in line 32 is local to main(). swapChar() can't know about it unless you pass it to it. To stay away from pointers for now, you should define swapChar() like this:
int swapChar(int i)
And store the value returned in main()'s i (you really should rename this to something more meaningful such as currentPlayer). Also, the swapChar() function needs to check whether you're trying to swap from the last element in the array to the first one, otherwise you'll eventually end up trying to access memory outside the array (player[3]).
Finally, note that this line
player[i];//switch to new charater
is completely useless. It simply returns player[i]'s value and does nothing to it. Semantically, it's as if you said
3;
I really recommend you substitute the array for a std::vector.
Thanks everyone I new to programming and trying to learn c++ on my own.
I will have to look into this vector thing if I have read it right vectors store objects right, I thought arrays did that too, am I wrong?
Internally, vectors store their objects in arrays. However, they can grow to accomodate more objects as needed, and they handle the lower level details of array manipulation for you. They also remember their own size. Using them is simpler and safer and allows you to work at a higher level of abstraction than arrays.