declaration of `operator/' as non-function

G

gugdias

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++?
 
N

Neelesh

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>

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> ::eek:perator+ <> (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> :):eek:perator+ <>) (const T&, const matriz<T>&);

This should compile well.
 
G

gugdias

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::eek:stream;
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


----------------------------------------------------------
 
G

gugdias

Forget it... applying what you said :

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

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

Thank you very much Neelesh!

Gustavo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top