473,395 Members | 1,629 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,395 software developers and data experts.

usage of friend operators

Hello,

I have code for a vector template. However I am having problems with it.
Vector.hpp file starts like this:

template <class V, class I=int, class S=FullArray<V> >
class Vector: public Array<V, I, S>

{...

//Array and FullArray are some other templates that are already constructed.
//The goal of this Vector template is to simply create a template for vector
//classes with some numerical capabilities such as vector addition.

//This is not originally my code, however as far as I can understand + operator
//is overloaded to enable the Vector class some functionalities:
//Adding two vectors of same dimension. And also adding a vector and a scalar
//by adding the scalar to every dimension of the vector. This is beeing done in
//the following way:

// Return the sum of the elements
// Operators
Vector<V, I, S>& operator = (const Vector<V, I, S>& source);

Vector<V, I, S> operator - () const; // Unary minus

// Add v to every element
friend Vector<V, I, S> operator + (const Vector<V, I, S>& v, const V& a);

friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);

// Add the elements
Vector<V, I, S> operator + (const Vector<V, I, S>& v) const;

//However when I try to use this template in a main function I get the following:

[Warning] friend declaration `Vector<V, I, S> operator+(const Vector<V, I, S>&, const V&)' declares a non-template function

[Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning


I don't know what to do here. I would really appreciate help. Thanks.
Jun 26 '07 #1
12 2534
weaknessforcats
9,208 Expert Mod 8TB
This code:
// Add v to every element
friend Vector<V, I, S> operator + (const Vector<V, I, S>& v, const V& a);
isn't correct. Vector<V,I,S> is the class itself and it is the first argument meaning it is on the left of the + operator. When a Vector is on the left of the + operator, the compiler will supply the this pointer to the function so you don't need a Vector as the first argument.

This should not be a friend. It should be a member function:

Expand|Select|Wrap|Line Numbers
  1. Vector<V, I, S> operator + (const V& a); 
  2.  
However, if you insist on a friend function, you need to tell the compiler to not use the template for an operator+ with a const Vector& on the left and a const V& on the right. You do this by declaring an explicit specialization just after the template:

Expand|Select|Wrap|Line Numbers
  1. template<> Vector<V, I, S> operator + (const Vector<V, I, S>& v, const V& a); 
  2.  
that's what this warning is about:
[Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
An explicit specialization means you have written a function for this that you want to use instead of the function template.

To be sure I'm clear here, a function template is the template code whereas a template function is the function created by the compiler from a copy of the function template with the placeholder types substituted.
Jun 26 '07 #2
Hello,

I understand the idea behind replacing:

"friend Vector<V, I, S> operator + (const Vector<V, I, S>& v, const V& a);"

with Vector<V, I, S> operator + (const V& a);"

However, once I do this. I am still getting an error for:

friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);

[Warning] friend declaration `Vector<V, I, S> operator+(const V&, const Vector<V, I, S>&)' declares a non-template function

How can I take care of this? Are you suggesting to replace this with

Vector<V, I, S> operator + (const V& a);

Thanks
Jun 27 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);
For this to be a friend, it has to be a function that is already written.

Expand|Select|Wrap|Line Numbers
  1. template <class V, class I=int, class S=FullArray<V> >
  2. class Vector: public Array<V, I, S>
  3. {
  4.   public:
  5.     friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);
  6. };
  7.  
  8.  
  9. template <class V, class I, class S >
  10. Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v)
  11. {
  12.     //etc
  13. }
  14.  
The code above compiles and links with Visual Studio.NET 2005.

The default types need to be removed as they apply only to class templates.
Jun 27 '07 #4
is there a way to declare this template function in vector.h file. And then define it in vector.cpp file? Also, you are using Visual Studio.NET 2005. I am using Dev C++. Do you think is this a good compiler?
Jun 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
is there a way to declare this template function in vector.h file. And then define it in vector.cpp file? Also, you are using Visual Studio.NET 2005. I am using Dev C++. Do you think is this a good compiler?
Not unless your compiler supports export templates. Visual Studio.NET 2005 does not. Nor does any other compiler that I know of. If yours does, please let me know so I can update myself.

