0

I am not sure if I am doing the overload correctly.

...\point.h(42) : error C2061: syntax error : identifier 'Vec3' Point operator +(Vec3 a) const;

Here is my .h file:

    #include <fstream>
    #include <iostream>
    #include "vec3.h"

    using std::ofstream;
    using std::ifstream;
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;

    #ifndef POINT_H
    #define POINT_H

    //Define ourselves a basic 2D point class
    class Point
    {

        friend ofstream& operator <<(ofstream& output, const Point& p);
        friend ifstream& operator >>(ifstream& input, Point& p);
\    
        public:

        Point();
        Point(double _x, double _y);
        Point(double _x, double _y, double _z);

        double x,y,z;

        //Operators
        Point operator -(Point a) const;
        Point operator /(double s) const;
        Point operator *(double s) const;

        // Used to do vector point addition
        Point operator +(Vec3 a) const;

    };

    #endif

Here is my .cpp file

#include "point.h"

Point::Point(double _x, double _y, double _z)
{
    x= _x;
    y= _y;
    z= _z;
}

Point :: Point()
{
    x = 0.0;
    y = 0.0;
    z = 0.0;
}

Point::Point(double _x, double _y)
{
    x= _x;
    y= _y;
    z= 0;
}

    Point Point::operator -(Point a) const
    {
        return Point(x-a.x, y-a.y, z-a.z);
    }

    Point Point::operator /(double s) const
    {
        return Point(x/s, y/s, z/s);
    }

    Point Point::operator *(double s) const
    {
        return Point(x*s, y*s, z*s);
    }


// Vector Point Addition
    Point Point::operator +(Vec3 a) const
    {
        return Point(x+a.x, y+a.y, z+a.z);
    }

    ofstream& operator <<(ofstream& output, const Point& p)
    {
        output << p.x << " " << p.y << " " << p.z << "\n";
        return output;
    }

    ifstream& operator >>(ifstream& input, Point& p)
    {
        input >> p.x >> p.y >> p.z;
        return input;
    }

Here is the Vec3.h

    #ifndef VEC3_H
#define VEC3_H

#include "point.h"

class Vec3
{
    friend ofstream& operator <<(ofstream& output, const Vec3& p);
    friend ifstream& operator >>(ifstream& input, Vec3& p);

    public: 
    Vec3();
    Vec3(double _x, double _y);
    Vec3(double _x, double _y, double _z);

    double x,y,z;

    //Operators
    Vec3 operator -(Vec3 a) const;
    Vec3 operator /(double s) const;
    Vec3 operator *(double s) const;

    // Used to do vector Vec3 addition
    Vec3 operator +(Vec3 a) const;
    Point operator +(Point a) const;

};

#endif
6
  • 2
    Could you post the vec3.h file? Commented Oct 15, 2010 at 3:08
  • Hard to say without vec3.h. One thing you should note is that it's good to have const references to arguments in operators, as long as you'r passing classes and structs. Commented Oct 15, 2010 at 3:09
  • Check out for typo in vec3.h.. whether the class name is vec3 or Vec3 something lik dat.. Compiler clearly tells it can't identify Vec3... Commented Oct 15, 2010 at 3:17
  • @dutt, So I should pass in consts for my arguments? Commented Oct 15, 2010 at 3:18
  • 1
    Generally, it's bad style to have using declarations in header files, because then anybody who includes your header file gets those using declarations whether they want them or not. Instead, use fully qualified names in your header file (std::ofstream instead of just ofstream). Commented Oct 15, 2010 at 3:21

1 Answer 1

1

vec3.h includes point.h and point.h includes vec3.h. You need to remove the circular dependency, by forward declaring one of the classes.

Sign up to request clarification or add additional context in comments.

1 Comment

Good spotting! I just wrote an answer about that, too: stackoverflow.com/questions/3939151/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.