c++ - 书中的代码未运行 - 旧语法或错误代码?

标签 c++ string

我正在通过游戏编程学习 Beginning 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 作为开发环境

最佳答案

使用 #include <string>也是。

关于c++ - 书中的代码未运行 - 旧语法或错误代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611801/

相关文章:

java - 将 arraylist int 转换为字符串

java - 我正在使用 toString() 方法将 map 转换为字符串。有没有办法可以将字符串转换回 map ?

javascript - 通过在字符串中捕获类/id 来选择不同的元素是否被认为是一种不好的做法?

mysql - 如何删除MySQL数据库中不再需要的字符串?

c++ - 模板、函数重载及其出现顺序

c++ - CLOCKS_PER_SEC 在不同操作系统中的行为

c++ - 优雅地捕获 LoadLibrary() 错误

C++ 三元运算符执行条件

c - 要求存储字符串,并存储在带打印的链表中,scanf

c++ - 为什么类内初始化程序中不允许使用 double