我在 Linux 上使用 g++ 编译器的简单代码时遇到问题,但在 Windows 上使用 Visual studio professional 2012 时出现许多错误。
代码:
string tmp = *path;
if(tmp.length() == 0)
*path = Name_;
else
*path = Name_ + '.' + tmp;
错误:
Error 1 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 'char'
程序将我指向 + 运算符。 我的内容还包括:
#include <iostream>
#include <stdio.h>
#include <string.h>
此外,我在 cout << 方面遇到了问题。尽管包含 iostream,但 Visual Studio 无法识别运算符 <<。
谢谢
最佳答案
您需要#include <string>
而不是string.h
(后者声明 C 字符串函数,如 strstr
、 strcmp
等)
如果您的操作数是std::string
,包括<string>
可能还会解决 <<
的问题不被认可。
编辑:顺便说一句,if (tmp.empty())
通常优于 if(tmp.length() == 0)
.
关于c++ - Visual Studio 错误 C2784,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20781193/