Ok, I'm in my second month of C++ at Keiser University. Our final project is to make a console application of the board game Battleship, putting nearly all the code into a class in a header file and calling that in a separate source file.
Generally speaking, it's going poorly. X.X Specifically though, the compiler I'm using (Visual Studio) is giving me error messages under the equals sign in my attribute initializations. Not only that, but when I call those variables later, it flags them and says that they are undefined.
Wait a second. I just defined the one I called barely ten lines ago. Anybody know what's going on here? **NOTE: cropped because it's all a mishmash atm, but this is the part I'm referring to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int p1Win = 0;
int p2Win = 0;
int p1_score = 0;
int p2_score = 0;
int draw = 0;
int boolInt = 0;
bool comp = " ";
};
void primary()
{
srand(time(NULL));
cout << " Battleship" <<endl;
while(p1Win == 0 && p2Win == 0)
^^I am getting red squiggles similar to MS Word under the second p1Win and p2Win.
Inside the function primary, the following variables exist:
1) Global variables.
2) Variables you passed in (none of those, I see - primary takes no parameters)
3) Variables you created in the function. None of those.
The variables p1Win and p2Win don't exist inside the function primary. This is how C++ works. Look up "local scope". And then global scope, and any other kind of scope that gets mentioned.
Is that a class definition above the function primary? If that's a class definition, you've really missed the point of classes and need to go back to the start.
Although I have had my share of variable scope problems, I don't think that's the case here. Above that function, other than a few #include statements and little else, is a class declaration with public and private sections (that's what that brace and semicolon under line 7 is.) Would that make a difference?
An enormous difference. Here is how to use classes:
1) Define the class. This is what you've done above the function primary, I believe. Defining a class does not create an instance of that class.
2) Create an instance of the class. This is done in the usual way; classType name;, much like int x; or double y;
3) Use it.
Here is an example of parts 1 and 2 and 3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// define the class
class food
{
public:
string nameOfFood;
int calories;
};
// Now, create an instance
food someInstanceOfTheFoodClass;
// Now, use it
someInstanceOfTheFoodClass.calories = 7;
That's most likely what it is. I'm still VEEERY iffy on class structures, so I'll give this a shot. Thanks everybody for the answers! I'll probably be back with more questions soon hehe.
class game
{
public:
//declare functions
void input1();
//void input2(bool);
void display();
void primary();
//declare variables
private:
char board [10][10];
int p1Win;
int p2Win;
int draw;
bool comp;
};
game battleship;
battleship.p1Win = 0;
{
p1Win = 0;
The error is on "battleship." and stops before the p1Win integer, saying it has no storage class. Going by the above example, it would be comparable to "someInstanceOfTheFoodClass." It's an instance of the "game" class, so supposedly wouldn't need a data type like int.
What exactly are you trying to do here? This makes no sense. WHAT variable named p1Win? The only variables that exist are INSIDE your class object, and you've ALREADY tried to set that to zero like this: battleship.p1Win = 0;
That won't actually work because you made p1Win a private variable, but I can see what you tried.
I've got that part mostly working now. I was missing a constructor and a game() function inside of the class. Bigger and nastier problems await me now ;)