【问题标题】:Eclipse, C++ method str() could not be resolvedEclipse,C++ 方法 str() 无法解析
【发布时间】:2017-03-25 21:31:32
【问题描述】:

使用默认的 str() 示例: http://www.cplusplus.com/reference/sstream/stringstream/str/

// stringstream::str
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream, std::stringbuf

int main () {
  std::stringstream ss;
  ss.str ("Example string");
  std::string s = ss.str();
  std::cout << s << '\n';
  return 0;
}

代码助手说str()无法解析,构建时没有与str()相关的错误

我在 Eclipse 中有CDT GCC Built-in Compiler Settings

并且未选中使用带有标志的全局提供程序:

"${COMMAND} ${FLAGS} -E -P -v -dD -std=c++11 "${INPUTS}"

我也尝试过来回更改工具链,但没有成功。

【问题讨论】:

  • 我尝试在 Visual Studio 2013 上运行相同的代码,它运行良好。
  • @AhsanMustafa 它看起来更像是 Eclipse IDE 错误,而不是语法或代码错误
  • @ABusyProgrammer 知道了:D
  • 我实际上已经先检查过了,但到目前为止我尝试了这些选项但没有运气,我实际上也可以运行该程序。

标签: c++ eclipse


【解决方案1】:

问题是由 g++ 头文件的结构引起的。在标题&lt;sstream&gt; 中只定义了“基本”模板类。在调用str() 方法时,我遇到了与ostringstream 相同的错误。 当我将代码中的变量类型更改为std::basic_ostringstream&lt;char&gt; 时,错误消失了。自动完成功能也再次起作用(它以前不起作用,这是 eclipse 扫描仪在某种程度上遇到问题的好兆头)。当然这不是一个真正好的解决方案,因为它使用了 basic_* 类型,但它是一种摆脱(无害)错误的方法。像ostringstream 这样的“官方”类型的typedef 在标题&lt;iosfwd&gt; 中前向声明。 当我在标准包含文件之后的代码中包含 typedef(在全局命名空间中)时:

typedef std::basic_ostringstream<char> ostringstream;

并使用此类型ostringstream 而不是std::ostringstream,错误也会消失。所以问题是由标准头文件&lt;iosfwd&gt;中的前向声明引起的(这并不意味着这些头文件有误,只是eclipse扫描仪对此感到困惑)。

我正在使用 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609 btw。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2012-08-05
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多