Reputation: 1348
Inside the declaration of the class Density
, I built these member function:
class Density {
public:
template <typename Container>
void printStream (Container<Point>::iterator lo, Container<Point>::iterator hi);
......
};
In the cpp file:
template <typename Container>
void Density::printStream (Container<Point>::iterator lo, Container<Point>::iterator hi)
{
...
}
But get these errors when trying to compile:
src/../include/density.hpp:166:23: error: 'Container' is not a template
src/../include/density.hpp:166:50: error: expected unqualified-id before 'lo'
src/../include/density.hpp:166:50: error: expected ')' before 'lo'
src/../include/density.hpp:166:50: error: expected initializer before 'lo'
src/density.cpp: In member function 'void Density::startAlgorithm()':
src/density.cpp:291:43: error: 'printStream' was not declared in this scope
src/density.cpp: At global scope:
src/density.cpp:327:28: error: 'Container' is not a template
src/density.cpp:327:55: error: expected unqualified-id before 'lo'
src/density.cpp:327:55: error: expected ')' before 'lo'
src/density.cpp:327:55: error: expected initializer before 'lo'
What should I modify? And also, why, as I'd like to understand this issue.
Upvotes: 1
Views: 649
Reputation: 393769
NB. As commented, you might not be aware of the implications that using templates has on the visibility of template definitions in the header files. Let me point you to the c++-faq entry for that: Why can templates only be implemented in the header file?
Using a template template argument:
template <template <typename...> class Container>
void Density::printStream ()
{
typename Container<Point>::iterator lo;
typename Container<Point>::iterator hi;
}
What you are trying to do, seems to me to be impossible, since the iterator arguments are non-deducible context, so you'd end up explicitely specifying the container type anyways:
density_instance.printStream<std::vector>(it1, it2);
Note, however, that's not really a problem, since you probably don't really care about the precise type of the container. The idiomatic way would be:
template <typename It>
void printStream (It lo, It hi);
which you can freely call using
std::vector<int> v { 1,2,3 };
density_instance.printStream(begin(v), end(v));
But also with a non-class container, since the iterators are what matters:
const int a[] = { 1,2,3 };
density_instance.printStream(std::begin(a), std::end(b));
Upvotes: 4