书本中的代码未运行-旧语法或错误代码? 我通过游戏编程学习C++的开始。

书本中的代码未运行-旧语法或错误代码? 我通过游戏编程学习C++的开始。,c++,string,C++,String,我在书中举了一个例子: #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; s

我在书中举了一个例子:

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



}
#包括
#包括
#包括
使用名称空间std;
int main(){
string word1=“游戏”;
字符串字2(“超过”);
字符串字3(3,“!”);
字符串短语=word1+word2+word3;
字符串短语=word1+“”+word2+word3;
cout c:\program files(x86)\microsoft visual studio 11.0\vc\include\xutility(1801):请参见“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1801):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1801):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1801):请参阅“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14):错误C2784:“std::_数组常量迭代器std::operator+(_数组常量迭代器::差异类型,std:_数组常量迭代器)∶无法从“std::string”推断“std:_数组常量迭代器”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1662):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1662):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1662):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1662):请参阅“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14):错误C2784:“std::reverse\u迭代器std::operator+(\u Diff,const std::reverse\u iterator&”):无法从“std::string”推断“const std::reverse\u iterator&”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1226):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1226):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1226):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1226):请参阅“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14):错误C2784:“std::_revranitstd::operator+(_Diff,const std::_Revranit&”):无法从“std::string”推断“const std:_Revranit&”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1031):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1031):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1031):请参阅“std::operator+”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1031):请参阅“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14):错误C2676:二进制“+”:“std::string”未定义此运算符或到预定义运算符可接受的类型的转换
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15):错误C2374:“短语”:重新定义;多次初始化
1> c:\users\victor\documents\visualstudio 2012\projects\gametestexample\gametestexample\gameover.cpp(14):参见“短语”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15):错误C2784:“std::_String_iterator std::operator+(_String_iterator::difference_type,std:_String_iterator):”无法从“const char[1]”推断出“std:_String_iterator”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xstring(420):请参阅“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15):错误C2784:“std::_String_const_iterator std::operator+(_String_const_iterator::difference_type,std:_String_const_iterator)”:无法从“const char[1]推断出“std:_String_const_const_iterator”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xstring(288):请参见“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15):错误C2784:“std::move\u迭代器std::operator+(\u Diff,const std::move\u迭代器&”):无法从“const char[1]”推断出“const std::move\u迭代器&”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1947):请参见“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15):错误C2784:“std::_数组迭代器std::operator+(_数组迭代器::差异类型,std:_数组迭代器)::无法从“const char[1]”推断出“std:_数组迭代器”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xutility(1801):请参阅“std::operator+”的声明
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15):错误C2784:“std::_数组常量迭代器std::operator+(_数组常量迭代器::差异类型,std:_数组常量迭代器)∶无法从常量字符[1]推断出“std:_数组常量迭代器”的模板参数
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\
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 ==========