int_type不是命名类型的挫败感

int_type not naming type frustration

本文关键字:类型 type int      更新时间:2023-10-16

我有一个虚拟方法类型的代码中的int_type是从这里改编的,这是代码给出编译错误的唯一情况:

‘int_type’ does not name a type
 int_type ThreadLogStream::overflow(int_type v)
 ^.

到目前为止的调试步骤

  • 如果我徘徊在QT创建者IDE中的int_type的任何其他实例上,它显示了traits_type::int_type std::basic_streambuf::int_type,这意味着int_type的所有其他实例都不应引起问题。

  • 根据此参考,INT_TYPE确实属于basic_streambuf,但称其为Traits::int_type不起作用。

  • 我试图输入问题变量,如下所示,但char_typetraits_type尚未识别。

threadlogstream.cpp

...
int_type ThreadLogStream::overflow(int_type a)
{
    int_type b = a; //This gives no errors
    return b;
}
...

threadlogstream.hpp

#include <iostream>
#include <streambuf>
#include <string>
//typedef std::basic_streambuf< char_type, traits_type> base_class;
//typedef typename base_class::int_type int_type;
class ThreadLogStream :  public QObject, std::basic_streambuf<char> {
    Q_OBJECT
public:
    ThreadLogStream(std::ostream &stream);
    ~ThreadLogStream();
protected:
    virtual int_type overflow(int_type v); // This gives no errors
    virtual std::streamsize xsputn(const char *p, std::streamsize n);
}

请帮忙 - 我在这方面丢了头发。

看来您的int_type应该代表std::basic_streambuf<>::int_type。在这种情况下

ThreadLogStream::int_type ThreadLogStream::overflow(int_type a)
{
  ...

即。为功能返回类型使用合格的名称。参数名称在类范围中查找(这就是为什么int_type在参数列表中填充罚款的原因)。但是返回类型在封闭范围中查找,这就是为什么您必须明确质量质量。

这就是它一直在C 中的方式。

但是,在thailting返回类型语法中的返回类型(自C 11以来可用)也在类范围中查找,这意味着您可以替代地执行此操作

auto ThreadLogStream::overflow(int_type a) -> int_type
{
  ...

看起来您需要使其成为尾随的返回类型,例如:

auto ThreadLogStream::overflow(int_type v) -> int_type {
    // ...
}

说明:int_type需要在ThreadLogStream的范围中查找,但是如果您有领先的返回类型,它将在命名空间范围中查找,因为它在之前您提到了名称ThreadLogStream::overflow,这会触发查找ThreadLogStream的范围。通过在合格的ID 之后放置返回类型,避免了此问题。