So I'm a little confused about how this works. I've followed other examples to come up with the following:
nEntity = 5;
and I'm not sure why the compiler doesn't like it. Any hints?
Best How To :
It looks like you're trying to create what is called a "constructor," which is a function called to create new copies of
Entity. There's two things to fix here:
- First off, you need to forward declare your constructor. Otherwise, C++ doesn't know your class has one until it's too late. This seems a little wonky at first, but once you start using multiple cpp files, you'll see it makes more sense.
- Second, your constructor needs arguments. It's just a syntax change.
This should work:
public: // by default, everything is "private" and only available to
// Entity itself. By adding this line here, everything that
// follows can be used anywhere
Entity(); // this "forward declares" that you're going to have a
// constructor with no arguments
int nEntity; // usually variables are marked "private," but I have a
// feeling you'll want to keep it public for a bit, until
// you get used to classes
nEntity = 5;
And, as an extra credit effort, you can also use the special notation for assigning values to class members which is only available in constructors (no other functions can use this notation). They have a few advantages which are rather tricky to explain, but if you get in the habit of using this notation, it'll be helpful to you later
// no extra instructions here. The assignment was done on the
// line starting with a colon!