Skip to main content
gamedev.net
🔒 Locked

Using std::swap with vector?

Started by ebs2002 Aug 28, 2006 at 5:13 PM 5 replies 1.7k views
Original Post
ebs2002
ebs2002
The following code generates an error using VS2005 Express:
// TestVector.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	int a=0, b=1;
	std::swap<int> (a,b);
	system("Pause");
	return 0;
}
The error is C2825, and is from including Vector and using swap. If I do not use std::swap, I do not generate any errors. This is puzzling me, and I'm finding no solution on Google or anywhere else. The error does not occur in VS.NET 2003
Red Ant
Red Ant
Shouldn't you be #including algorithm?
Alpha_ProgDes
Alpha_ProgDes
According to this, you're using the wrong header. also if you're using to access std::swap, wouldn't you be swapping two vector elements as opposed to two primitive ints? I think the reason you're getting the error is because you're passing the wrong type into the function.
Beginner in Game Development?  Read here. And read here.  
Fruny
Fruny
only defines the swap operation for vectors (i.e. swap > for some T). If you want to swap two arbitrary entities, you'll want to include
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Simian Man
Simian Man
Get rid of the type specification on swap. ie change std::swap(a,b); to std::swap(a,b);

I have no idea why this gave you an error though [wow].

Edit: To the above posters, adding #include was my first guess, but it does not solve his problem!
ebs2002
ebs2002
I was trying algorithm before. It didn't work.

I'm using std::swap, not vector::swap, so that's not my problem either.

std::swap is just a basic template swap, and works just fine if I haven't included vector. When I include vector, it doesn't like vector being included. When I include vector but don't use swap, it likes vector just fine.
Fruny
Fruny
Quote:
Original post by Simian Man
Get rid of the type specification on swap. ie change std::swap(a,b); to std::swap(a,b);


You are correct.

Quote:
I have no idea why this gave you an error though [wow].


Because the template argument is forcing the selection of one of the container-swapping primary templates rather than relying on parameter type deduction.

Quote:
Edit: To the above posters, adding #include was my first guess, but it does not solve his problem!


It would address the problem if it were the only thing he were including. [smile]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

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

Sign in to reply to this topic.