Click to See Complete Forum and Search --> : Converting Header from VC++ to MinGW


sybixsus
February 21st, 2006, 10:23 AM
Hi,

I'm trying to use a dll which is supposed to work with practically any C++ compiler ( according to the author. ) The header files supplied seem to be set up for MS VC++ though. There are a number of errors, so I'll begin with the first one.

There's a struct definition that's causing an error to start with. I've trimmed it right down to make it easier to see the problem.


typedef struct MY_VECTOR
{
float x;
float y;

float MY_VECTOR::Dot(CONST MY_VECTOR& v)
{
return x*v.x + y*v.y;
}


} MY_VECTOR;



The error I get is that the compiler is expecting a ; after Dot and before ( in the declaration.

I'm really not sure what the problem is here. It looks fine to me, with my very limited C++ skills.

Graham
February 21st, 2006, 10:47 AM
For starters, this is pretty awful C++. It looks to me like a C programmer has read a couple pages of a C++ book and decided to have a go.

I can't remember if it's actually an error, but the "MY_VECTOR::" in front of Dot() is unnecessary (and may be illegal).

As for the rest of it; based on that example, I would probably avoid using it, since the author does not appear to know how to code C++ correctly. Using typedef is utterly pointless, and is an indicator of a C programmer. Leaving the data mambers public is dangerous, and would fail any serious code review. CONST is not a C++ keyword, and, if it's a macro, then it shows more wooly thinking.

The C++ way would look more like:

class MyVector
{
public:
float Dot(const MyVector& other) const
{
return x*other.x + y*other.y;
}
float X() const { return x; }
float Y() const { return y; }

private:
float x, y;
};

And I would throw in a couple of constructors for good measure.

sybixsus
February 21st, 2006, 01:26 PM
Hi Graham,

Thanks very much for the very quick and helpful response. The compiler seems happy to permit MY_VECTOR:: in front of Dot() so it must indeed have been the CONST which was making it error. I've gone through the headers and there seem to be a lot of similar keywords in caps, such as CONST and FLOAT. Perhaps these have some relevance if you're using VC++? I'm changing them all to lower case, and that seems to be letting it compile so far.

Thanks also for the suggestions on improving the code, I will go back and do that once I have everything at a point where it compiles.

The struct does have constructors, I just left them out to make the problem easier to diagnose, since there was no error with them.

Thanks again.