Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to make a template class-friend function

41 views
Skip to first unread message

JiiPee

unread,
Jan 17, 2015, 7:54:43 AM1/17/15
to
So I have this global function which I want to be a friend of a class. I
tried all kind of combinations after googling but none worked so far. If
anybody remembers how to do this would be nice, thanks. Here is the code:

// forwards declaring things for the template function - from google...
template <typename T>
class Vector2D;

template <typename T>
Vector2D<T> operator * (const T& pointXY, const Vector2D<T>& point);

// my class
template <typename T>
class Vector2D : public Coordinate2D<T>
{
public:
// this is what I tried last time.. does not work
friend Vector2D<T> operator * (const T& pointXY, const Vector2D<T>&
point);
};

// the global function implementation:
template <typename T>
inline Vector2D<T> operator * (const T& pointXY, const Vector2D<T> & point)
{
return point * pointXY;
}

Victor Bazarov

unread,
Jan 17, 2015, 8:19:43 AM1/17/15
to
Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
newsgroup here: http://www.parashift.com/c++-faq-lite/

Victor
--
I do not respond to top-posted replies, please don't ask

JiiPee

unread,
Jan 17, 2015, 8:37:15 AM1/17/15
to
On 17/01/2015 13:19, Victor Bazarov wrote:
>
> Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
> newsgroup here: http://www.parashift.com/c++-faq-lite/
>
> Victor

I actually did read (quite carefully) that and tried it, but it did not
work. But I now double checked, tried again, and still not working. On
site it says (
http://www.parashift.com/c++-faq-lite/template-friends.html ) :

"one simple approach is pre-declare each template friend function /above/"

I did that (pls see my code)....

Then it says:
"Also you add <> in the friend lines, as shown:

friend Foo<T> operator+ <> (const Foo<T>& lhs, const Foo<T>& rhs);

"

I also did that, then my line becomes:

friend Vector2D<T> operator * <> (const T& pointXY, const Vector2D<T>&
point);

But if I try to compile this it gives an error: "error: declaration of
'operator*' as non-function|"

Why is it not working for me?

Melzzzzz

unread,
Jan 17, 2015, 8:39:02 AM1/17/15
to
Perhaps you should put template keyword?

JiiPee

unread,
Jan 17, 2015, 8:52:11 AM1/17/15
to
I tried all 4 combinations (template, <>) and none worked. Template
before friend and after

Template friend

friend Template

Louis Krupp

unread,
Jan 17, 2015, 9:04:05 AM1/17/15
to
Can you post a complete code sample that reproduces the problem?

Louis

Melzzzzz

unread,
Jan 17, 2015, 9:07:02 AM1/17/15
to
On Sat, 17 Jan 2015 13:51:52 +0000
template <> friend Vector2D<T> operator*(...)

JiiPee

unread,
Jan 17, 2015, 9:11:08 AM1/17/15
to
On 17/01/2015 14:06, Melzzzzz wrote:
> template <> friend

"error: explicit specialization in non-namespace scope 'class
ct::Vector2D<T>'|"

but... the foo-example on the site works... so maybe I have to check
very carefully what is the difference... just cannot find it... somebody
help :)

Victor Bazarov

unread,
Jan 17, 2015, 9:12:30 AM1/17/15
to
I wonder how long it will take JiiPee to re-read FAQ 5.8 and follow the
instructions.

V

Victor Bazarov

unread,
Jan 17, 2015, 9:13:30 AM1/17/15
to
<sigh> I pointed you to the question 5.8 not because I wanted you to
read the template section of the FAQ but because I wanted you to follow
*exactly* the recommendation on how to post questions about the code
that does not work correctly. Did you? Did you post a complete code?
Did you post the compiler output verbatim? ... And all that needs to be
in the same message! It's not enough to *desire* help.

Please, post the complete code that we could copy-and-paste into our
development environment to be compiled (or tried to be compiled).
Please post *verbatim* the compiler error you get (we don't all have the
same compiler as you.

Keep in mind that templates need to be instantiated (i.e. *used*) to be
analyzed by the compiler and linker.

Do you get me?

V

JiiPee

unread,
Jan 17, 2015, 9:14:54 AM1/17/15
to
On 17/01/2015 13:19, Victor Bazarov wrote:
>
> Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
> newsgroup here: http://www.parashift.com/c++-faq-lite/
>
> Victor

Ok, I compiled the site example, and it works. So obviously in my code
there is something I cannot find. I ll try to double check..

site example which works:

template<typename T> class Foo; // pre-declare the template class itself
template<typename T> Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>&
rhs);

template<typename T>
class Foo {
public:
Foo(T const& value = T());
friend Foo<T> operator+ <> (const Foo<T>& lhs, const Foo<T>& rhs);
private:
T value_;
};

template<typename T>
Foo<T>::Foo(T const& value)
: value_(value)
{ }

template<typename T>
Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs)
{ return Foo<T>(lhs.value_ + rhs.value_); }


JiiPee

unread,
Jan 17, 2015, 9:19:27 AM1/17/15
to
On 17/01/2015 14:13, Victor Bazarov wrote:
>
> <sigh> I pointed you to the question 5.8 not because I wanted you to
> read the template section of the FAQ but because I wanted you to
> follow *exactly* the recommendation on how to post questions about the
> code that does not work correctly.

oh I see... ok, If the error remains I do that.


>
>
> Keep in mind that templates need to be instantiated (i.e. *used*) to
> be analyzed by the compiler and linker.

yes i know... i did that.

I ll try to find myself the error... if not I post complete code.


Öö Tiib

