Creating an object with "<<" allowed?

I am trying to make some sort of text based game and I am still learning c++. I have already set up a simple class as "monster" which will be used by the enemy and the player.

This is my code which the compiler complains:
1
2
3
4
	for(int i = 0; i < numMonster; ++i)
	{
		monster Monster << i; //numMonster can never be higher than 5
	}


Why doesn't this work? I don't want to end up having to make 5 of the object "monster", is there a way that I could make them have different names? I know there is a way using "ifs" but thats just gets too messy if I ever want to have 100s of these.

Is there an alternative to what I am trying to achieve? I don't want to make more than necessary monsters because that would use up too much unused space.

Many thanks in advance!
<< is an operator. What you have there doesn't make too much sense... I know some people here disagree with me on this, but if you don't know what arrays or vectors are, or what a declaration is yet maybe you should learn the language first and then attempt to make a game.
Last edited on
Well, I agree with hanst99

Line 3 is very, very wrong. Not just the misuse of operator<<

To start with you need to get a handle on instances of classes, and how to construct them.
And then scoping.
Last edited on
Like I said I only know little :P

Go on this thread:
http://www.cplusplus.com/forum/articles/12974/

It has projects which are suited for beginners, I am up to the dungeon crawl and I find it impossible to do without using classes.

The problem isn't the class, the problem is the rest.
Do you have any suggestions for tutorials that I could possible do? Or simple projects?
The C++ language tutorial on this page would be a good start. I see projects as practices - they are good for strengthening understanding of certain concepts, but they don't really help learning the language itself.
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.)
Last edited on
Topic archived. No new replies allowed.