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

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

P: n/a
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 Tcompclass KMatrix;
template<class TcompKMatrix<Tcompoperator* (const Tcomp r, const
KMatrix<Tcomp>&);

template<class Tcompclass 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<Tcompoperator * <>(const Tcomp r, const
KMatrix<Tcomp&m);

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

class AO_Sphere {
public:
void draw();
};
void AO_Sphere::draw() {
KMatrix<doubleorientierung;
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
Sep 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hello,

Preben wrote:
template<class Tcompclass KMatrix;
template<class TcompKMatrix<Tcompoperator* (const Tcomp r, const
KMatrix<Tcomp>&);
Declare the functions you want to make friends before the template
class:

KMatrix<doublerotX(double angle);

template <class Tcomp>
KMatrix<Tcompoperator * (const Tcomp r, const KMatrix<Tcomp&m);
>
template<class Tcompclass KMatrix {
/** ** *scalar multiplication */
friend KMatrix<Tcompoperator * <>(const Tcomp r, const
KMatrix<Tcomp&m);

/** ** *generate rotation matrices about x,y,z-axes */
friend KMatrix<doublerotX(double angle);
};
KMatrix<doubleorientierung;
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
Sep 25 '06 #2

P: n/a
>template<class Tcompclass KMatrix;
>template<class TcompKMatrix<Tcompoperator* (const Tcomp r, const
KMatrix<Tcomp>&);

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

KMatrix<doublerotX(double angle);
Okay, I've been trying with
template <class TCompKMatrix<doublerotX(double angle)

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

template <class Tcomp>
KMatrix<Tcompoperator * (const Tcomp r, const KMatrix<Tcomp&m);
>template<class Tcompclass KMatrix {
>/** * scalar multiplication */
friend KMatrix<Tcompoperator * <>(const Tcomp r, const
KMatrix<Tcomp&m);

/** * generate rotation matrices about x,y,z-axes */
friend KMatrix<doublerotX(double angle);
};
>KMatrix<doubleorientierung;
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<Tcompoperator * <>(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
Sep 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.