Thread: strange problem with operator overloading

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    strange problem with operator overloading

    Using visual studio 2003

    In a header file I have as follows
    Code:
    class Vector2_1
    {
    private:
    	double a, b;
    public:
    	Vector2_1() {a=0; b=0;}
    	Vector2_1(double vec1, double vec2):a(vec1), b(vec2) {}
    	void SetSpecificValue(double value, int pos);
    	double GetSpecificValue(int pos) const;
    };
    
    
    class Vector1_2:public Vector2_1
    {
    public:
    	Vector1_2* Vector1_2::operator * (const Matrix2_2& M);  //This is the problematic line
    };
    
    class Matrix2_2
    {
    private:
    	double M11, M12, M21, M22;
    public:
    	Matrix2_2(double UPLEFT, double UPRIGHT, double DOWNLEFT, double DOWNRIGHT):M11(UPLEFT), M12(UPRIGHT), M21(DOWNLEFT), M22(DOWNRIGHT) {}
    	Matrix2_2() {M11=0; M12=0; M21=0; M22=0;}
    	void SetSpecificValue(double value, int row, int column);
    	double Getspecificvalue(int row, int column) const;
    
    	Matrix2_2* operator * (const Matrix2_2&);
    	Vector2_1* operator * (const Vector2_1&);
    };
    for some reason I get the following error
    error C2143: syntax error : missing ',' before '&'

    I tried to remove the CPP from the solution and leave the header only. The error disappeared. Adding the CPP with no contents but #include "Matrix.h" returns the error.

    Help?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think you are supposed to put a forward declaration of Matrix2_2 before Vector1_2. The compiler doesn't know what Matrix2_2 is when it gets to the error line.

    Also, why do you return a pointer from Vector1_2 * Matrix2_2 and exactly what would it be pointing at?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by anon View Post
    I think you are supposed to put a forward declaration of Matrix2_2 before Vector1_2. The compiler doesn't know what Matrix2_2 is when it gets to the error line.

    Also, why do you return a pointer from Vector1_2 * Matrix2_2 and exactly what would it be pointing at?
    1) Thank you!!! :-) It worked
    2)

    Code:
    Vector1_2* Vector1_2::operator * (const Matrix2_2& M)
    {
    	Vector2_1* vec2=new Vector2_1;
    	vec2->SetSpecificValue(a*M.Getspecificvalue(1,1)+b*M.Getspecificvalue(2,1),1);
    	vec2->SetSpecificValue(a*M.Getspecificvalue(1,2)+b*M.Getspecificvalue(2,2),2);
    	return vec2;
    }
    I thought of doing so in order to pass a pointer and not by value. But it is problematic.
    For instance, I will have an operator in Matrix2_2

    Matrix2_2* operator + (const Matrix2_2&);

    So I will have a problem doing

    Matrix2_2 mat1, mat2;
    mat2=mat1+mat1+mat1;

    Because the returned value from the first sum is a pointer and not const Matrix2_2&.

    What do you think I should do instead?
    Last edited by misterowakka; 01-17-2008 at 11:14 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not a good idea.

    1) Unusual syntax. When overloading operators do like integers do. The usage is not
    Code:
    int a, b;
    int* p = a * b;
    2) Who is going to deallocate the new objects or even know which objects were allocated dynamically?

    3) You may be underestimating the optimizing capabilities of compilers. Any temporary copies may be optimized away, if not otherwise then if you coded it like this:

    Code:
    Vector1_2 Vector1_2::operator * (const Matrix2_2& M) const
    {
        return Vector1_2(a*M.Getspecificvalue(1,1)+b*M.Getspecificvalue(2,1),
            a*M.Getspecificvalue(1,2)+b*M.Getspecificvalue(2,2));
    }
    (The const is added for const-correctness.)
    Last edited by anon; 01-17-2008 at 11:22 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM