-
Porting from g++ to VC6
Hello! I'm presently trying to port a school project from g++ to VC and I'm having a few problems. Right now I'm trying to port a simple vector class.
#include <stdio.h> //sprintf
#include <math.h>
#include <iostream.h>
#include <limits.h>
class vector2 {
public:
...constructors
...class functions
//For input and output
istream &input(istream &is) {
is.ignore(INT_MAX,'(');
is >> x;
is.ignore(INT_MAX,',');
is >> y;
is.ignore(INT_MAX,')');
return is;
}
};
this generates errors:
>syntax error : missing ';' before '&'
>'istream' : missing storage-class or type specifiers
Are there a lot of differences in syntax within class declarations in VC compared to g++?
Do I have to tell the linker to include the math library somhow?
Well, any help is appreciated:)
If you know of any web pages dealing with moving from gcc&g++ to MSVC feel fre to point me to em:D
-
What you have posted compiles fine for me (in VC++ 6.0). You will have to post more code or post the exact compiler error including line #'s (and the code on those lines).
gg
-
Opps should've tried that myself before I posted...doh
Anyway..this is the entire vector class
Below is a sample cpp file and the entire vectorclass.
Thank you very much for taking your time with this btw.
/*--------------------------------------------------
Sample.cpp FRAX
--------------------------------------------------*/
#include "vector2.h"
void main()
{
vector2 v2TestVector1(0.1,0.4);
return;
}
And this is the vector class
/////////////////////////////////////////////////////////////////////////////
// Filename: vector2.h
// Description: 2D vector class
/////////////////////////////////////////////////////////////////////////////
#ifndef __vector2_h
#define __vector2_h
#include <stdio.h> //sprintf
#include <math.h> //sqrt[abs],acos[angle]
#include <iostream.h>
#include <limits.h>
class vector2 {
public:
float x,y;
public:
vector2():x(),y() {}
vector2(float xx,float yy):x(xx),y(yy) {}
vector2(const vector2 &v):x(v.x),y(v.y) {}
// [] operator to get elements
float &operator [](int i) {return (&x)[i];}
const float &operator [](int i) const {return (&x)[i];}
//assignment operator
vector2 &operator =(const vector2 &v) {x=v.x; y=v.y; return *this;}
//relation operators
bool operator ==(const vector2 &p) const {return ((x==p.x)&&(y==p.y));}
bool operator !=(const vector2 &p) const {return !((*this)==p);}
//simple arithmetics
vector2 &operator+=(const vector2 &p) {x+=p.x;y+=p.y;return *this;}
vector2 operator + (const vector2 &p) const {return vector2(x+p.x,y+p.y);}
vector2 &operator-=(const vector2 &p) {x-=p.x;y-=p.y;return *this;}
vector2 operator - (const vector2 &p) const {return vector2(x-p.x,y-p.y);}
vector2 operator - () const {return vector2(-x,-y);}
vector2 &operator*=(float f) {x*=f;y*=f;return *this;}
vector2 operator * (float f) const {return vector2(x*f,y*f);}
vector2 &operator/=(float f) {x/=f;y/=f;return *this;}
vector2 operator / (float f) const {return vector2(x/f,y/f);}
//scalar product
float operator * (const vector2 &v2) const {return x*v2.x+y*v2.y;}
//z component of vector product
float operator ^ (const vector2 &v2) const {return x*v2.y-y*v2.x;}
//abs by definition
float abs() const {return sqrt((*this)*(*this));}
//For input and output
istream &input(istream &is) {
is.ignore(INT_MAX,'(');
is >> x;
is.ignore(INT_MAX,',');
is >> y;
is.ignore(INT_MAX,')');
return is;
}
virtual ostream &output(ostream &os) const {
os.precision(3);
os << "vector2(" << x << ", " << y << ")";
return os;
}
};
//wrong order multiply
inline vector2 operator *(float f, const vector2 &v) {return v*f;}
inline istream &operator >>(istream &is,vector2 &v) {
return v.input(is);
}
inline ostream &operator <<(ostream &os,vector2 const &v) {
return v.output(os);
}
#endif //__vector2_h
/////////////////////////////////////////////////////////////////////////////
// END OF FILE //
/////////////////////////////////////////////////////////////////////////////
-
Compiles fine for me (apart from a few warnings). Whats not working for you?
Also, when you post code, use code tags.
gg
-
Strange
LOL, what a weird problem this has been. I think it's my VC6 setup that's playing tricks on me.
I think it might be because I used some strange variant of STLport when I compiled some CVS code earlier. I also installed a new platform SDK and probably messed things up there as well.
I removed these libraries from my include path but now I seem to get problems with the linker instead of syntax errors.
Perhaps I should give my computer the stab of death and just reinstall everything.
Thank you very much for your help. Without it I would never have found this out:)
And I'll use the code tags in the future:)
Edit: Now it works great, finaly I can continue:)