Instantiating an object

Hey guys need some help.

Is this possible
1
2
3
  	char** level; 
level[o][o] = new Class();


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]
Last edited on
Guys please help.I tried eveything and looked everywhere.Its really bugging me.help please
You are confused as heck.

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:

1
2
3
char** level = new char*[/* rows */];
level[0] = new char[/* columns */];
// ... 

Get your facts straight before you fiddle around.
From what we understand, you're trying to create a two dimensional array of your class. Your instantiation of your object shouldn't be a problem.

1
2
Class level[numRows][numCols];
level[0][0] = new Class('C');


Hope this helps.
Last edited on
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.
GRex2595, that won't compile. It should be something like:

1
2
Class* level[rows][columns];
level[0][0] = new Class('C');
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.

http://www.cplusplus.com/doc/tutorial/inheritance/

*I program more in Java, so I don't know for sure if this will work, but since Java is derived from C++, I think it'll work.
Do all the things (zombies, player, walls, etc.) that go on that map have a common, parent class?
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

Edit:*spelling
Last edited on
Then create an array Map of type ParentClass and assign its cells any derived object that you want. I.e.,

1
2
3
4
ParentClass* Map[rows][columns];
Map[0][0] = new Player('P');
Map[3][9] = new Zombie('Z');
// etc. 
1
2
3
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.
Last edited on
GRex, lines 2 and 3 of your code assume level is declared as this:
 
Parent* level[numRows][numCols];
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.

http://stackoverflow.com/questions/9772053/defining-an-array-of-type-class

The OP and one of the responders didn't have the array as a pointer, so I figured there wouldn't be a problem with it not being a pointer array.
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.
Are you talking about it doing this on the console, or doing this in the code?
Implementing it in my code but updating on the console the console.Your help will be much appreciated.I
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.
Topic archived. No new replies allowed.