Unable to understand templates and constructor related errors

#include <iostream>
#include <vector>
#include <string>

using namespace std;

enum class demo_initialize { a = 1, b, c, d };

class Base {
public:
Base(demo_initialize initialize) : mInitialize(initialize) {}

protected:
demo_initialize mInitialize;
};

template <typename T>
class Derived : public Base 
{
public:
Derived(T &value, demo_initialize initialize = demo_initialize::a) : Base(initialize), mValue(value), mLen(sizeof(T)) 
{
}

void display() { 
    cout << "Derived<T>{" << mValue << "; " << Base::mInitialize << "}";
}
protected:
T &mValue;
size_t mLen;
};

int main()
{
string string_to_reference = "world";
Derived<string> obj(string_to_reference, demo_initialize::c);
obj.display();

}

      

I have tried this piece of code. when compiling, I got an error on this line:

cout << "Derived<T>{" << mValue << "; " << Base::mInitialize << "}";

      

and the error was

In instantiation of 'void Derived<T>::display() [with T =   std::basic_string<char>]':
37:17:   required from here
26:49: error: cannot bind 'std::basic_ostream<char>' lvalue to   'std::basic_ostream<char>&&'
In file included from /usr/include/c++/4.9/iostream:39:0,
             from 1:
/usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of   'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT,   _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>;   _Tp = demo_initialize]'
 operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)

      

I cannot figure out this error. Can anyone help me with solving this?

+3


source to share


3 answers


The values ​​of the enum class are treated as if they were a new type, without an implicit cast to its base type. If you want to use std::ostream& operator<<( std::ostream&, const demo_initialize& )

on them, you need to provide it or use:

void display() {
    auto mI = static_cast<std::underlying_type<demo_initialize>::type >(Base::mInitialize);
    std::cout << "Derived<T>{" << mValue << "; " << mI << "}";
  }

      



As for "why" there is such a strange error regarding rvalues ​​- blame the developers of the standard library;).

0




Base::mInitialize

is not automatically appended to int

in line

cout << "Derived<T>{" << mValue << "; " << Base::mInitialize << "}";

      



Using:

cout << "Derived<T>{" << mValue << "; " << static_cast<int>(Base::mInitialize) << "}";

      

+2


source


You are trying to call operator<<

for thread a demo_initialize

. You have not defined this operator anywhere in your code, and since it demo_initialize

is an enumeration that you have defined yourself, there is not one in the standard library for it operator<<

.

Solve this by running operator<<(std::ostream &os, demo_initialize out)

:

std::ostream& operator<<(std::ostream &os, demo_initialize out) {
    //Or some other logic, depending on the output format that you want.
    return os << static_cast<std::underlying_type_t<demo_initialize>>(out); 
}

      

+1


source







All Articles