0

我正在通过游戏编程学习开始 C++

在书中我有这个例子:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main (){

    string word1 = "Game";
    string word2("Over");
    string word3(3, '!');

    string phrase= word1 + word2 + word3;
    string phrase= word1 + "" + word2 + word3;

    cout<< " The phrase is: " << phrase << "\n \n";
    cout<< " The phrase has " << phrase.size() << "on it! \n\n";

    cout<< " The character at position 0 is " << phrase[0] << "on it! \n\n";

    cout<< "Changing the character at position 0 \n\n";
    phrase[0] = 'V';
    cout << "The phrase now is " << phrase << "\n\n";


    for( unsigned int i=0; i < phrase.size() ; i++){
        cout << " Character at position "<< i << "is : " << phrase[i] << "\n" ;
    }
    cout << "The word  'Over' begin at " << phrase.find("Over") << endl;

    if (phrase.find("eggplant")==string::npos)
    {
        cout<<"'Eggplant' is not in the phrase"<<endl;
    }
    phrase.erase(4,5);
    cout<<"phrase now is" << phrase << endl;

    phrase.erase();

    if(phrase.empty())
    {
        cout << "phrase is empty" << endl;

    }


    getchar();



}

当我尝试运行代码时,调试会返回:

1>------ Build started: Project: GameTestExample, Configuration: Debug Win32 ------
1>  gameover.cpp
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_String_iterator<_Mystr> std::operator +(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_String_const_iterator<_Mystr> std::operator +(_String_const_iterator<_Mystr>::difference_type,std::_String_const_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_const_iterator<_Mystr>' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::move_iterator<_RanIt> std::operator +(_Diff,const std::move_iterator<_RanIt> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_Array_iterator<_Ty,_Size> std::operator +(_Array_iterator<_Ty,_Size>::difference_type,std::_Array_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_iterator<_Ty,_Size>' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_Array_const_iterator<_Ty,_Size> std::operator +(_Array_const_iterator<_Ty,_Size>::difference_type,std::_Array_const_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_const_iterator<_Ty,_Size>' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2676: binary '+' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2374: 'phrase' : redefinition; multiple initialization
1>          c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14) : see declaration of 'phrase'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_String_iterator<_Mystr> std::operator +(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_String_const_iterator<_Mystr> std::operator +(_String_const_iterator<_Mystr>::difference_type,std::_String_const_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_const_iterator<_Mystr>' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::move_iterator<_RanIt> std::operator +(_Diff,const std::move_iterator<_RanIt> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_Array_iterator<_Ty,_Size> std::operator +(_Array_iterator<_Ty,_Size>::difference_type,std::_Array_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_iterator<_Ty,_Size>' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_Array_const_iterator<_Ty,_Size> std::operator +(_Array_const_iterator<_Ty,_Size>::difference_type,std::_Array_const_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_const_iterator<_Ty,_Size>' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const char [1]'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2676: binary '+' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(17): error C2088: '<<' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(20): error C2088: '[' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(23): error C2088: '[' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(24): error C2088: '<<' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(28): error C2088: '[' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(37): error C2088: '<<' : illegal for class
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

所以,我的意思是,到底发生了什么?无法意识到这是否是诸如“旧语法”之类的问题,我以前从未见过类似的东西,或者代码是否错误(我也不敢相信,没有人会在书中做这样的失败)

我使用 Microsoft Visual Studio Premium 20112 作为开发环境

4

1 回答 1

3

也用#include <string>

于 2013-03-25T09:47:30.167 回答