Archived

This topic is now archived and is closed to further replies.

Nazrix

problem w/ Classes

Recommended Posts

Okay, it''s another stupid Nazrix question I am just trying to make a class w/ 2 int variables, then declare item_database[3]. Then just set the location variable to different values.
  
class CNpcDatabaseInfo
{
public:
	int number;
	int location;

};

        CNpcDatabaseInfo items_database[3];

        items_database[0].location=2;
	items_database[1].location=1;
	items_database[2].location=0;
	items_database[3].location=2;
  
If I then check what items_database[0].location is, it gives me a wierd negative number which basically means to me that I''m not allocating the content of location correctly. Perhaps I should use a constructor of some sort. I am just not sure exactly how to do it. Thanks for any info. anyone has
http://www15.brinkster.com/nazrix/main.html "All you touch and all you see is all your life will ever be --Pink Floyd Need help? Well, go FAQ yourself.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
is that even possible
u only declared 3 elements
but u initialized 4?

i dont think this is right
items_database[3].location=2;

i shouldnt be there

Share this post


Link to post
Share on other sites
Well, the array would start w/ 0 and end w/ element #3...

I tried taking off the 3rd, but to no avail.




http://www15.brinkster.com/nazrix/main.html

"All you touch and all you see is all your life will ever be --Pink Floyd
Need help? Well, go FAQ yourself.

Share this post


Link to post
Share on other sites
He''s right it would be 0-2 (the off-by-one problem) but the problem doesn''t seem to be in what you described there. Trying reexamining the logic; I''ve had problems because I inadvertantly rewrote over the data. Sounds like the numbers weren''t being initialized before you checked them.

Try running different programs before running yours and see if different numbers appear. If they change then check flow control.

Share this post


Link to post
Share on other sites
oh yeah, that's right...I don't know what I was thinking...

It didn't help though...There's got to be something else unrelated I'm doing wrong cause now I just used a constructor like this:

    
class CNpcDatabaseInfo
{
public:
int number;
int location2;


CNpcDatabaseInfo()
{
number=0; location=0;
}
};

CNpcDatabaseInfo items_database[3];



I remarked out the part where I declare each location individually, but when I display the value of items_database[0].location it gives me a long negative number. It should be zero, right?






http://www15.brinkster.com/nazrix/main.html

"All you touch and all you see is all your life will ever be --Pink Floyd
Need help? Well, go FAQ yourself.


Edited by - Nazrix on January 30, 2001 4:59:14 PM

Share this post


Link to post
Share on other sites
Yes, your numbers are not being initialized. You should set them to zero in a constructor, like so:

CNpcDatabaseInfo::CNpcDatabaseInfo() : number(0), location(0) {}

Liquid

Share this post


Link to post
Share on other sites
quote:
Original post by C-Junkie
Sounds like the numbers weren''t being initialized before you checked them.



Nope, ''cause when I was initializing them individually, it was the very first thing that happened. It was in my Game_Init(), and it was the first line in there.

I''m doing something stupid, but I''m not sure what it is yet






http://www15.brinkster.com/nazrix/main.html

"All you touch and all you see is all your life will ever be --Pink Floyd
Need help? Well, go FAQ yourself.

Share this post


Link to post
Share on other sites
Use your debugger, step through the code putting a breakpoint there. Since you already have one severe bug in there (array size is 3, but you initialize 4 of them--will cause Very Bad Things to happen), you might want to look at some other piece of your code doing something to screw up memory. For instance, the "this" pointer could be overwritten, etc.

Also, on style: most programmers use a struct instead of a class type when declaring public data members. The two are exactly the same except for default permissions, but a C++ rule of thumb is to never have public data members in a class. I''d either change the type to struct, or move them into private data members and write accessors/operators to read/set them.

Share this post


Link to post
Share on other sites
The following implementation worked for me and I did not have a problem. It must be with something else in your code.

I did both, declared the object as a global variable and inside the main, they both gave the same results. What compiler are you using. I am using VC6 w/SP4.

You would get the problem that you outlined if somewhere in your code you are accessing an invalid object. Are you sure that the game init code has a valid database_object?

  

//Class Implementation

CNpcDatabaseInfo::CNpcDatabaseInfo()
:m_iLocation(0),m_iNumber(0)
{
}

CNpcDatabaseInfo::~CNpcDatabaseInfo()
{

}

int CNpcDatabaseInfo::GetLocation()
{
return m_iLocation;
}

int CNpcDatabaseInfo::GetNumber()
{
return m_iNumber;
}

//Main

int main(int argc, char* argv[])
{
CNpcDatabaseInfo database_objects[3];

for ( int Index=0;Index<3;Index++)
{
std::cout << "Database Object ["<<Index<<"]" << std::endl
<< "====================" << std::endl
<< "Location ==> " << database_objects[Index].GetLocation() << std::endl
<< "Number ====> " << database_objects[Index].GetNumber() << std::endl;

}

std::cin.get();

return 0;
}

Share this post


Link to post
Share on other sites
quote:
If I then check what items_database[0].location is, it gives me a wierd negative number which basically means to me that I''m not allocating the content of location correctly.


How are you displaying the data? It could very well be the method you use to display the contents. Don''t forget to check everything.

Share this post


Link to post
Share on other sites