【问题标题】:reading string in c++在 C++ 中读取字符串
【发布时间】:2014-11-21 20:57:36
【问题描述】:

Dev C++ 编译器中,我可以编写并成功编译:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
  string method;
  cin >> method;
  return 0;
}

但是当我在Visual Studio 2013 (console app mode) 中编写上面的代码时,我得到了这个错误:

Error: no operator ">>" matches these operands
operand types are: std::istream >> std::string

编辑

Visual Studio:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  string method;
  cin >> method;
  return 0;
}

我明白错误告诉我什么。但为什么只有Visual Studio 2013

【问题讨论】:

  • 您的代码在我的 VS 2013 中编译没有问题。也许您在新项目中尝试一下?
  • 感谢关注,但这是我的错误。我忘记了项目第一行的“#”。

标签: c++ visual-studio-2013 dev-c++


【解决方案1】:

Visual Studio 中读取字符串的简单示例:

#include "stdafx.h"
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
    std::string str ;
    getline(cin, str);
    cout << str;
_   getch();
    return 0;
}

【讨论】:

    【解决方案2】:

    尝试将标题“stdafx.h”放在其他标题之前。

    #include "stdafx.h"
    

    【讨论】:

    • @coni 如果您确实将此标头放在其他两个标头之前,那么我不知道错误的原因是什么。
    • @coni Vlad 是正确的,如果您确实这样做了 #include "stdafx.h"#include &lt;iostream&gt;#include &lt;string&gt;,您将不会收到该错误。
    • @Cyber​​, @VladfromMoscow 好的,我明白你在说什么,但我做到了。我不明白为什么VS13 会给我这个错误。我可以发截图...
    • @coni:虽然不太可能,但这可能会有所帮助。
    • @coni 除了这个错误还有其他警告吗?
    猜你喜欢
    • 2011-11-01
    • 2012-10-19
    • 2018-05-02
    • 2023-04-04
    • 2014-11-22
    • 1970-01-01
    • 2017-02-16
    • 2012-11-28
    相关资源
    最近更新 更多