Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template friend operator

Started by Si_con Jul 28, 2011 at 6:53 PM 7 replies 1.9k views
Original Post
Si_con
Si_con
I have problems trying to make a templated friend operator, i have searched on internet and they put that putting the declarations before the class, and the <> after the operator symbol it works, but i cant get that working, what im doing wrong? which is the right way of making a templated friend operator?



#ifndef _FOO_H_
#define _FOO_H_

template <class T> class Foo;
template <class T> const Foo<T> operator * (const int k, const Foo<T>& foo);

template <class T> class Foo {
private:
T a,b;

public:
Foo() {};
Foo(T n1,T n2):a(n1),b(n2) { };
Foo(const Foo& foo):a(foo.a),b(foo.b) { };
~Foo() {};

const Foo<T>& operator = (const Foo<T>& foo) {
a = foo.a;
b = foo.b;
return *this;
};

const Foo<T>& operator * (const Foo<T>& foo) {
a *= foo.a;
b *= foo.b;
return *this;
};

const Foo<T> operator * (const Foo<T>& foo) const {
return Foo<T>(foo.a, foo.b);
};

// template <class T>
friend const Foo<T> operator* <> (const int k, const Foo<T>& foo);
};



template <class T> const Foo<T> operator * (const int k, const Foo<T>& foo) {
return Foo<T>(foo.a*k, foo.b*k);
};

#endif




Si_con
Si_con
the friend operator part:




friend const Foo<T> operator* <> (const int k, const Foo<T>& foo);

Foo.h|35|error: declaration of 'operator*' as non-function|

Foo.h|35|error: expected ';' before '<' token|

SiCrane
SiCrane
Why do you have the <> in your friend statement?
sprite_hound
sprite_hound
This seems to work fine for me:

[source lang="cpp"]


template class Foo {
private:
T a,b;

public:
Foo() {};
Foo(T n1,T n2):a(n1),b(n2) { }
Foo(const Foo& foo): a(foo.a), b(foo.b) { }
~Foo() {}

const Foo& operator = (const Foo& foo) {
a = foo.a;
b = foo.b;
return *this;
}

Foo const& operator*(const Foo& foo) {
a *= foo.a;
b *= foo.b;

std::cout << "inline" << std::endl;
return *this;
}

template friend const Foo operator* (const int k, const Foo& foo);
};



template
const Foo operator * (const int k, const Foo& foo) {
std::cout << "friend" << std::endl;
return Foo(foo.a*k, foo.b*k);
};

int main(int, char*[])
{
Foo f;
5 * f;
f * f;
}
[/source]
Si_con
Si_con
if i compile that code it tells me: main.cpp|32|error: declaration of 'class T'| and main.cpp|8|error: shadows template parm 'class T'|
sprite_hound
sprite_hound
Hmm.. sounds like MSVC++ and whatever compiler you're using disagree about things. How about defining the friend function from within the actual class instead:

[source lang="cpp"]


template class Foo {
private:
T a,b;

public:
Foo() {};
Foo(T n1,T n2):a(n1),b(n2) { }
Foo(const Foo& foo): a(foo.a), b(foo.b) { }
~Foo() {}

const Foo& operator = (const Foo& foo) {
a = foo.a;
b = foo.b;
return *this;
}

Foo const& operator*(const Foo& foo) {
a *= foo.a;
b *= foo.b;

std::cout << "inline" << std::endl;
return *this;
}

friend const Foo operator* (const int k, const Foo& foo) {
std::cout << "friend" << std::endl;
return Foo(foo.a*k, foo.b*k);
}
};

int main(int, char*[])
{
Foo f;
5 * f;
f * f;
}
[/source]
Si_con
Si_con
Im using Code blocks with gcc and i have tried also with the vc++ but give me errors too.

Thanks, this version with the operator inline works, although still would like to know why the other is not working in some compilers as it seems

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.