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

P

Preben

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
 
B

Bernd Strieder

Hello,
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 said:
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
 
P

Preben

template said:
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>




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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top