Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法理解模板和构造函数相关错误 #包括 #包括 #包括 使用名称空间std; 枚举类demo_初始化{a=1,b,c,d}; 阶级基础{ 公众: 基本(demo_initialize):最小化(initialize){} 受保护的: 演示初始化最小化; }; 模板 派生类:公共基 { 公众: 派生的(T&value,demo\u initialize initialize=demo\u initialize::a):基(initialize),mvvalue(value),mLen(sizeof(T)) { } void display(){ cout_C++_Templates_Inheritance_Constructor - Fatal编程技术网

C++ 无法理解模板和构造函数相关错误 #包括 #包括 #包括 使用名称空间std; 枚举类demo_初始化{a=1,b,c,d}; 阶级基础{ 公众: 基本(demo_initialize):最小化(initialize){} 受保护的: 演示初始化最小化; }; 模板 派生类:公共基 { 公众: 派生的(T&value,demo\u initialize initialize=demo\u initialize::a):基(initialize),mvvalue(value),mLen(sizeof(T)) { } void display(){ cout

C++ 无法理解模板和构造函数相关错误 #包括 #包括 #包括 使用名称空间std; 枚举类demo_初始化{a=1,b,c,d}; 阶级基础{ 公众: 基本(demo_initialize):最小化(initialize){} 受保护的: 演示初始化最小化; }; 模板 派生类:公共基 { 公众: 派生的(T&value,demo\u initialize initialize=demo\u initialize::a):基(initialize),mvvalue(value),mLen(sizeof(T)) { } void display(){ cout,c++,templates,inheritance,constructor,C++,Templates,Inheritance,Constructor,Base::mInitialize不会自动转换为行中的int 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&l

Base::mInitialize
不会自动转换为行中的
int

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)

cout枚举类值被视为一个新类型,没有对其底层类型进行隐式转换。如果要使用
std::ostream&operator您正试图调用
操作符,为什么要在
字符串上使用
sizeof
T&mValue;
应该是
T mValue;
您正在调用const字符串的构造函数并传递引用。枚举类demo_初始化{a=1,b,c,d};我从这条语句中删除了类,并且得到了所需的输出。但是,你能解释一下在派生类的构造函数中是如何进行初始化的吗?哪些初始化?哪些初始化?你提供了对
string\u to\u reference
demo\u initialize::c
Base
构造函数的引用ize
作为一个本地
派生的
参数
initialize
等于
demo\u initialize::c
string\u to_reference
变为
value
,它被分配给
mValue
并且
mLen
变为
sizeof(std::string)
,而不是字符串的长度(改用
value.length()
).Thanx tsuki这很有帮助。如果你再澄清一个疑问,那将是一个很大的帮助。我尝试在显示函数中打印'mLen'的值,每次我得到的结果都是8,而不管我输入的字符串是什么。我无法理解这个值8是什么,以及应该如何打印我输入的字符串的大小。@NixiN请参阅我最后一句话的最后一部分。是的,tsuki,我明白了,thanx。
cout << "Derived<T>{" << mValue << "; " << Base::mInitialize << "}";
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)
cout << "Derived<T>{" << mValue << "; " << Base::mInitialize << "}";
cout << "Derived<T>{" << mValue << "; " << static_cast<int>(Base::mInitialize) << "}";
void display() {
    auto mI = static_cast<std::underlying_type<demo_initialize>::type >(Base::mInitialize);
    std::cout << "Derived<T>{" << mValue << "; " << mI << "}";
  }
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); 
}