Eternal Learner
Eternal Learner

Reputation: 3870

sorting a vector os strings c++ visual studio 2010 - error C2784 could not deduce template argument

I wanted to confirm if this is a bug in visual studio 2010. If yes then can any settings be changed? or how do I solve this?

#include <iostream>
#include <vector>
#include <algorithm>

std::vector<std::string> test;

test.push_back("hello"); test.push_back("ABC");

std::sort(test.begin(), test.end());

Well this happened because I did not include the std::string header .

Upvotes: 1

Views: 977

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490438

My first guess would be that you forgot to include a header (probably <string>). You can end up with some minimal forward declaration (or something like that) from a different header that lets part of the code compile, but other parts break in strange ways leading to misleading error messages.

In a quick test, this compiles with VS 2008 through 2012:

#include <vector>
#include <string>
#include <algorithm>

int main() {

    std::vector<std::string> test;

    test.push_back("hello");
    test.push_back("ABC");

    std::sort(test.begin(), test.end());
}

Upvotes: 4

Caribou
Caribou

Reputation: 2081

It compiles fine....

#include "stdafx.h"
#include <string>
#include <vector>
#include <algorithm>

int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::string> test;

test.push_back("hello");
test.push_back("ABC");

std::sort(test.begin(), test.end());
return 0;
}

Upvotes: 0

Related Questions