模板类的typedef总是默认构造? [英] typedef of template classes are always default constructed?
问题描述
大家好。我有一个简单的问题似乎微不足道,但我不能让b $ b让它起作用。我有一个类作为模板参数,
另一个类。这个想法如下:
#include< iostream>
使用命名空间std;
模板< class ClassB>
class ClassA
{
int a;
ClassB cb;
public:
void print(){
cout<<""在A类中打印< ;< endl;
}
};
class ClassB
{
int s_;
public:
ClassB(int s):s_(s){}
};
int main()
{
typedef ClassA< ClassBCA;
CA ca;
ca.print();
返回0;
}
当我尝试编译此代码时,我会收到
消息:
main2.cxx:在构造函数''ClassA< ClassB>中:: ClassA()'':
main2.cxx:7:错误:没有匹配函数来调用
''ClassB :: ClassB()''
main2。 cxx:24:注意:候选者是:ClassB :: ClassB(int)
main2.cxx:19:注意:ClassB :: ClassB(const ClassB&)
main2 .cxx:在函数''int main()''中:
main2.cxx:30:注意:合成方法''ClassA< ClassB> :: ClassA()''
首先需要这个
当然因为我没有提供默认构造函数。如果我将main()中的
更改为
typedef ClassA< ClassB(6)CA;
我收到错误:
main2.cxx:在函数''int main()''中:
main2.cxx:29:错误:a对构造函数的调用不能出现在
常量表达式中
main2.cxx:29:错误:模板参数1无效
main2。 cxx:29:错误:在'';''之前声明中的无效类型令牌
main2.cxx:31:错误:在''ca''中请求成员''print'',其中
属于非类型类型''main :: CA''
此外,如果我将代码更改为
ClassB cb(6);
typedef ClassA< cbCA;
我收到同样的错误。我究竟做错了什么?我不希望
类在类型定义中默认构造。谢谢
的帮助。
aa
Hello all. I have a simple question that seems trivial but I can''t
make it to work. I have a class that takes as a template argument,
another class. The idea is as follows:
#include <iostream>
using namespace std;
template <class ClassB>
class ClassA
{
int a;
ClassB cb;
public:
void print(){
cout<<"print within class A"<<endl;
}
};
class ClassB
{
int s_;
public:
ClassB(int s) : s_(s) {}
};
int main()
{
typedef ClassA<ClassBCA;
CA ca;
ca.print();
return 0;
}
The thing is that when I try to compile this code, I receive the
message:
main2.cxx: In constructor ''ClassA<ClassB>::ClassA()'':
main2.cxx:7: error: no matching function for call to
''ClassB::ClassB()''
main2.cxx:24: note: candidates are: ClassB::ClassB(int)
main2.cxx:19: note: ClassB::ClassB(const ClassB&)
main2.cxx: In function ''int main()'':
main2.cxx:30: note: synthesized method ''ClassA<ClassB>::ClassA()''
first required here
Of course because I don''t provide a default constructor. If I change
within main() to something like
typedef ClassA<ClassB(6)CA;
I receive the error:
main2.cxx: In function ''int main()'':
main2.cxx:29: error: a call to a constructor cannot appear in a
constant-expression
main2.cxx:29: error: template argument 1 is invalid
main2.cxx:29: error: invalid type in declaration before '';'' token
main2.cxx:31: error: request for member ''print'' in ''ca'', which
is of non-class type ''main::CA''
Furthermore, if I change the code to
ClassB cb(6);
typedef ClassA<cbCA;
I receive the same error. What am I doing wrong? I don''t want the
class be to be default constructed within the type definition. Thanks
for your help.
aa
推荐答案
aaragon写道:
aaragon wrote:
大家好。我有一个简单的问题似乎微不足道,但我不能让b $ b让它起作用。我有一个类作为模板参数,
另一个类。这个想法如下:
#include< iostream>
使用命名空间std;
模板< class ClassB>
class ClassA
{
int a;
ClassB cb;
public:
void print(){
cout<<""在A类中打印< ;< endl;
}
};
class ClassB
{
int s_;
public:
ClassB(int s):s_(s){}
};
int main()
{
typedef ClassA< ClassBCA;
CA ca;
ca.print();
返回0;
}
当我尝试编译此代码时,我会收到
消息:
main2.cxx:在构造函数''ClassA< ClassB>中:: ClassA()'':
main2.cxx:7:错误:没有匹配函数来调用
''ClassB :: Cl assB()''
main2.cxx:24:注意:候选人是:ClassB :: ClassB(int)
main2.cxx:19:注意:ClassB :: ClassB(const ClassB&)
main2.cxx:在函数''int main()''中:
main2.cxx:30:注意:合成方法''ClassA< ; ClassB> :: ClassA()''
这里首先需要
当然因为我没有提供默认构造函数。如果我将main()中的
更改为
typedef ClassA< ClassB(6)CA;
我收到错误:
main2.cxx:在函数''int main()''中:
main2.cxx:29:错误:a对构造函数的调用不能出现在
常量表达式中
main2.cxx:29:错误:模板参数1无效
main2。 cxx:29:错误:在'';''之前声明中的无效类型令牌
main2.cxx:31:错误:在''ca''中请求成员''print'',其中
属于非类型类型''main :: CA''
此外,如果我将代码更改为
ClassB cb(6);
typedef ClassA< cbCA;
我收到同样的错误。我究竟做错了什么?我不希望
类在类型定义中默认构造。谢谢
的帮助。
Hello all. I have a simple question that seems trivial but I can''t
make it to work. I have a class that takes as a template argument,
another class. The idea is as follows:
#include <iostream>
using namespace std;
template <class ClassB>
class ClassA
{
int a;
ClassB cb;
public:
void print(){
cout<<"print within class A"<<endl;
}
};
class ClassB
{
int s_;
public:
ClassB(int s) : s_(s) {}
};
int main()
{
typedef ClassA<ClassBCA;
CA ca;
ca.print();
return 0;
}
The thing is that when I try to compile this code, I receive the
message:
main2.cxx: In constructor ''ClassA<ClassB>::ClassA()'':
main2.cxx:7: error: no matching function for call to
''ClassB::ClassB()''
main2.cxx:24: note: candidates are: ClassB::ClassB(int)
main2.cxx:19: note: ClassB::ClassB(const ClassB&)
main2.cxx: In function ''int main()'':
main2.cxx:30: note: synthesized method ''ClassA<ClassB>::ClassA()''
first required here
Of course because I don''t provide a default constructor. If I change
within main() to something like
typedef ClassA<ClassB(6)CA;
I receive the error:
main2.cxx: In function ''int main()'':
main2.cxx:29: error: a call to a constructor cannot appear in a
constant-expression
main2.cxx:29: error: template argument 1 is invalid
main2.cxx:29: error: invalid type in declaration before '';'' token
main2.cxx:31: error: request for member ''print'' in ''ca'', which
is of non-class type ''main::CA''
Furthermore, if I change the code to
ClassB cb(6);
typedef ClassA<cbCA;
I receive the same error. What am I doing wrong? I don''t want the
class be to be default constructed within the type definition. Thanks
for your help.
模板可以将类型或(整数)常量表达式作为
参数,这些参数必须在* compile time *。
创建像cb或ClassB(6)这样的对象实例都是运行时
操作。您必须提供默认构造函数(或现有构造函数的默认
参数),或者向ClassA<>的
构造函数传递必要的参数以进行初始化B级。
干杯! --M
Templates can take types or (integral) constant expressions as
parameters, and these parameters must be available at *compile time*.
Creating an object instance like cb or ClassB(6) are both run-time
operations. You must either provide a default constructor (or a default
parameter on your existing constructor), or pass to ClassA<>''s
constructor the necessary parameters to initalize class B.
Cheers! --M
aaragon写道:
aaragon wrote:
你好所有。我有一个简单的问题似乎微不足道,但我不能让b $ b让它起作用。我有一个类作为模板参数,
另一个类。这个想法如下:
#include< iostream>
使用命名空间std;
模板< class ClassB>
class ClassA
{
int a;
ClassB cb;
public:
void print(){
cout<<""在A类中打印< ;< endl;
}
};
class ClassB
{
int s_;
public:
ClassB(int s):s_(s){}
};
int main()
{
typedef ClassA< ClassBCA;
CA ca;
ca.print();
返回0;
}
当我尝试编译此代码时,我会收到
消息:
main2.cxx:在构造函数''ClassA< ClassB>中:: ClassA()'':
main2.cxx:7:错误:没有匹配函数来调用
''ClassB :: Cl assB()''
main2.cxx:24:注意:候选人是:ClassB :: ClassB(int)
main2.cxx:19:注意:ClassB :: ClassB(const ClassB&)
main2.cxx:在函数''int main()''中:
main2.cxx:30:注意:合成方法''ClassA< ; ClassB> :: ClassA()''
这里首先需要
当然因为我没有提供默认构造函数。如果我将main()中的
更改为
typedef ClassA< ClassB(6)CA;
我收到错误:
main2.cxx:在函数''int main()''中:
main2.cxx:29:错误:a对构造函数的调用不能出现在
常量表达式中
main2.cxx:29:错误:模板参数1无效
main2。 cxx:29:错误:在'';''之前声明中的无效类型令牌
main2.cxx:31:错误:在''ca''中请求成员''print'',其中
属于非类型类型''main :: CA''
此外,如果我将代码更改为
ClassB cb(6);
typedef ClassA< cbCA;
我收到同样的错误。我究竟做错了什么?我不希望
类在类型定义中默认构造。谢谢
的帮助。
aa
Hello all. I have a simple question that seems trivial but I can''t
make it to work. I have a class that takes as a template argument,
another class. The idea is as follows:
#include <iostream>
using namespace std;
template <class ClassB>
class ClassA
{
int a;
ClassB cb;
public:
void print(){
cout<<"print within class A"<<endl;
}
};
class ClassB
{
int s_;
public:
ClassB(int s) : s_(s) {}
};
int main()
{
typedef ClassA<ClassBCA;
CA ca;
ca.print();
return 0;
}
The thing is that when I try to compile this code, I receive the
message:
main2.cxx: In constructor ''ClassA<ClassB>::ClassA()'':
main2.cxx:7: error: no matching function for call to
''ClassB::ClassB()''
main2.cxx:24: note: candidates are: ClassB::ClassB(int)
main2.cxx:19: note: ClassB::ClassB(const ClassB&)
main2.cxx: In function ''int main()'':
main2.cxx:30: note: synthesized method ''ClassA<ClassB>::ClassA()''
first required here
Of course because I don''t provide a default constructor. If I change
within main() to something like
typedef ClassA<ClassB(6)CA;
I receive the error:
main2.cxx: In function ''int main()'':
main2.cxx:29: error: a call to a constructor cannot appear in a
constant-expression
main2.cxx:29: error: template argument 1 is invalid
main2.cxx:29: error: invalid type in declaration before '';'' token
main2.cxx:31: error: request for member ''print'' in ''ca'', which
is of non-class type ''main::CA''
Furthermore, if I change the code to
ClassB cb(6);
typedef ClassA<cbCA;
I receive the same error. What am I doing wrong? I don''t want the
class be to be default constructed within the type definition. Thanks
for your help.
aa
什么阻止你提供默认的ctor在ClassB?
class ClassB
{
int s_;
public:
ClassB():s_(0){}
};
//或者A类中的一个:
模板< class T>
class ClassA
{
int a;
T t;
public:
ClassA():a(0),t(0){}
void print() const {...}
};
顺便说一下,从来没有,从来没有一个好主意_not_来初始化所有的
你的会员。
即使以后会改变这些。
Whats preventing you from providing a default ctor in ClassB?
class ClassB
{
int s_;
public:
ClassB() : s_(0) { }
};
// or alternatively, one in ClassA:
template <class T>
class ClassA
{
int a;
T t;
public:
ClassA() : a(0), t(0) { }
void print() const { ... }
};
By the way, its never, never, a good idea _not_ to initialize ALL of
your members.
Even when these will be changed afterwards.
有人可以解释为什么上面的程序在使用指针时有效吗?
could anyone explain why the above program works if pointer is used ?
这篇关于模板类的typedef总是默认构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!