c++ - Visual Studio 错误 C2784

标签 c++ visual-studio-2012

我在 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 字符串函数,如 strstrstrcmp 等)

如果您的操作数是std::string ,包括<string>可能还会解决 << 的问题不被认可。

编辑:顺便说一句,if (tmp.empty())通常优于 if(tmp.length() == 0) .

关于c++ - Visual Studio 错误 C2784,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20781193/

相关文章:

c++ - 如何使用 boost::xpressive static 在语义操作中引用可选的子匹配?

c++ - COM 调度接口(interface)事件

c# - 从常规注释生成 XML 注释

.net - 如何确定执行期间来自异步 httpclient 的并发请求数?

visual-studio-2012 - Visual Studio 2012 HelpViewer 2.0 仍然损坏 : location specified installing content invalid

asp.net-mvc - 在VS2012中调试MVC应用程序尝试启动IIS Express两次

c++ - 将 const 参数作为参数传递时的神秘错误消息

c++ - 使用继承和模板的数据库接口(interface)

c++ - 无法从 GetSystemTime() 获取毫秒数

c++ - 将 MBCS 和 Unicode 库链接在一起