在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
?
答案 0 :(得分:2)
尝试放置标题&#34; stdafx.h&#34;在其他标题之前。
#include "stdafx.h"
答案 1 :(得分: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;
}