473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

friend template function with multiple types

I'm trying to write a class for a smart pointer. I'm having a problem
though when trying to implement operator== with both sides being a Smart.
(Or any function that I try to bring U into.)
The pointer contained might not be the exact same type but still get
tested for equality. Here's what I have:
template <typename T> class Smart;

template<typename T, typename U>
bool operator== ( const Smart<T>& a, const Smart<U>& b );

template <typename T> class Smart
{
/* On the next line, GCC says:
"type/value mismatch at argument 1 in template parameter list for
`template<class T> class Smart expected a type, got `U'"
*/

friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
// friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

}

bool operator== (const Smart<T>& a, const<SmartU>& b)
{
return true;
}
It also warns about the angle brackets not being there, which are in the
commented out line, but adding them does not solve the error.

What must I do to make the compiler happy?

Thanks,
-Jim
Jul 19 '05 #1
4 5534
Bonzo wrote in
news:do************************@syrcnyrdrs-02-ge0.nyroc.rr.com:
I'm trying to write a class for a smart pointer. I'm having a problem
though when trying to implement operator== with both sides being a
Smart. (Or any function that I try to bring U into.)
The pointer contained might not be the exact same type but still get
tested for equality. Here's what I have:
template <typename T> class Smart;

template<typename T, typename U>
bool operator== ( const Smart<T>& a, const Smart<U>& b );

template <typename T> class Smart
{
/* On the next line, GCC says:
"type/value mismatch at argument 1 in template parameter list
for
`template<class T> class Smart expected a type, got `U'"
*/
// Overkill but harmless:
template <typename A, typename B>
friend bool operator == (Smart<A> const &a, Smart<B> const &b);
// But why not just make it a member function?

template <typename U>
bool operator == (Smart<U> const &rhs) const
{
return false /* or whatever */;
}

friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
// friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

}

bool operator== (const Smart<T>& a, const<SmartU>& b)
{
return true;
}
It also warns about the angle brackets not being there, which are in
the commented out line, but adding them does not solve the error.

What must I do to make the compiler happy?


HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2
"Bonzo" <do**@email.me> wrote...
I'm trying to write a class for a smart pointer. I'm having a problem
though when trying to implement operator== with both sides being a Smart.
(Or any function that I try to bring U into.)
The pointer contained might not be the exact same type but still get
tested for equality. Here's what I have:
template <typename T> class Smart;

template<typename T, typename U>
bool operator== ( const Smart<T>& a, const Smart<U>& b );

template <typename T> class Smart
{
/* On the next line, GCC says:
"type/value mismatch at argument 1 in template parameter list for
`template<class T> class Smart expected a type, got `U'"
*/

friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
You have to say that this is from a template:

template<class T, class U> friend bool operator ==(...

otherwise, what's "U"?

And unfortunately, it's impossible to make a partial specialisation
a friend.
// friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

} ^^^
You forgot the semicolon here...

bool operator== (const Smart<T>& a, const<SmartU>& b)
Again, this is not right. What's "T" and "U"? What's const<SmartU>?
Why don't you post the real code instead of typing it here?
{
return true;
}
It also warns about the angle brackets not being there, which are in the
commented out line, but adding them does not solve the error.

What must I do to make the compiler happy?


You probably want to find a good book on templates.

Here is what compiles:
----------------------------------------
template<class T> class Smart;

template<class T,class U>
bool operator== ( const Smart<T>&, const Smart<U>&);

template<class T> class Smart
{
template<class S, class U>
friend bool operator==( const Smart<S>&, const Smart<U>&);
};

template<class T, class U>
bool operator== (const Smart<T>& a, const Smart<U>& b)
{
return true;
}

int main()
{
Smart<int> si;
Smart<bool> sb;
si == sb;
}

----------------------------------------

HTH

Victor
Jul 19 '05 #3
In article <vg************@corp.supernews.com>,
"Victor Bazarov" <v.********@attAbi.com> wrote:
You forgot the semicolon here...

bool operator== (const Smart<T>& a, const<SmartU>& b)


Again, this is not right. What's "T" and "U"? What's const<SmartU>?
Why don't you post the real code instead of typing it here?


Good question. I made a stripped down version just to demonstrate the
error. I actually *did* compile it, getting the same error, was
satisfied, and posted it. Unfortunately, there were plenty of new errors
after the one I posted about.

After spending the better part of the day upping my template knowledge,
stripping out all the friend stuff for member functions, and making
changes similar to your code, not only does it work but I understand it.
Many thanks.

-Jim
Jul 19 '05 #4
In article <be************@ID-196037.news.uni-berlin.de>,
"John Harrison" <jo*************@hotmail.com> wrote:
First of I'd question the need to compare pointers of different type, normal
pointers don't work like that so why should smart pointers?
Yeah, I ripped that code out. I wouldn't be using it. It was in some
smart pointer code I was basing mine off of.
Secondly I'd question the need for friendship at all. Most smart pointers
have a member function that lets you get at the raw pointer


As did I, but I didn't really understand what was going on enough. Now
that I do, I kicked my friends out and added some member functions
instead.

Thank you everyone for the replies.

-Jim
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Stippler | last post by:
Hi what's wrong about the following code? It does not compile. template <typename T> class A { public: class B; };
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
5
by: James Aguilar | last post by:
Hello, all. I saw this in the provided code of a lab that I have due in a couple of weeks in my algorithms class. It compiled fine in g++ but did not compile with the VC++ compiler. It does...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
2
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.