A
aaragon
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<ClassB> CA;
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<cb> CA;
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
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<ClassB> CA;
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<cb> CA;
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