Basically, I want to instantiate a new class, and give it a char as a parameter.
What I have is.
1 2 3
level[o][o] = new Class('C');
I want level[0][0] to be C;
I am completely lost.When i do that it gives me this error
1 2 3 4 5 6 7
Map.h:82:40: error: invalid conversion from ‘Class*’ to ‘char’ [-fpermissive]
Map.h:87:40: error: invalid conversion from ‘Class*’ to ‘char’ [-fpermissive]
In file included from Game.h:4:0,
from Main.cpp:2:
Map.h: In member function ‘void Map::readMap()’:
Map.h:82:40: error: invalid conversion from ‘Class*’ to ‘char’ [-fpermissive]
Map.h:87:40: error: invalid conversion from ‘Class*’ to ‘char’ [-fpermissive]
char** level; declares a pointer to a pointer to a character. However, you are assigning it an object of type Class; this is a mistake.
Moreover, you are assuming that char** level; is a two-dimensional character array. This is another mistake, unless you have explicitly, dynamically allocated an array:
I'm trying to create a game.so the CLASS will contain a char that would be placed on a map.So I want to assign the parameter in the constructor to a two-dimensional array to place that passed paremeter on the map.
bookerb6, tell us more about your game, because from the sounds of it, you're complicating the task. If you want to place characters on a two-dimensional character array, why don't you simply use a function?
Whoops. I was trying to do two tasks at once. Sorry bookerb6.
I think what you're really looking for is in the class definition, not in the main part of your code. You should have a constructor that takes a character as a parameter. That will help.
Its a mission game.basically a player has to start on a certain point.say level[0][0].he has to move through the entire map fighting with zombies on the way.these zombies are also placed by a 2dimensional array on the map through certain conditions.there are a lot of zombies and each zombie has its own class.So I want to place the zombies on those specific locations on the map through calling the their constructors.
Have you ever heard of inheritance? You could use inheritance to make a class that a zombie and the player are both members of, then you can put them both in the same array.
Yes, there is a parent class, which two classes inherit from to determine whether they are moving objects or not.Then the zombies who inherit from these classes depending on whether they move or not.I have constructors that take in a char.the problem is as a showed above.creating an object and assigning it to an array
Parent[numRows][numCols] level;
level[0][0] = new ChildA(characterA);
level[0][1] = new ChildB(characterB);
This is some pseudocode where Parent is the name of the parent class, ChildA and ChildB are the names of two subclasses of Parent, and characterA and characterB are the characters you're going to use to represent the object in your output.
Another option is that you could default your character and zombies to have a specific character from the start, so you don't have to give them characters during execution.
*I've never done this in C++, but if it works like Java, then that code should work just fine.
As I said, that's how it would work in Java. I couldn't find a page that thoroughly addressed this, so I wrote my pseudocode based off of what I saw on this page.
Thanks guys
@Grex, thanks a lot.
It finally worked, now I can read my map from a file and the game plays well.But I have a slight buck.Hw do you make a screen to play on one board and not generate another everytime you input.
You can't avoid generating a new board on the console, but you can clear the console. Read the second thread in the beginners forum completely, and you should be able to find the answer to clearing the screen.
In the code, you could make your characters keep their location in the array so that you can just populate the array as needed with their locations when you need them, and you only have to update the characters that move rather than starting all over.