Jump to content
  • Advertisement
Sign in to follow this  
mrtie_dye

operator new

This topic is 5493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to create a game that has many different rooms. Each room will have several objects that contain a description of the object, as well as a few functions that format the description and display it on the screen. I want to create a function for each room that will initilize all the objects in that particular room. Once I exit the initilization function, I will want to call on the object members in a function that decides which room to display. Once I am done with that room, I will destroy all the objects. I know I am going to need to use the operator new, but the book I am learning from only has a few paragraphs on new. Can someone show me how to use new? Here is the code so you can see what I am talking about.
class CItem
{
/* This is the class that will hold the description as well as the functions to print the descrition to the screen.*/
};

void InitRoom1(void);
void DestroyRoom1(void);

void InitRoom1(void)
{	
	CItem Room();
	CItem Poster();

/* Not sure what to do here, but I need some way of making the objects created above last outside this funtion.*/
}

void DestroyRoom1(void)
{
//This will destroy all objects once I am done with that room
}


int main()
{
	int lookingat;	/*this keeps track of what object they  are looking at*/
	InitRoom1();	//Initilize the room

	switch(lookingat)
	{
/* How do I keep the objects alive so I can use them here? I don't want to make them static, becasue I want to be able to destroy them.*/
		case 1: room.printdesc();
		break;

		case 2: posterprintdesc();
		break;

		default: cout << "What are you looking at?";
	}

	DestroyRoom1();
	
	return 0;
}




[Edited by - mrtie_dye on September 8, 2004 12:02:07 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:

CItem * items = new CItem[NUMITEMS];

items[index].function();
...

delete[] items;


I assumed that the first line goes in the InitRoom1 function, the second line goes in main, and the third line goes in the DestroyRoom1 function. When I do this, it gives me an undeclared identifier error for items in the DestroyRoom1 function and in main.

Share this post


Link to post
Share on other sites
keep in mind that if you have this line in your initializtion funciton:

CItem * items = new CItem[NUMITEMS];

the code in your other functions will not be able to see it because it will not be in scope.

What you must do is place the declaration of items:

CItem *items;

Somewhere that all of the functions can then see it.

In your case this would be as a globally accessable variable. You will probably want to just use a global. The following is a mockup of what I'm talking about:

struct some_class {};

some_class *global_var;

void InitGlobal()
{
global_var = new some_class[100];
}

void UseGlobal()
{
... do something with global_var ...
}

void DestroyGlobal()
{
delete[] global_var;
}



edit: fixed code tags

Share this post


Link to post
Share on other sites
No, man(or woman), you either have to make items global or make it a member in a class. Because what you've done, is declared it inside a function. As soon as that function exits, items is no longer a valid identifier. Its gone out of scope.

EDIT: Darn, just missed it!

Share this post


Link to post
Share on other sites
Thanks so much for your help. I am almost there. A strange thing happens, though, and I don't know how to find out why. I have included the full code below so you can put it into c++ and see the error message. If you look at the function SnakesRoom(), you will see where I delete pRoom on the last line of the function. If I comment this line out, the program runs fine. If I leave it there, it gives me some strange debug error. Any help would be greatly appreciated.


#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

class CItem
{
private:
char* pm_desc;
int* charcount;
static int state;

public:

CItem(char* desc, int charcounted)
{
pm_desc = new char[strlen(desc) + 1];
strcpy(pm_desc, desc);
charcount = new int;
*charcount = charcounted;

}

CItem()
{
}

void DrawLine(string szText, int X, int Y, WORD color)
{
HANDLE OutputH;
COORD position = {X, Y};

OutputH = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(OutputH, color);

SetConsoleCursorPosition(OutputH, position);

cout << szText;

}

void DrawBorder()
{
int x = 3;
int y = 2;

DrawLine("*************************************************************************", x, y, FOREGROUND_BLUE);
for(int i; y < 18; y++)
{
DrawLine("*", x, y,FOREGROUND_BLUE);
}
y = 3;
x = 75;
for(; y < 18; y++)
{
DrawLine("*", x, y,FOREGROUND_BLUE);
}
x= 3;
DrawLine("*************************************************************************", x, y, FOREGROUND_BLUE);
}


void mf_printdesc()
{
int count = 60;
int currentchar = 0;
cout << endl << endl << endl;

while(count < *charcount)
{
while(pm_desc[count] != ' ' || count > *charcount)
count--;
cout << endl << "\t";
for(;currentchar < count; currentchar++)
{
cout << pm_desc[currentchar];
}
count += 60;
}
cout << endl << "\t";
for(; currentchar < *charcount; currentchar++)
{
cout << pm_desc[currentchar];
}

DrawBorder();
DrawLine("Enter Command: ", 3, 20, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
delete[] pm_desc;
delete charcount;
}
};

void SnakesRoom(void);
void InitSnakeRoom(CItem* pRoom);

void SnakesRoom(void)
{
CItem* pRoom = new CItem[1];
InitSnakeRoom(pRoom);
pRoom[0].mf_printdesc();
delete[] pRoom;
}

void InitSnakeRoom(CItem* pRoom)
{
char room[] = {" Description of the room. I have added this sentence in here just so you can see what the mf_printdesc function does."};
char poster[] = {" Description of a poster."};
char* proom = room;
char* pposter = poster;
int charcount;

charcount = (sizeof room) / (sizeof room[0]);
CItem Room(proom, charcount);

charcount = (sizeof poster) / (sizeof poster[0]);
CItem Poster(pposter, charcount);

pRoom[0] = Room;
pRoom[1] = Poster;
}

int main()
{
SnakesRoom();
return 0;
}


Share this post


Link to post
Share on other sites
Its a bad idea to re-allocate your CItems every time you go in the room. Besides, do you really need to dynamically allocate them?

However, The error is coming up because you are only allocating 1 CItem, and you try to change 2 of them. Then on the delete, it causes an assertian failure.

To fix this, do this:
CItem * pRoom = new CItem[2]; //allocates 2 CItems

because later on, you do this:

//modifies 2 CItems, one of which was not allocated to you
pRoom[0] = Room;
pRoom[1] = Poster;

Share this post


Link to post
Share on other sites
Quote:
Original post by mrtie_dye
Thanks so much for your help. I am almost there. A strange thing happens, though, and I don't know how to find out why. I have included the full code below so you can put it into c++ and see the error message. If you look at the function SnakesRoom(), you will see where I delete pRoom on the last line of the function. If I comment this line out, the program runs fine. If I leave it there, it gives me some strange debug error. Any help would be greatly appreciated.

*** Source Snippet Removed ***


NO NO NO! when you are using C++, use it, not C! try this:
#include <iostream>			
#include <windows.h>
#include <string>
using namespace std; // this line is really shitty, use "using cout" instead

class CItem
{
// your CItem object
};

class SnakesRoom : public CItem
{
// your SnakesRoom object
};

int main()
{
SnakesRoom room1(); // nice
return 0;
}




[Edited by - Triglav on September 8, 2004 3:19:34 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Triglav
...

int main()
{
SnakesRoom room1(); // nice
return 0;
}



SnakesRoom room1(); is actually a function declaration, it doesnt create an instance of SnakesRoom .

SnakesRoom room1; //nicer

:)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!