[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 170 KB, 550x510, C++ACCIDENT.jpg [View same] [iqdb] [saucenao] [google]
2038435 No.2038435 [Reply] [Original]

I have a templates class Deck and in it I am overload the << operator:

template <class T>

class Deck {

public:
friend ostream& operator<<(ostream&, Deck<T>& );

}

then I define this operator...

but i get warning:

warning: friend declaration 'std::ostream& operator<<(std::ostream&, Deck<T>&)' declares a non-template function

and when i use it i get linker error that this function is not found.

Why is that?
thanks /sci/

ps. I can provide .h file if u guys want, but i don't think its necessary

>> No.2038441

I love C++

>> No.2038467

You've never read the C++ FQA, have you?

It shows.

http://yosefk.com/c++fqa/

>> No.2038957

OP here...
>>2038467 I read that bro, but what I don't understand is this:

"The snag happens when the compiler sees the friend lines way up in the class definition proper. At that moment it does not yet know the friend functions are themselves templates; it assumes they are non-templates like this:"

Now i understand this, but what confuses me is that isn't this true aswell for non-friend functions too? why don't they generate a linker error too?

thanks

>> No.2039046

Learn to c++ kid.

>> No.2039278

bump