No declaration and missing operator* in gcc 4.1.1, but not icc andgcc 3.3

Discussion in 'C++' started by Preben, Sep 25, 2006.

  1. Preben

    Preben Guest

    Hi everyone

    Could you help me with these erorrs:
    -----------
    rotX.cpp: In member function 'void AO_Sphere::draw()':
    rotX.cpp:32: error: 'rotX' was not declared in this scope
    rotX.cpp:33: error: no match for 'operator*' in '-0x000000001 *
    orientierung'
    -----------

    and one of the errors is also pointed out by icc:
    -----------
    rotX.cpp(33): error: no operator "*" matches these operands
    operand types are: int * KMatrix<double>
    orientierung = -1*orientierung;
    ^

    compilation aborted for rotX.cpp (code 2)
    -----------


    And the code is as follows:
    -----------
    #define PI 3.14159265358979323846

    template<class Tcomp> class KMatrix;
    template<class Tcomp> KMatrix<Tcomp> operator* (const Tcomp r, const
    KMatrix<Tcomp>&);

    template<class Tcomp> class KMatrix {
    public:

    KMatrix();
    KMatrix(int rows, int cols); /** KMatrix of dimension rows x cols */
    KMatrix(const KMatrix<Tcomp>&);
    ~KMatrix();

    const KMatrix<Tcomp>&
    operator -= (const KMatrix<Tcomp>&);

    /** * scalar multiplication */
    friend KMatrix<Tcomp> operator * <>(const Tcomp r, const
    KMatrix<Tcomp> &m);

    /** * generate rotation matrices about x,y,z-axes */
    friend KMatrix<double> rotX(double angle);
    };

    class AO_Sphere {
    public:
    void draw();
    };


    void AO_Sphere::draw() {
    KMatrix<double> orientierung;
    orientierung = rotX(double(2*PI)/double(1));
    orientierung = -1*orientierung;
    }
    -----------

    The first problem is that the rotX method isn't found. How come that
    this is the case. It is defined as a friend method? Is the syntax not
    correct? How to alter?

    The next problem could actually easily be solved by replacing a "-1."
    instead of "-1" in the second-last line. But however for code
    maintenance this is a bad idea.
    How can I let the compiler accept the code by casting the -1 to a double
    automatically.
    As far as I know, any function on a template should automatically cast
    to the given value if an implicit constructor is given. Since this is
    int and double there shouldn't be such a problem, or?


    Thanks

    / Preben
    Preben, Sep 25, 2006
    #1
    1. Advertising

  2. Re: No declaration and missing operator* in gcc 4.1.1, but not icc and gcc 3.3

    Hello,

    Preben wrote:

    > template<class Tcomp> class KMatrix;
    > template<class Tcomp> KMatrix<Tcomp> operator* (const Tcomp r, const
    > KMatrix<Tcomp>&);


    Declare the functions you want to make friends before the template
    class:

    KMatrix<double> rotX(double angle);

    template <class Tcomp>
    KMatrix<Tcomp> operator * (const Tcomp r, const KMatrix<Tcomp> &m);

    >
    > template<class Tcomp> class KMatrix {


    > /**  *  scalar multiplication */
    > friend KMatrix<Tcomp> operator * <>(const Tcomp r, const
    > KMatrix<Tcomp> &m);
    >
    > /**  *  generate rotation matrices about x,y,z-axes */
    > friend KMatrix<double> rotX(double angle);
    > };


    > KMatrix<double> orientierung;
    > orientierung = rotX(double(2*PI)/double(1));
    > orientierung = -1*orientierung;


    To avoid problems due to incorrect types and for clarity use -1.0:

    orientierung = -1.0*orientierung;

    You might have to make the type of the scalar another parameter of
    scalar multiplication to enable multiplicating an int scalar. That way
    the compiler would not fail instantiating the right friends. I think
    using numerical POD types as template parameters can be a bit tempting
    to get right, if you expect natural behaviour, i.e. all those implicit
    casts (like int to double) done automatically.

    gcc has become stricter since gcc-3.4 in the C++ it accepts. See the
    changes for gcc 3.4 and the following releases at the site gcc.gnu.org
    for an overview. This is why your code used to not have problems with
    earlier gcc releases. Other compilers might be not as strict, as well.

    Bernd Strieder
    Bernd Strieder, Sep 25, 2006
    #2
    1. Advertising

  3. Preben

    Preben Guest

    Re: No declaration and missing operator* in gcc 4.1.1, but not iccand gcc 3.3

    >> template<class Tcomp> class KMatrix;
    >> template<class Tcomp> KMatrix<Tcomp> operator* (const Tcomp r, const
    >> KMatrix<Tcomp>&);

    >
    > Declare the functions you want to make friends before the template
    > class:
    >
    > KMatrix<double> rotX(double angle);


    Okay, I've been trying with
    template <class TComp> KMatrix<double> rotX(double angle)

    but how to know, when to apply the template <class TComp> and when not
    to?... I'm quite a bit confused!


    > template <class Tcomp>
    > KMatrix<Tcomp> operator * (const Tcomp r, const KMatrix<Tcomp> &m);
    >
    >> template<class Tcomp> class KMatrix {

    >
    >> /** * scalar multiplication */
    >> friend KMatrix<Tcomp> operator * <>(const Tcomp r, const
    >> KMatrix<Tcomp> &m);
    >>
    >> /** * generate rotation matrices about x,y,z-axes */
    >> friend KMatrix<double> rotX(double angle);
    >> };

    >
    >> KMatrix<double> orientierung;
    >> orientierung = rotX(double(2*PI)/double(1));
    >> orientierung = -1*orientierung;

    >
    > To avoid problems due to incorrect types and for clarity use -1.0:
    >
    > orientierung = -1.0*orientierung;
    >
    > You might have to make the type of the scalar another parameter of
    > scalar multiplication to enable multiplicating an int scalar. That way
    > the compiler would not fail instantiating the right friends. I think
    > using numerical POD types as template parameters can be a bit tempting
    > to get right, if you expect natural behaviour, i.e. all those implicit
    > casts (like int to double) done automatically.


    So you mean, that I should implement and operator

    friend KMatrix<Tcomp> operator * <>(const int r, const KMatrix<Tcomp> &m);

    and maybe others as well?
    This will however mean a lot of redundancy in code!

    > gcc has become stricter since gcc-3.4 in the C++ it accepts. See the
    > changes for gcc 3.4 and the following releases at the site gcc.gnu.org
    > for an overview. This is why your code used to not have problems with
    > earlier gcc releases. Other compilers might be not as strict, as well.


    Yes, that's nice when you write new programs, but correcting other
    people's errors is a quite difficult when you haven't used templates before!


    Thanks / Preben
    Preben, Sep 25, 2006
    #3
    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. Patrick Laurent

    How to force 'inline' with GCC or ICC

    Patrick Laurent, May 15, 2005, in forum: C++
    Replies:
    44
    Views:
    43,146
    jagadev
    Oct 6, 2006
  2. easter bunny

    icc, gcc sse alignement

    easter bunny, Jul 3, 2007, in forum: C++
    Replies:
    2
    Views:
    392
    easter bunny
    Jul 4, 2007
  3. Col
    Replies:
    0
    Views:
    392
  4. CoL
    Replies:
    2
    Views:
    435
    Tim Prince
    Aug 17, 2007
  5. J K
    Replies:
    6
    Views:
    520
    James Kuyper
    Feb 23, 2012
Loading...

Share This Page