簡體   English   中英

排序矢量os字符串c ++ visual studio 2010 - 錯誤C2784無法推斷出模板參數

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

我想確認這是否是Visual Studio 2010中的錯誤。如果是,則可以更改任何設置嗎? 或者我該如何解決這個問題?

#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());

這發生的原因是因為我沒有包含std :: string標頭。

我的第一個猜測是你忘了包含一個標題(可能是<string> )。 你可以從一個不同的頭文件中得到一些最小的前向聲明(或類似的東西),這些頭文件允許部分代碼編譯,但是其他部分以奇怪的方式破壞,導致誤導性的錯誤消息。

在快速測試中,這與VS 2008到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());
}

編譯好......

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM