Reputation: 2571
I wish to use a template that has a default parameter l
in class A, but the program produces errors:
class B {
public:
B(){
...
}
}
template <int l = 1>
class A {
public:
A(const B& b){
...
}
}
int main(){
B b;
A(b) a; // error: missing template arguments before '(' token
A<5>(b) a; // error: expected ';' before 'a'
}
How can I fix this?
Upvotes: 0
Views: 104
Reputation: 96800
A
is a template, so you'll have to provide the template brackets irrespective of whether or not it is supplied with a default value.
A<> a(b);
If you don't want to use the template brackets, you can use a typedef
declaration to avoid it:
typedef A<> X;
The other errors you are experiencing are due to the fact that you did not end your class definitions with a semicolon.
Here is a working program -- http://ideone.com/occE71#view_edit_box
Upvotes: 0
Reputation: 157334
Just because it's a templated class doesn't change the initialization syntax:
A<5> a(b);
Using the defaulted parameter:
A<> a(b);
Upvotes: 1