the destructor isn't obligatory, neither is the constructor
you would only have a destructor if you have memory to free up so if you didn't use pointers, you wouldn't need the destructor and your code will look pretty :D
Printable View
the destructor isn't obligatory, neither is the constructor
you would only have a destructor if you have memory to free up so if you didn't use pointers, you wouldn't need the destructor and your code will look pretty :D
Oh, OK. Works for me. :D
But, now when I have something like this:
It gives me a compiler and linker output error.Code:class unit
{
int health, strength;
public:
unit (int, int);
~unit();
};
unit::unit(int hlth, int str) : health(hlth), strength(str)
{
}
int main()
{
srand(time(NULL));
unit hero(100,10);
int *enemies[5];
cin.get();
}
It says something like "undefined reference to `unit::~unit(void)'"
And when I comment out the second line in int main ("unit hero(100,10);") it compiles fine. Am I defining the object correct?
This is because you put a destructor in your class description and the compiler is now expecting you to provide one. Take it out and the compiler will do its thing and generate one for you automatically.Quote:
It gives me a compiler and linker output error.
It says something like "undefined reference to `unit::~unit(void)'"
This:
Not this:Code:class unit
{
int health, strength;
public:
unit (int, int);
};
Code:class unit
{
int health, strength;
public:
unit (int, int);
~unit();
};
Ah, of course. Stupid mistake on my part. Thanks.
Edit:
I have another question. Is there anyway to have a pointer point to an object?
I tried something to this effect, and it didn't work:
Code:int *enemies[2];
enemies[0] = &goblin;
You mean something like that?
But why use pointers if you absolutely don't need to?Code:unit* pHero = new unit(100, 10);
To make a collection of unit, use a standard container instead of raw arrays. E.g
Code:std::vector<unit> troops;
troops.push_back(unit(100, 10));
//access troops[0] from here on
Ahh. The thought of using Vectors never even came to mind. Thanks. ^^
However, I've come to yet another problem.
Something like this gives me an error:
displaystats() being a class function.Code:for(int i=0; i<=goblin.size(); i++)
{
(goblin.at(i)).displaystats();
}
It gives me an error for the line "(goblin.at(i)).displaystats();"
Error:
Anyone have any input?Code:55 c:\docume~1\me\desktop\test.cpp
no matching function for call to `vector<unit,allocator<unit> >::at (int)'
Edit: Nevermind. If I just use goblin[i] it works fine.
Make sure you use < and not <=.
Also, I'm surprised goblin.at(i) didn't work. It should. What compiler and version are you using?
That uses an old MingW version of gcc I believe. Consider upgrading. If you like Dev-C++, use the version 5 beta, which is like 4.9.9.2 or something. Other good, free options are Code::Blocks and VC++ 2005 Express.
>> I tried VC++ 2005 Express but it said it was a 30 day trial.
Really?
A couple years ago they said they would give it away free for one year. A year passed but people still kept recommending it and downloading it, so I assumed that it was still available.
Looking at the website it appears that the express edition is still free (http://msdn.microsoft.com/vstudio/express/downloads/ make sure you pick Visual C++, not C#). I prefer Visual C++ but Code::Blocks might be more suitable for a beginner.
Yup. That's the same page I downloaded it from. It said the same thing, that there was a 30 day trial, for both VC++ and VB. Though, Code::Blocks seems to be to my likings anyway so I think I'll stick with it. Thanks for the help.