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

declaration of `operator/' as non-function

I'm coding a simple matrix class, which is resulting in the following
error when compiling with g++ 3.4.2 (mingw-special):

* declaration of `operator/' as non-function
* expected `;' before '<' token

<snip>
---------------------------------------------------
template <class T>
class matriz;
matriz<T> operator+ (const T&, const matriz<T>&);
template <class T>
matriz<T> operator- (const T&, const matriz<T>&);
template <class T>
matriz<T> operator* (const T&, const matriz<T>&);
template <class T>
matriz<T> operator/ (const T&, const matriz<T>&);

template <class T>
class matriz
{
private:
...
class linha
{
code of nested function...
};

public:
...
matriz<T> operator+ (const matriz<T>&) const;
matriz<T> operator- (const matriz<T>&) const;
matriz<T> operator* (const matriz<T>&) const;

friend matriz<T> operator+ <> (const T&, const matriz<T>&);
friend matriz<T> operator- <> (const T&, const matriz<T>&);
friend matriz<T> operator* <> (const T&, const matriz<T>&);
friend matriz<T> operator/ <> (const T&, const matriz<T>&);
};

---------------------------------------------------
</snip>

The problem is only with the operator/, which is being declared
identical to the others. What is odd is that when i add this
declaration:

matriz<T> operator/ (const matriz<T>&) const;

it compiles without errors.

Does it appear to be a bug in g++?

Nov 11 '05 #1
3 18770

gu*****@gmail.com wrote:
I'm coding a simple matrix class, which is resulting in the following
error when compiling with g++ 3.4.2 (mingw-special):

* declaration of `operator/' as non-function
* expected `;' before '<' token

<snip>
---------------------------------------------------
template <class T>
class matriz;
matriz<T> operator+ (const T&, const matriz<T>&);
This will not work. operator+ must be declared a template

template <class T>
matriz<T> operator- (const T&, const matriz<T>&);
template <class T>
matriz<T> operator* (const T&, const matriz<T>&);
template <class T>
matriz<T> operator/ (const T&, const matriz<T>&);

template <class T>
class matriz
{
private:
...
class linha
{
code of nested function...
};

public:
...
matriz<T> operator+ (const matriz<T>&) const;
matriz<T> operator- (const matriz<T>&) const;
matriz<T> operator* (const matriz<T>&) const;

friend matriz<T> operator+ <> (const T&, const matriz<T>&);
friend matriz<T> operator- <> (const T&, const matriz<T>&);
friend matriz<T> operator* <> (const T&, const matriz<T>&);
friend matriz<T> operator/ <> (const T&, const matriz<T>&);
};

---------------------------------------------------
</snip>

The problem is only with the operator/, which is being declared
identical to the others. What is odd is that when i add this
declaration:

matriz<T> operator/ (const matriz<T>&) const;

it compiles without errors.
It shouldnot. And it doesnot.

Does it appear to be a bug in g++?


No. Comeau online compiler also doesnot compile.
The problem is that the declaration of member operator+ hides all
overloaded declarations from the global scope. To access the global
function, you need to explicitly qualify it :

friend matriz<T> ::operator+ <> (const T&, const matriz<T>&);
But this will not compile - since :: gets associated with matriz<T>.
Instead, put parenthesis around the function name.

friend matriz<T> (::operator+ <>) (const T&, const matriz<T>&);

This should compile well.

Nov 11 '05 #2
Sorry, I stripped out some parts of the code to be short, then i
stripped the
template <class T> line before matriz<T> operator+. So it's declared
correctly.
Anyway, i tried your suggestion, but it produces the same error.

I'm posting the full header code now:

-----------------------------------------------------------------------
#ifndef MATRIZ_H
#define MATRIZ_H

#include <iostream>
#include <cstdlib>
#include <assert.h>

using std::cout;
using std::cin;
using std::ostream;
using std::istream;

template <class T>
class matriz;
template <class T>
ostream &operator<< (ostream &saida, const matriz<T> &mat);
template <class T>
istream &operator>> (istream &entrada, matriz<T> &mat);
template <class T>
matriz<T> operator+ (const T&, const matriz<T>&);
template <class T>
matriz<T> operator- (const T&, const matriz<T>&);
template <class T>
matriz<T> operator* (const T&, const matriz<T>&);
template <class T>
matriz<T> operator/ (const T&, const matriz<T>&);

template <class T>
class matriz
{
private:
T **matPtr, *matStore;
int lin, col;

class linha
{
private:
T *linPtr;
int linTam;
public:
linha(T *, int);
T &operator[] (int);
const T &operator[] (int) const;
};

public:

matriz(int=1, int=1);
matriz(const matriz<T> &);
~matriz();
const matriz<T> &operator= (const matriz<T> &);

linha operator[] (int indice);

friend ostream& operator<< <>(ostream &, const matriz<T> &);
friend istream& operator>> <>(istream &, matriz<T> &);

const bool operator== (const matriz<T>&) const;
const bool operator!= (const matriz<T>&) const;

matriz<T> operator+ (const matriz<T>&) const;
matriz<T> operator- (const matriz<T>&) const;
matriz<T> operator* (const matriz<T>&) const;

matriz<T> operator+ (const T&) const;
matriz<T> operator- (const T&) const;
matriz<T> operator* (const T&) const;
matriz<T> operator/ (const T&) const;

matriz<T> &operator+= (const matriz<T>&);
matriz<T> &operator-= (const matriz<T>&);
matriz<T> &operator*= (const matriz<T>&);

matriz<T> &operator+= (const T&);
matriz<T> &operator-= (const T&);
matriz<T> &operator*= (const T&);
matriz<T> &operator/= (const T&);

friend matriz<T> operator+ <> (const T&, const matriz<T>&);
friend matriz<T> operator- <> (const T&, const matriz<T>&);
friend matriz<T> operator* <> (const T&, const matriz<T>&);
friend matriz<T> operator/ <> (const T&, const matriz<T>&);
};

#endif // MATRIZ_H
----------------------------------------------------------

Nov 11 '05 #3
Forget it... applying what you said :

friend matriz<T> (::operator/ <>) (const T&, const matriz<T>&);

really works.
It seems I need to learn more about namespaces...

Thank you very much Neelesh!

Gustavo

Nov 11 '05 #4

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

Similar topics

2
by: Xavier Decoret | last post by:
The following code does not compoile with gcc-3.2.3 namespace dummy { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Interface of Foo...
3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
9
by: sb | last post by:
If there is at least one user-defined constructor, no constructors are implicitly declared. struct my_vec : public vector<int> { double foo; my_vec(size_t n) : vector<int>(2*n) {} // oops, no...
5
by: j0mbolar | last post by:
operator = (const char *string) { if(m_string) { free(m_string); m_string = 0; } if(string) { m_string = strdup(string); } }
10
by: Whitney Kew | last post by:
Hello, I have a question regarding type conversion operators. Let's say I have a simple (nonsensical) class as follows: class MakeNewInt { private: int* _n;
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
5
by: Mykhaylo Khodorev | last post by:
Hi, all I've created a class derived from CObArray. The declaration of operator of CObArray looks like: CObArray* operator (int nIndex) const; CObArray*& operator (int nIndex); I understand...
4
by: rjd | last post by:
Does anybody else see this in VS.NET 2003? When I get a genuine C++ compiler error (could be anything, usually innocuous) it's regularly followed by a slew of these spurious and completely...
23
by: Randy | last post by:
Since these operators can't be member functions, and since friend functions can't be declared virtual, how do I make my inserters and extractors polymorphic? --Randy Yates
2
by: Mark P | last post by:
Consider a class in which I redefine operator new(std::size_t): struct A { void* operator new(std::size_t size) {/* my implementation */} }; This has the consequence of hiding the placement...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.