By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

declaration of `operator/' as non-function

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.