3

I have a virtual method type int_type in code being adapted from here that is the only case where the code gives the compile error:

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

Debugging steps so far

  • If I hover over any other instance of int_type in the Qt Creator IDE, it shows traits_type::int_type std::basic_streambuf::int_type, implying every other instance of int_type should not be causing problems.

  • According to this reference, the int_type does indeed belong to basic_streambuf, but calling it Traits::int_type did not work.

  • I attempted to typedef the problem variable, as demonstrated in the typedefs below, but char_type and traits_type were not recognized.

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);
}

Please help - I am losing hair over this.

Community
  • 1
  • 1
Graeme Rock
  • 601
  • 5
  • 21
  • This is not supposed to compile if `int_type` is not seen in global namespace. You are supposed to use qualified name for return type when a function is declared outside of the class. – AnT stands with Russia Dec 05 '16 at 21:56

2 Answers2

2

It appears that your int_type is supposed to stand for std::basic_streambuf<>::int_type. In that case in an out-of-class member definition you are supposed to write

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

i.e. use a qualified name for the function return type. Parameter names are looked up in class scope (which is why a mere int_type compiles fine in the parameter list). But return types are looked up in the enclosing scope, which is why you have to quality it explicitly.

That is how it has always been in C++.

However, the return type in trailing return type syntax (available since since C++11) is also looked up in class scope, which means that you can alternatively do this

auto ThreadLogStream::overflow(int_type a) -> int_type
{
  ...
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

Looks like you need to make it a trailing return type, like so:

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

Explanation: int_type needs to be looked up in the scope of ThreadLogStream, but if you have a leading return type, it will be looked up in namespace scope since it comes before you mention the name ThreadLogStream::overflow, which triggers lookup in the scope of ThreadLogStream. By placing the return type after the qualified-id, this problem is avoided.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312