As it stands, your template must be at the global level (most likely in a header file).

I am curious why you are writing a Vector instead of using the one in the C++ Standard LIbrary??
Jun 28 '07 #6
Because this vector class is going to be for numerical purposes only and will include some additional mathematical functionality.

I think we are getting closer to solving this. But I am still having some problems. So I understand that before referring to a friend function we must declare it globally. So I have done that. In vector.h file before the line

template <class V, class I=int, class S=FullArray<V> >
class Vector: public Array<V, I, S>

I have declared and implemented:
template <class V, class I, class S>
Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v)
{
...
}


Then I get the following error for this line:

expected constructor, destructor, or type conversion before '<' token
expected `;' before '<' token


Thanks for your help so far.
Jun 28 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Look at my reply #4. That works.

Your friend function uses a Vector argument. Therefore, the friend function must appear after the Vector class definition.

I am still confused abiout your Vector.h and Vector.cpp. Everything must be in Vector.h.

Templates are not code. They are patterns the compiler makes copies of and substitutes types.
Jun 28 '07 #8
Hello,

I don't understand the problem but when I do it like you do that is first the vector template and then the template function as your #4 code. My dev c++ compiler seems to be ignoring the template function implementation. All of these are in vector.h. I get the same old errors:

[Warning] friend declaration `Vector<V, I, S> operator+(const V&, const Vector<V, I, S>&)' declares a non-template function

[Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
Jun 29 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
OK. Time to see your vector.h. I will compile it with Visual Studio.NET 2005 and see what happens.
Jun 29 '07 #10
Ok, from the following link:

http://web.mit.edu/~tulumbeg/www/

you can download vector.h and vector.cpp. Notice that these are the original code so function templates are defined in vector.cpp. Though as I have told you, moving those to vector.h did not help. Also, for your reference you can find all the files under this project at the above link. Thank you.
Jun 30 '07 #11
Ok, from the following link:

http://web.mit.edu/~tulumbeg/www/

you can download vector.h and vector.cpp. Notice that these are the original code so function templates are defined in vector.cpp. Though as I have told you, moving those to vector.h did not help. Also, for your reference you can find all the files under this project at the above link. Thank you.
I'm not sure if weaknessforcats realizes it, but he helped me out with a similar problem I had with a template that included a friend function, but he hasn't given the same answer here.

What he told me, and it worked, was that you need to mark the friend function as a template inside the class template itself.

so the code that he posted before:
Expand|Select|Wrap|Line Numbers
  1. template <class V, class I=int, class S=FullArray<V> >
  2. class Vector: public Array<V, I, S>
  3. {
  4. public:
  5.     friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);
  6. }; 
  7.  
  8.  
  9. template <class V, class I, class S >
  10. Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v)
  11. {
  12.     //etc
  13. }
should be:
Expand|Select|Wrap|Line Numbers
  1. template <class V, class I=int, class S=FullArray<V> >
  2. class Vector: public Array<V, I, S>
  3. {
  4. public:
  5.     template<class V, class I, class S>
  6.     friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);
  7. }; 
  8.  
  9.  
  10. template <class V, class I, class S >
  11. Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v)
  12. {
  13.     //etc
  14. }
line 5 is the key line. Try that. I'm not entirely familiar with templates with more than one parameter, but I think it should work.
Jun 30 '07 #12
OK, thanks this seems to work. Even though I still have some problems with the overall code. I will create another post for that.
Jul 2 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Michael Klatt | last post by:
I've just encountered a strange situation (at least to me) regarding friend operators and member operators: #include <map> class Key { friend bool operator<(const Key& lhs, const Key& rhs)...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
43
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
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() { }
0
by: Christopher | last post by:
On Apr 11, 12:07 am, Ian Collins <ian-n...@hotmail.comwrote: I've never seen those implemented _inside_ the class, but defined and implemented outside the class and then made a friend of the...
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
11
by: balach | last post by:
hi all; i am new in .NET and i am assigned work like this. my question is that, 3 clients are connected wtih server on LAN, i want to get the cpu usage and physical memory usage of one client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.