Well, when I read the code, the first thing I see is an incorrect class declaration. Single instances of a class are being constructed -- reusing the name Monster -- within the scope of a for loop, so it won't be accessible from outside. And operator<< is then being misapplied to it.
Your code fragment is too short for me to get a good gauge of your current knowledge, but after scanning the exercises you link to, I would suggest you start off with an array-based approach along the lines of
1 2 3 4 5 6
|
monster Monster[5];
for(int i = 0; i < numMonster; ++i)
{
Monster[i].setId(i); //numMonster can never be higher than 5
}
|
This requires the monster class to have a default constructor and a setId() method.
A neater solution would use a std::vector of monsters, but this raises various issues about the class declaration (declaring the copy constructor, assignment operator, etc. if required)
[
http://www.cplusplus.com/reference/stl/vector/]
So have you read?
[
http://www.cplusplus.com/doc/tutorial/classes/]
[
http://www.cplusplus.com/doc/tutorial/classes2/]
Now, while this is wrong, as you can't use an operator as part of a constructor declaration (directly), apart from operator= (see note below)
monster Monster << i;
you could use
monster Monster = monster() << i;
but this is braindead code: it creates a temporary default monster, applies the operator to it, and then fires the copy constructor.
what you need for a
single instance is
monster Monster(i);
which uses a constructor that takes an int. But this only works for single instances, not for arrays.
[
http://www.cplusplus.com/doc/tutorial/arrays/]
Arrays of classes always use the default constructor. Which is why the init loop above uses setId().
(Note that "operator=" is a special case for constructor declarastions:
monster Monster = i;
is equivalent to
monster Monster(i);
that is, the "=" causes the a constructor to be used, not operator=)
(Note that the rules are about to change, with C++11. See the "Initializer List" on the this page:
http://en.wikipedia.org/wiki/C%2B%2B11)
[
http://www.cplusplus.com/doc/tutorial/dynamic/]
You could get around this by using dynamic allocation, creating the monsters on the heap using new, but this brings in a lot of other, unwanted issues. For example, pointers
[
http://www.cplusplus.com/doc/tutorial/pointers/]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
monster* Monster[5]; // monster pointers
for(int i = 0; i < numMonster; ++i)
{
Monster[i] = new monster(i); // uses int constructor
}
// use monster pointers (call through ->)
int a = ...
int b = ...
Monster[a]->attack(Monster[b]);
...
for(int i = 0; i < numMonster; ++i)
{
delete Monster[i]; // destroy monster
}
|
[
http://www.cplusplus.com/doc/tutorial/operators/]
I would leave operator<< aside for now. Unless you want to write your monsters out to cout or other ostream. (It should only ever be used for either bit shift operations or stream instertions: not to act as a setter. "Preserve natural semantics for overloaded operators" : C++ Coding Standards, by Sutter & Alexandrescu.)