1. 程式人生 > >VC編譯器下如何解決error C2679、error C2676、error C2784、fatal error C1903錯誤

VC編譯器下如何解決error C2679、error C2676、error C2784、fatal error C1903錯誤

最近編寫了一小段程式,在GNU編譯器下能編譯通過,但在VC編譯器下卻顯示一大堆錯誤。檢視編譯資訊,其中錯誤提示如下:
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
fatal error C1903: unable to recover from previous error(s); stopping compilation

這段程式如下所示,程式不斷從stdin中讀入單詞,若該單詞是第一次出現,則輸出該單詞,否則忽略。注:輸入中單詞以空格間隔,標點符號僅出現在單詞後面,且和單詞字母間沒有其他字元。

例如輸入:where there is a will, there is a way.

則應輸出:where there is a will way

#include <iostream>
#include <cstring>
#include <set>
using namespace std;

int main()
{
    char str[100];
    string word;
    set<string> words;
    while(cin >> str){
	int n = strlen(str) - 1;
	char c = str[n];
	if (!isalpha(c))
	    str[n] = 0;
        word = str;
        if (words.find(word) == words.end()){
            cout << word << " ";
            words.insert(word);
        }           
    }
    return 0;
}

通過定位錯誤,是其中cout << word << " "一行程式碼有誤。經過一番嘗試之後,將該行程式碼修改為 cout << word.c_str() << " "; 完整程式碼如下:

#include <iostream>
#include <cstring>
#include <set>
using namespace std;

int main()
{
    char str[100];
    string word;
    set<string> words;
    while(cin >> str){
	int n = strlen(str) - 1;
	char c = str[n];
	if (!isalpha(c))
	    str[n] = 0;
        word = str;
        if (words.find(word) == words.end()){
            cout << word.c_str() << " ";
            words.insert(word);
        }           
    }
    return 0;
}

但此次錯誤資訊卻更多了,且大多是C2784錯誤:

error C2784: 'bool __cdecl std::operator ==(const class std::multiset<_K,_Pr,_A> &,const class std::multiset<_K,_Pr,_A> &)' : could not deduce template argument for 'const class std::multiset<_K,_Pr,_A> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

而且這次定位錯誤,卻定位了到了set模板檔案。無奈之下只好繼續修改程式碼,將set修改為vector。修改後程式碼如下:

#include <iostream>
#include <cstring> 
#include <vector>
using namespace std;

bool my_find(vector<string> &words, string &str)
{
	for(vector<string>::iterator iter = words.begin(); 
	iter != words.end(); ++iter)
		if((*iter) == str)
			return true;
	return false;
}

int main()
{
    char str[100];
    string word;
    vector<string> words;
    while(cin >> str){
		int n = strlen(str) - 1;
		char c = str[n];
		if (!isalpha(c))
			str[n] = 0;
		word = str;
        if (!my_find(words, word)){
            cout << word.c_str() << " ";
            words.push_back(word);
        }           
    }
    return 0;
}
但是到了這一步,編譯器依然還是報C2784錯誤。但這個程式已經將編譯錯誤定位到了if((*iter) == str)這一行。於是,我做了最後的修改。程式碼如下:
#include <iostream>
#include <cstring> 
#include <vector>
using namespace std;

bool my_find(vector<string> &words, string &str)
{
	for(vector<string>::iterator iter = words.begin(); 
	iter != words.end(); ++iter)
		//if((*iter) == str)
		if(strcmp((*iter).c_str(), str.c_str()) == 0)
			return true;
	return false;
}

int main()
{
	char str[100];
    string word;
    vector<string> words;
    while(cin >> str){
		int n = strlen(str) - 1;
		char c = str[n];
		if (!isalpha(c))
			str[n] = 0;
		word = str;
        if (!my_find(words, word)){
            cout << word.c_str() << " ";
            words.push_back(word);
        }           
    }
    return 0;
}


到了這一步,程式碼終於編譯通過。我也終於鬆了口氣,但心頭還是滿是疑惑,為什麼這些程式碼在GNU的編譯器下可以正常編譯,在VC的編譯器下就是不行呢?突然我靈光一閃!為何不上網搜一搜,看看別人是怎麼解決的呢?於是我搜索了錯誤提示資訊。如下一個帖子中的回覆說明了這個問題:
http://bbs.csdn.net/topics/40256358
其中三樓和五樓回答了這一問題。

答案就是:加 #include<string>

至此,問題告一段路。回到我的第一個解決方案,在新增完整的標頭檔案後,終於在GNU和VC的編譯器下都順利通過了。