#include ___.h

Well first off, this is my very first post on the forums. So hi! :D

I've been learning C++ for a while now, finding internet tutorials, reading books, consulting my uncle once but that was only while he was visiting a month ago. I ran into problems, and usually found the answer via google or my "Sam's Teach Yourself C++" book by Jesse Liberty and Rogers Cadenhead. Don't mean to advertise, but if other beginners want to take a look at that then I highly reccomend it.

So to get to the point here, I can't find the answer this time. I've found related questions, but none have seemed to help me (they probably could, I just get lost very easily in all the other answers and then I don't find mine).

My problem is that I can't figure out what is wrong with how I'm declaring/linking my class.cpp, header.h, and main.cpp.

I've been coding some things like rectangle calculators, guessing games, and so on, and I finally decided I (aka thought I) was ready to make a text-based game. I have some done already, but I never really bothered to separate out the classes from the main. I was trying to tonight, and after about three hours of working it over (including numerous breaks) I still get the same error (and the same headache):

 
error: expected unqualified-id before 'public'


I can't seem to get rid of this one error, no matter how I work it.

Here's the code:

zombie.h
1
2
3
4
5
6
public class zombie()
{
  int hp;
  int st;
  int ref;
};

zombie.cpp
1
2
3
4
5
6
7
8
#include "zombie.h"

void zombie()
{
    int hp = (rand()%2)+5;
    int st = (rand()%2)+5;
    int ref = (rand()%2)+5;
}


And here's the "#include"s of main.ccp, where I'm trying to call up a zombie...

main.cpp
1
2
3
4
5
6
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;

#include "zombie.h" 


In main.cpp, all the code worked before I even implemented the zombie files. Now, I put in the zombie.h and zombie.cpp, and added cout << zombie.st; to main.cpp and it breaks everything with that one little error.

Anyone who actually knows what they're doing have any advice? Thanks.
1
2
3
4
5
6
public class zombie()
{
  int hp;
  int st;
  int ref;
};


This is not how you define a class in C++.

1) Don't put public before the class keyword. Whether a class is public depends on its current scope (if defined globally like you're doing, and like you'll do 99% of the time, it will be public)

2) don't put parenthesis after the class name. You do that for functions, not classes.

3) If you want your members (hp, st, ref) to be public, you put a public label before you declare them.

The correct way to make that class would be like this:

1
2
3
4
5
6
7
class zombie
{
public:
  int hp;
  int st;
  int ref;
};


Or, since you have all public members and no functions, a struct might be better suited. You could also declare it like this:

1
2
3
4
5
6
7
8
struct zombie
{
  // note you don't need a "public:" here
  //  struct members default to public
  int hp;
  int st;
  int ref;
};



Also...

1
2
3
4
5
6
void zombie()
{
    int hp = (rand()%2)+5;
    int st = (rand()%2)+5;
    int ref = (rand()%2)+5;
}


This is also illegal.

zombie is a class, not a function. Name it something else.

Or are you trying to make a constructor here? If you are:

1) don't put a return type (like 'void'). Constructors don't have a return type.

2) constructors need to be prototyped in the class:

1
2
3
4
5
6
class zombie
{
public:
  zombie();  // prototype the ctor here, again note:  no "void"
  // ...
};


3) You need to put the scope operator when giving the ctor a body:

1
2
3
4
zombie::zombie()  // again note:  no "void"
{
  // ...
}


4) don't redefine your variables in your ctor. saying int hp creates another integer with the same name. It does not change the "hp" that belongs to the class. Instead it changes the hp that is local only to the ctor.

1
2
3
4
5
zombie::zombie()
{
  hp = (rand()%2)+5;  // note:  no "int".  We don't want to declare a new var here
  // .. do the same with st, ref
};
Thank you, now that I look at this it all seems so simple, especially since I knew most of this (though still far from an expert, or even average). I guess I was just staring at the code for too long. Thanks for your time and patience, the game is running smooth again.
Last edited on
Topic archived. No new replies allowed.