【问题标题】:Trouble with identifying why the IDE is saying undeclared identifier in C++无法识别 IDE 为何在 C++ 中显示未声明的标识符
【发布时间】:2018-04-07 04:57:08
【问题描述】:

我现在有一堂课,里面有这几行代码。我正在努力解决的是我的 IDE 中写的函数“使用未声明的标识符作为根”

这是为什么?

template<typename T>
    class X
    {
    public:
        const void write(std::ostream & output);

    private:
        std::unique_ptr< TreeNode<Ty> > root;


    };


    const void write(std::ostream & output)
    {
        root->write(output);
    }

使用模板编辑以显示更完整的代码范围。

【问题讨论】:

  • const void的返回类型是什么? void 返回类型意味着函数不返回值。 const 如何应用于未返回的值?也许您想在之后放置constvoid write(std::ostream&amp; output) const;
  • @ThomasMatthews 最后的 const 是什么意思?我想要实现的是编写一个使用 ostream 引用在根上调用 write 的函数。将 write 标记为 const。
  • 方法末尾的const 表示该方法不会写入任何类成员。
  • “使用未声明的根标识符”:它真的是这么说的吗?没有意义。确定不是“使用未声明的标识符:root”?准确无误。

标签: c++ identifier


【解决方案1】:

尝试使用作用域解析运算符告诉编译器您的write 函数属于class X

const void X::write(std::ostream & output)
{
    root->write(output);
}

编辑 1:模板
使用模板,语法变为:

template<typename T>
const void
X<T>::write(std::ostream & output)
{
    root->write(output);
}

【讨论】:

  • 当我这样做时它告诉我 X 不是类、命名空间或枚举
  • @Scanner write的定义是在X的定义之后并且在同一个文件中吗?或者在包含X定义的文件的#include之后?
  • @aschepler write 的定义在 X 的定义之后并且在同一个文件中。它们都在模板 下。写入已在类中的 public 下声明为 const void write(std::ostream & output);
  • @Scanner 模板参数很可能与您的问题有关。您可以编辑您的问题以准确显示您的意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多