all irght, I would like to know what I am doing wrong, this compiles and works as it should:
template<typename T, unsigned int N>
class vecN
{
public:
T elements[N];
//stuff
};
template<typename T, unsigned int N>
inline ostream &operator<<(ostream &ostr, const vecN<T,N> &in)
{
unsigned int i;
ostr << "(";
for(i=0;i<N;++i)
{
if(i!=0)
ostr << ", ";
ostr << in;
}
ostr <<")";
return ostr;
}
//somewhere in a function
vecN<float,3> it;
cout << it;
but the following the compiler pukes on:
template<typename T>
class FunctionFactory
{
//lots of stuff
public:
class Function
{
//stuff
public:
//stuff
ostream &Print(ostream &ostr) const
{
//stuff
}
};
}
template<typename T>
inline ostream &operator<<(ostream &ostr, const typename FunctionFactory<T>::Function &obj)
{
return obj.Print(ostr);
}
//somewhere in main...
FunctionFactor<float>::Function handy
//blah
cout << handy //compile fails here!
the exact compile message is: "error: no match for ‘operator<<’ in ‘std::cout << handy" and then proceeds to list all the ways that the << operator is overloaded.
but it works jsut fine if I have
handy.Print(cout);
what am I doing wrong? (compiler g++ v4.02)
Close this Gamedev account, I have outgrown Gamedev.
Is that code copy-and-pasted? If so, you're missing a semi-colon on the class just before you define your operator<<. If it's not a copy-and-paste, it should be (who knows what small mistake you could be overlooking in the actual code).
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
still gets rejected by the compiler (i.e. it does not believe that the function exists) but the second suggestion does work. Also, I don't _think_ that inline is redundant in the case for template stuff: template stuff (I imagine) gets generated for each source file (thus the linker does not really see DOSomething<float> for example) but I _think_ that unless you put inline there, then the compiler wil have exactly one copy of DOSomething<float> for each soruce file that uses it and generate a "jmp" to that address, where as inline _suggests_ to the compiler to inline the function...
Close this Gamedev account, I have outgrown Gamedev.