Jump to content
  • Advertisement
Sign in to follow this  
Sean_Seanston

Vectors won't work: "error C2664"

This topic is 3843 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

For some reason, I can't get push_back to work with vectors at all. I tried this simple code:
std::vector<int> x;
x.push_back(new int);
But I get error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'int *' to 'const int &' I've looked around and that seems to be the way that vectors are used in tutorials and I can't seem to find anything to fix this. What's happening?

Share this post


Link to post
Share on other sites
Advertisement
Error is pretty much bang on here, the compiler doesn't know how to make an integer from an integer pointer.

For a vector with template T, std::vector<T>::push_back() takes a const T reference.

Either you want a vector of integer pointers (unlikely), or you want to push_back an actual integer:

x.push_back(42);

Share this post


Link to post
Share on other sites
K...

But what I really wanted to do was use it with a user-defined class so I could make an object of that class when something happens and push it back on the vector. How's that done exactly? I saw some tutorials that seemed to be like I was doing :o

Even stranger, when I push_back a number, I get

error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: __thiscall std::_Vector_const_iterator<i...

And it goes on for ages but you get the idea. Must be something fairly simple but I haven't been able to notice anything strange or anything that I know to normally result in an unresolved external...

Share this post


Link to post
Share on other sites
Hmm, checked around and the problem seems to be a build error.

Changing the Code Generation Runtime Library to Multi-Threaded Debug DLL fixes it...

One question though: I'm using SDL and in Lazy Foo's tutorials he says to set it to Multi-Threaded DLL instead. Will the debug DLL cause a problem with SDL?

Share this post


Link to post
Share on other sites
When programming with SDL you should always use the multithreaded dll versions of the runtime library. In a debug build you would use the Multithreaded Debug DLL while in a Release build you would use the Multithreaded DLL (no debug).

Share this post


Link to post
Share on other sites
So I just use the Multithreaded Debug DLL for programming in SDL? K.

As for using new with push_back(), how do you make it work?

EDIT: Would you have to make it a vector of pointers to the objects you want to store?

Share this post


Link to post
Share on other sites
Jep, that's correct. Keep in mind that the vector won't delete the object that is pointed at when you remove the pointer from the vector, it only removes the pointer itself. You are still responsible for delete'ing what you new'ed.

Share this post


Link to post
Share on other sites
Quote:
So I just use the Multithreaded Debug DLL for programming in SDL? K.


No, you use the Debug version when you are making a Debug build, you use the Release version when you are making a Release build.

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
Jep, that's correct. Keep in mind that the vector won't delete the object that is pointed at when you remove the pointer from the vector, it only removes the pointer itself. You are still responsible for delete'ing what you new'ed.


Hmm... that seems a bit of work then... well probably just that I haven't really used pointers or dynamic allocation much before ^_^

Basically I wanted to put explosions in my Space Invaders clone that would be created and put into a vector whenever they were needed. Up to now, I'd been using static arrays for such things (like my enemies themselves) but that involved keeping all 55 of my enemies in existence even when they were no longer required or blitted to the screen, which I'm sure is terrible nooby style.

Alright, so if I want to have a vector of explosions like that, I just need to create a vector of pointers to explosions and then use the -> operator with those to change the explosions and when the explosion is finished I remove the explosion from the vector and use delete on the pointer?

Looks like this game'll be much more of a learning exercise than I'd at first anticipated. Yay I suppose :O

Quote:
Original post by CodeMunkie
Quote:
So I just use the Multithreaded Debug DLL for programming in SDL? K.


No, you use the Debug version when you are making a Debug build, you use the Release version when you are making a Release build.


K. As long as SDL won't mess up because of using the Debug DLL... it should all be fine :)

Share this post


Link to post
Share on other sites
Alright, I've actually got the explosions working now. Whenever I kill an enemy, an explosion pops up and animates perfectly.

Yay, thanks for the help all ^_^

Now for the bad part; I need help erasing the pointer from the vector and deleting it. Basically I have 5 frames in the animation, 0-4. When the frame counter gets to 5, I want to remove the explosion.

I've heard of iterators but do I need one for this?

I used normal ints fine for things like this for example:

	for(unsigned int i = 0; i < explosions.size(); i++)
{
explosions.at(i)->animate();
}


But this doesn't work:

void Explosion::manageExpl()
{
std::vector<Explosion>::iterator it;

for(unsigned int i = 0; i < explosions.size(); i++)
{
if( explosions.at(i)->frame == 5 )
{
explosions.erase(explosions.begin());
delete explosions.at(i);
}
}
}


It compiles fine but when that finishes I get an exception:

Unhandled exception at 0x7c812a5b in Spice_Invaders.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012f7ec..

I'm sure I'm doing something real stupid since I haven't worked with either pointers or vectors a lot, much less both at the same time. Any ideas?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!