So I am currently working on a project to make a library with all of the different data structures in C . Here I have declared a class Bag
:
template<typename Type>
class Bag
{
// ...
inline static const char* print_seperator = "\n";
public:
// ...
inline static void set_seperator(const char* new_seperator)
{
Bag::print_seperator = new_seperator;
}
}
Now this works fine, but when I try to use it in my main()
function like this:
sgl::Bag::set_seperator(", ");
This shows the following error:
Argument list for class template
sgl::Bag
is missing
..so I gave the argument list for the class template:
sgl::Bag<int>::set_seperator(", ");
..and it works fine.
But I don't want to type that out every time. Is there any way I can overcome this?
CodePudding user response:
You can use default template argument for the template type parameter Type
as shown below:
//use default argument for template type parameter "Type"
template<typename Type = int>
class Bag
{
// ...
inline static const char* print_seperator = "\n";
public:
// ...
inline static void set_seperator(const char* new_seperator)
{
Bag::print_seperator = new_seperator;
}
};
int main()
{
//no need to specify int
Bag<>::set_seperator(", "); //angle bracket still needed
return 0;
}