unread,
Jan 17, 2015, 9:31:32 AM1/17/15
to
On Saturday, 17 January 2015 15:37:15 UTC+2, JiiPee wrote:
> On 17/01/2015 13:19, Victor Bazarov wrote:
> >
> > Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
> > newsgroup here: http://www.parashift.com/c++-faq-lite/
> >
> > Victor
>
> I actually did read (quite carefully) that and tried it, but it did not
> work. But I now double checked, tried again, and still not working.

Sorry, but then you can't read:

5.8.2 Post complete code: put in all necessary #includes and declarations of needed types and functions
5.8.5 Post the tools you used: compiler name, version number, operating system, etc
5.8.6 Post the tool options you used: libraries, exact compiler and linker options, etc
5.8.7 Post the exact messages you received; differentiate between compiler, linker, and runtime messages

Instead you paste piece of code that can't compile nowhere and write "it
didn't work, I tried again, it didn't work".

To me all compiler's also give errors in the lines of "I have no idea what
is Coordinate2D.", also ... I can not see problems with that friend.

JiiPee

unread,
Jan 17, 2015, 9:33:24 AM1/17/15
to
Ok, here is the error code which can be compiled. I posted the
foo-version from the site which works.. so cannot see what is the
difference:

template <typename T> class Vector2D2;
template <typename T> Vector2D2<T> operator * (const T& pointXY, const
Vector2D2<T>& point);

template <typename T>
class Vector2D2
{
public:
// error: none of these works...
friend Vector2D2<T> operator* <> (const T& pointXY, const
Vector2D2<T>& point);
// friend Vector2D2<T> operator* (const T& pointXY, const
Vector2D2<T>& point);
T b;
};

template <typename T>
Vector2D2<T> operator* (const T& pointXY, const Vector2D2<T>& point)
{
Vector2D2<T> cc;
cc.b = point.b * pointXY;
return cc;
}

int main()
{
Vector2D2<double> jj;
3.0 * jj;
}

JiiPee

unread,
Jan 17, 2015, 9:34:50 AM1/17/15
to
On 17/01/2015 14:31, Öö Tiib wrote:
> Instead you paste piece of code that can't compile nowhere and write
> "it didn't work, I tried again, it didn't work". To me all compiler's
> also give errors in the lines of "I have no idea what is
> Coordinate2D.", also ... I can not see problems with that friend.

ok, i just posted a complete code

JiiPee

unread,
Jan 17, 2015, 9:35:20 AM1/17/15
to
On 17/01/2015 14:03, Louis Krupp wrote:
> Can you post a complete code sample that reproduces the problem?
>
> Louis

Just posted it.

JiiPee

unread,
Jan 17, 2015, 10:01:14 AM1/17/15
to
hmmm, sometimes my compiler compiles it and sometimes not....

I ll try to first sort out where is the real problem. ... I ll come back
if the error remains.

JiiPee

unread,
Jan 17, 2015, 10:22:07 AM1/17/15
to
it was the inheritance what caused the problem.. so something in base
class. The template works. So if I change the operator from * to % for
example, then everything compiles. So in base class there is also * -
operator which gave troubles .

Well, the advice to make a code which you guys can compile it helped to
find this out.

Now just have to check the base class why this happens....

Paavo Helde

unread,
Jan 17, 2015, 11:35:01 AM1/17/15
to
JiiPee <n...@notvalid.com> wrote in news:o6vuw.154649$6k.8...@fx09.am4:

> it was the inheritance what caused the problem.. so something in base
> class. The template works. So if I change the operator from * to % for
> example, then everything compiles. So in base class there is also * -
> operator which gave troubles .

Looks like you need to use explicit namespace in the friend declaration.
Like ::operator* or mynamespace::operator*.

hth
Paavo

JiiPee

unread,
Jan 17, 2015, 12:10:06 PM1/17/15
to
in base class/with base class? Ok, thanks... I ll try this.

red floyd

unread,
Jan 17, 2015, 12:18:22 PM1/17/15
to
Better, but what *ERROR* messages did you receive?

JiiPee

unread,
Jan 17, 2015, 12:48:51 PM1/17/15
to
in the other post I explain that the error is related to the base
class.... so if I dont inherite anything then it does compile. So this
is one of these where I though the error was there but it was somewhere
else (it has to do with the base class *-operation function ... they
somehow conflict.... i ll check this later).

Paavo Helde

unread,
Jan 17, 2015, 1:12:34 PM1/17/15
to
In the friend declaration in the derived class. But as you have failed so
far to post a failing code example and/or the exact error message, one
cannot be sure.

Cheers
Paavo

JiiPee

unread,
Jan 17, 2015, 1:17:28 PM1/17/15
to
yes true, I did not give the inheritance code. I gave the
non-inheritance version which actually works...

I ll check this myself ... i have not yet tried to fix the base class. I
ll come back if I fail to do it.

red floyd

unread,
Jan 17, 2015, 10:11:43 PM1/17/15
to
I'm glad you found the error, but if you hadn't... My point was that
5.8 tells you how to post problems.

1. Post the code
2. Name the compiler and system
3. Post your errors.


JiiPee

unread,
Jan 18, 2015, 7:23:50 AM1/18/15
to
On 18/01/2015 03:11, red floyd wrote:
>
> I'm glad you found the error, but if you hadn't... My point was that
> 5.8 tells you how to post problems.
>
> 1. Post the code
> 2. Name the compiler and system
> 3. Post your errors.


yes true. although if its a very simple issue, like one line, i guess
its not necessary all those. But here yes...

am still trying to figure out what causes it.

>
>

0 new messages