declaration of `operator/' as non-function

Discussion in 'C++' started by gugdias@gmail.com, Nov 11, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Neelesh Guest

    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> ::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.
     
    Neelesh, Nov 11, 2005
    #2
    1. Advertising

  3. Guest

    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


    ----------------------------------------------------------
     
    , Nov 11, 2005
    #3
  4. Guest

    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
     
    , Nov 11, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    2
    Views:
    8,405
    Alex Vinokur
    Oct 1, 2004
  2. Ovidesvideo
    Replies:
    4
    Views:
    553
    Andrey Tarasevich
    Dec 10, 2004
  3. Replies:
    4
    Views:
    1,132
    Richard Tobin
    Dec 12, 2006
  4. Bolin
    Replies:
    4
    Views:
    420
  5. Luca Forlizzi
    Replies:
    4
    Views:
    486
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page