Skip to main content
gamedev.net
🔒 Locked

weird c++ problem

Started by tjrr3d Dec 20, 2005 at 5:03 PM 5 replies 500+ views
Original Post
tjrr3d
tjrr3d
Hi all. I'm making a squirrel game with SDL and I'm having some weird compile errors. FYI: SDL, VS.NET I have a squirrel class like this: #define SPEED 5 Squirrel::Squirrel(SDL_Surface *img ) { health = 100; srand(SDL_GetTicks()); position.x = rand()%640; position.y = 300; position.dx = 0; position.dy = 0; HoldingAcorn = false; currImg = img; } Squirrel::~SquirrelGame() { } So then in the main program I do this: SDL_Surface *SquirrelWalkLeft = IMG_Load("SquirrelWalkLeft.png"); Squirrel sq = new Squirrel(SquirrelWalkLeft); and I get: c:\Documents and Settings\tjrr3d\Desktop\My Toy\sqirrrelgame.cpp(45): error C2440: 'initializing' : cannot convert from 'Squirrel *' to 'Squirrel' No constructor could take the source type, or constructor overload resolution was ambiguous c:\Documents and Settings\tjrr3d\Desktop\My Toy\squirrel.cpp(18): error C2523: 'Squirrel::~SquirrelGame' : destructor tag mismatch This should be correct, right?
SiCrane
SiCrane
Squirrel sq = new Squirrel(SquirrelWalkLeft);
should be
Squirrel * sq = new Squirrel(SquirrelWalkLeft);
and
Squirrel::~SquirrelGame()
should be
Squirrel::~Squirrel()
Drew_Benton
Drew_Benton
On the line: Squirrel sq = new Squirrel(SquirrelWalkLeft);

What is the sole basics of being able to use the new operator? What must that left hand side be? If your stuck, take a look at our error message, "'initializing' : cannot convert from 'Squirrel *' to 'Squirrel'"
tjrr3d
tjrr3d
oh duh on the squirrelGame should be just squirrel(I've been coding for 9 hrs straight);

About the pointer line.
I was planning on putting all squirrels in a vector callled of course squirrels.
So i did this:
vector squirrels;

So if i do:
Squirrel *sq = new Squirrel(...);
then I can't do
squirrels.push_back(sq);

Forgive me if this is very basic for you experts out there. This is my first real c++ program.

I don't think I can make a vector of Squirrel pointers can I?
If so then how do I access each elements members?
I assume:
Squirrel *tmpSq = squirrels.at(i);
tmpSq->health

right?


jflanglois
jflanglois
You can make a vector of Squirrel pointers, but you have to deal with deallocation yourself.
Otherwise, you can either dereference the pointer when calling push_back (squirrels.push_back( *sq );) -- making sure it is a valid pointer of course -- or you can make sq a stack allocated object, that is, Squirrel sq( SquirrelWalkLeft );.


jfl.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.