syntax help?

This is a discussion on syntax help? within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm new to c++(got experience with C sharp and java) and i've found myself quite confused with the syntax ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    syntax help?

    Hi, i'm new to c++(got experience with C sharp and java) and i've found myself quite confused with the syntax setting up classes.
    I've been trying to port a Vector class that I wrote for a pool game I developed using Processing, however I don't know whether or not I'm on the right track with tracking down the syntax differences.

    I've been trying to work it out from the compiler error messages (probably not the best way) but my utter noobish-ness is causing me much frustration, so I was wondering if someone could have a look at what I've got so far and steer me in the right direction.

    Code:
    class Vector
    {
    public:
      float *x;
      float *y;
     
    
     Vector(const Vector &v);
     Vector(float &vX,float &vY);
     
     float vectorLength(const Vector &v);
     Vector vectorNormal(const Vector &v);
     Vector AddVect(const Vector &v1,const Vector &v2);
     Vector SubVect(const Vector &v1,const Vector &v2);
     Vector ScaleIncrease(const Vector &v, int s);
     Vector ScaleDecrease( const Vector &v, int s);
     float DotProduct(const Vector &v1, const Vector &v2 );
    };
    
    //2 constructors
     Vector::Vector(const Vector &v)
    {
      x=v.x;
      y=v.y;
    }  
    
     Vector::Vector(float &vX,float& vY)
    {
      *x=vX;
      *y=vY; 
    }
    
    //get vector length
     float Vector::vectorLength(const Vector &v)
      {
        float vX=(*v.x)*(*v.y);
        float vY=(*v.y)*(*v.y);
        return(sqrt(vX+ vY));
      }
    
    //get vector normal
     Vector Vector::vectorNormal(const Vector &v)
     {
      float temp = 1/vectorLength(v);
      *v.x *= temp;
      *v.y *= temp;
     
      return( v );
    }
    
    //return sum of 2 vectors
    Vector Vector::AddVect(const Vector &v1, const Vector &v2 )
    {
      Vector v=new Vector(0,0);
      v.x = *v1.x + *v2.x;
      v.y = *v1.y + *v2.y;
      return( v );
    }
      
    //subtract 2 vectors
    Vector Vector::SubVect(const Vector &v1, const Vector &v2 )
    {
      Vector *v=new Vector(0,0);
      *v->x = v1.x - v2.x;
      *v->y = v1.y - v2.y;
      return( v );
    }
    
    
    Vector Vector::ScaleIncrease(const Vector &v, int s )
    {
      v.x *= s;
      v.y *= s;
      return( v );
    }
    
    Vector Vector::ScaleDecrease(const Vector &v, int s)
    {
      v.x /= s;
      v.y /= s;
      return( v );
    }
    
    
    float Vector::DotProduct(const Vector &v1, const Vector &v2 )
    {
      return((v1.x*v2.x) + (v1.y*v2.y));
    }
    If anyone could help I'd be most grateful!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For your two-argument constructor, there's no good reason to use references - just use plain floats. You could also use member initializer lists in the constructors.
    Code:
       float vX=(*v.x)*(*v.y);
    These should both be v.x. You don't need vX and vY, BTW. I think your formulas for vectorNormal are wrong, unless you're using an unusual definition of normal. It would also be both more general and simpler to have a single Scale function that takes a float argument, instead of two that take integer arguments.

    Edit: Your two data member variables x and y should be float, not float *. Making this fix entails making a bunch of fixes in the rest of the code.
    Last edited by robatino; 08-07-2007 at 11:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21