Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.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

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++ 如何在类中包含模板成员?_C++_Templates - Fatal编程技术网

C++ 如何在类中包含模板成员?

C++ 如何在类中包含模板成员?,c++,templates,C++,Templates,我试图创建一个包含模板对象成员的类。比如说 mt_queue_c<int> m_serial_q("string"); mt_queue_c m_serial_q(“字符串”); 但是,当我这样做时,它无法编译。如果我将这一行移到类定义的之外,使其成为一个全局对象,它就可以很好地编译 我将代码压缩成如下所示的最小可能的失败单元(是的,它没有意义,因为缺少其他成员变量和函数…) #包括 #包括 #包括 #包括 #包括 样板 类别mt_队列c{ 公众: 显式mt_queue_c

我试图创建一个包含模板对象成员的类。比如说

mt_queue_c<int>    m_serial_q("string");
mt_queue_c m_serial_q(“字符串”);
但是,当我这样做时,它无法编译。如果我将这一行移到类定义的之外,使其成为一个全局对象,它就可以很好地编译

我将代码压缩成如下所示的最小可能的失败单元(是的,它没有意义,因为缺少其他成员变量和函数…)

#包括
#包括
#包括
#包括
#包括
样板
类别mt_队列c{
公众:
显式mt_queue_c(const std::string和name,
常量容器&cont=Container()):
m_名称(名称),
m_c(续)
{};
虚~mt_队列_c(void){};
受保护的:
//队列的名称,在错误消息中使用。
std::字符串m_名称;
//管理队列的容器。
集装箱集装箱;
};
//测试的字符串对象
std::字符串test2(“foobar”);
//两个测试表明它可以作为一个全局
mt_队列_c外部1(“字符串”);
mt_队列_c外部2(测试2);
//两次尝试将对象作为成员对象包含失败。
课堂废话{
mt_queue_c m_serial_q(“字符串”);//这是48
mt_queue_c m_serial_q2(test2);//这是50
};
//添加main仅仅是因为。
int main()
{
std::cout试试这个:

class blah { 
    mt_queue_c<int>    m_serial_q;    // this is 48 

    mt_queue_c<char>   m_serial_q2;      // this is 50 

    blah() : m_serial_q("string"), m_serial_q2(test2)
    {

    }
}; 
class废话{
mt_queue_c m_serial_q;//这是48
mt_queue_c m_serial_q2;//这是50
blah():m_serial_q(“字符串”),m_serial_q2(test2)
{
}
}; 
试试这个:

class blah { 
    mt_queue_c<int>    m_serial_q;    // this is 48 

    mt_queue_c<char>   m_serial_q2;      // this is 50 

    blah() : m_serial_q("string"), m_serial_q2(test2)
    {

    }
}; 
class废话{
mt_queue_c m_serial_q;//这是48
mt_queue_c m_serial_q2;//这是50
blah():m_serial_q(“字符串”),m_serial_q2(test2)
{
}
}; 

为类创建默认构造函数
等等

并在构造函数初始化列表中初始化模板对象的值

为类创建默认构造函数
blah

并在构造函数初始化列表中初始化模板对象的值这与模板无关-不能在类定义中直接初始化非静态成员(C++03,§9.2/4):

仅当成员声明符声明了
const
integral或
const
枚举类型的
static
成员(9.4)时,才能包含常量初始值设定项,请参见9.4.2

如果要显式初始化数据成员,请使用构造函数初始值设定项列表:

blah::blah() : m_serial_q("string") {}

这与模板无关-不能在类定义中直接初始化非静态成员(C++03,§9.2/4):

仅当成员声明符声明了
const
integral或
const
枚举类型的
static
成员(9.4)时,才能包含常量初始值设定项,请参见9.4.2

如果要显式初始化数据成员,请使用构造函数初始值设定项列表:

blah::blah() : m_serial_q("string") {}

嘎!我在找一条斑马线,专注于晦涩难懂的错误消息,并认为这一定是模板的特殊之处。我完全忽略了嵌套类的基础知识。感谢所有人的评论。我不会再犯这个错误了。嘎!我在找一条斑马线,专注于晦涩难懂的错误消息,并认为它一定是模板有些特别。我完全忽略了嵌套类的基础知识。感谢大家的评论。我不会再犯这个错误了。