I've been trying to write a class called Point, with a file Point.cpp including a header file Point.h . I tried to set a global variable of type Point called ORIGIN, however I get an error message saying "error: ‘ORIGIN’ does not name a type"

Point.h is as follows:

// Point.h

//#include <iostream>

#include <string>

#include <cmath>

#include <cstdlib>

#ifndef POINT_H

#define POINT_H

using namespace std;

class Point{

    public:

        //Point(double a, double b);

        Point();

        void setPoint(Point &p, double a, double b);

        friend istream& operator>>(istream& cin, Point& p);

        friend ostream& operator<<(ostream& cout, const Point& p);

        friend double operator - (const Point& p1, const Point& p2);

        double x,y;

    //private:    

        //double x,y;

};



extern Point ORIGIN;



istream& operator>>(istream& cin, Point& p);

ostream& operator<<(ostream& cout, const Point& p);

double operator - (const Point& p1, const Point& p2);

#endif

Point.cpp is as follows:

// Point.cpp

#include "Point.h"

Point ORIGIN;

//ORIGIN = setPoint(ORIGIN,0,0);

ORIGIN.x = 0;

ORIGIN.y = 0;

void Point::setPoint(Point& p, double a, double b){

    p.x = a;

    p.y = b;

}



Point::Point(){

    //setPoint(this,0,0);

}

istream& operator>>(istream& cin, Point& p){

    string str, str_x, str_y;

    int comma;

    cin >> str;

    comma = str.find(",");

    str_x = str.substr(1,comma-1);

    str_y = str.substr(comma,str.length()-3-str_x.length()); // -3 is from "(" "," and ")"

    p.x = atof(str_x.c_str());

    p.y = atof(str_y.c_str());

}

ostream& operator<<(ostream& cout, const Point& p){

    cout << "(" << p.x << "," << p.y << ")";

}

double operator - (const Point& p1, const Point& p2){

    double distance;

    distance = sqrt( pow( (p2.x-p1.x),2) + pow( (p2.x-p1.x),2));

    return distance;

}

I have a feeling the error has something to do with the part in Point.cpp where I try to assign values to ORIGIN's x and y member variables, although I'm not quite sure how to fix this :(

any help would be appreciated, thanks!

I managed to fix this problem, but now it seems that I'm still having a problem with overloading the " << " operator. I get an error message saying

no match for ‘operator<<’ in ‘cout << "("

What am I doing wrong here?? Again, any help would be appreciated, thanks!

I've been trying to write a class called Point, with a file Point.cpp including a header file Point.h . I tried to set a global variable of type Point called ORIGIN, however I get an error message saying "error: ‘ORIGIN’ does not name a type"

Point.h is as follows:

// Point.h

//#include <iostream>

#include <string>

#include <cmath>

#include <cstdlib>

#ifndef POINT_H

#define POINT_H

using namespace std;

class Point{

    public:

        //Point(double a, double b);

        Point();

        void setPoint(Point &p, double a, double b);

        friend istream& operator>>(istream& cin, Point& p);

        friend ostream& operator<<(ostream& cout, const Point& p);

        friend double operator - (const Point& p1, const Point& p2);

        double x,y;

    //private:    

        //double x,y;

};



extern Point ORIGIN;



istream& operator>>(istream& cin, Point& p);

ostream& operator<<(ostream& cout, const Point& p);

double operator - (const Point& p1, const Point& p2);

#endif

Point.cpp is as follows:

// Point.cpp

#include "Point.h"

Point ORIGIN;

//ORIGIN = setPoint(ORIGIN,0,0);

ORIGIN.x = 0;

ORIGIN.y = 0;

void Point::setPoint(Point& p, double a, double b){

    p.x = a;

    p.y = b;

}



Point::Point(){

    //setPoint(this,0,0);

}

istream& operator>>(istream& cin, Point& p){

    string str, str_x, str_y;

    int comma;

    cin >> str;

    comma = str.find(",");

    str_x = str.substr(1,comma-1);

    str_y = str.substr(comma,str.length()-3-str_x.length()); // -3 is from "(" "," and ")"

    p.x = atof(str_x.c_str());

    p.y = atof(str_y.c_str());

}

ostream& operator<<(ostream& cout, const Point& p){

    cout << "(" << p.x << "," << p.y << ")";

}

double operator - (const Point& p1, const Point& p2){

    double distance;

    distance = sqrt( pow( (p2.x-p1.x),2) + pow( (p2.x-p1.x),2));

    return distance;

}

I have a feeling the error has something to do with the part in Point.cpp where I try to assign values to ORIGIN's x and y member variables, although I'm not quite sure how to fix this :(

any help would be appreciated, thanks!

Hi lonelycloud,

I have been having some problems with the XXXX does not name a type and I'm interested to see how did you fix yours. Im sorry I cant help you with your other problem.

best regards!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.