Re: compiler error
On May 14, 5:06 am, Aston Martin <masoom.sha...@gmail.com> wrote:
On May 14, 1:26 pm, Zeppe
<zeppe@.remove.all.this.long.comment.email.it> wrote:
Aston Martin wrote:
template<typename T, int dummy>
friend ostream& operator << ( ostream& outStream, const
SomeClass<T, dummy>& sc );
the function is not template. If it was so, anyway, you were hiding the
original templates names with this second template declaration.
the correct declaration of such a friend function is (with forward
references)
#include<iostream>
using namespace std;
Hello Zeppe, thanks for your advise, I see your point, but also
disappointed that even this does give exactly the same error
and it should, since you are redeclaring the template parameters.
Perhaps this might explain it better:
int main()
{
int n = 99;
int n = 0; // why is that not allowed?
}
#include<iostream>
using namespace std;
template<typename T, int dummy>
class SomeClass;
template<typename T, int dummy>
ostream& operator << ( ostream& outStream, const SomeClass<T, dummy>&
sc )
{
outStream << "someVariable: " << sc.someVariable << endl;
return outStream;
}
template<typename T, int dummy>
class SomeClass
{
private:
T someVariable;
public:
SomeClass() : someVariable(65) {}
~SomeClass() {}
template<typename T, int dummy>
friend ostream& operator << ( ostream& outStream, const
SomeClass<T, dummy>& sc );
The error tells you what you did wrong. typename T and int dummy are
redeclared.
template<typename U, int Dummy>
friend ostream&
operator << ( ostream& outStream,
const SomeClass<U, Dummy>& sc );
....or...
friend ostream&
operator << < > ( ostream& outStream,
const SomeClass<T, dummy>& sc );
};
int main(int argc, char* argv[])
{
SomeClass<int,3> cl;
cout << cl;
return 0;
}