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的編譯器下都順利通過了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章