Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > No declaration and missing operator* in gcc 4.1.1, but not icc andgcc 3.3

Reply
Thread Tools

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

 
 
Preben
Guest
Posts: n/a
 
      09-25-2006
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
 
Reply With Quote
 
 
 
 
Bernd Strieder
Guest
Posts: n/a
 
      09-25-2006
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
 
Reply With Quote
 
 
 
 
Preben
Guest
Posts: n/a
 
      09-25-2006
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Differences in GCC and ICC compiled objects? relocatable. J K C Programming 6 02-23-2012 04:38 PM
ICC using -cxxlib-gcc results in - /usr/lib/crtbeginS.o: No such file --error CoL C Programming 2 08-17-2007 02:40 AM
ICC with -cxxlib-gcc --- results in ld: /usr/lib/crtbeginS.o: No such file error Col C++ 0 08-16-2007 02:18 PM
icc, gcc sse alignement easter bunny C++ 2 07-04-2007 06:47 PM
How to force 'inline' with GCC or ICC Patrick Laurent C++ 44 10-06-2006 04:11 AM



Advertisments