0

have a problem with types while assign, for example i have type point

class point:public pair<int, int>
{
    private: int index;
    public: point(int);

    point(int, int, int);

    int GetIndex();

    float GetDiff(point);

};

point::point(int Index): index(Index) {};

point::point(int x, int y, int Index): index(Index)
{
    first = x;
    second = y; 
}

int point::GetIndex()
{
    return index;
}

float point::GetDiff(point Point)
{
     return pow(pow(Point.first-first,2.0f) + pow(Point.second-second,2.0f),0.5f);
}

it's compile correct, and work well [i think)] but when i want to use it, i get a error, it's code that use this class(point)

class Line
{
    public:
    Line();
    point firstPoint;
    point secondPoint;
};
Line::firstPoint = point(0); // i get error, same as on line 41
//and for example

struct Minimal
{
    Minimal();
    Line line();
    void SetFirstPoint(point p)
    {
        line.firstPoint = p;//41 line, tried point(p), same error. 
        UpdateDist();
    }
    void SetSecondPoint(point p)
    {
        line.secondPoint = p;
        UpdateDist();
    }
    void UpdateDist(void)
    {
        dist = line.firstPoint.GetDiff(line.secondPoint);
    }
    float dist;
};

and where is the error that give me gcc compiler

|41|error: 'firstPoint' in 'class Line' does not name a type|
araknoid
  • 3,065
  • 5
  • 33
  • 35
john
  • 3
  • 1

1 Answers1

0

Notice that this line:

Line line();

Does not declare a member variable of type Line, but rather a function called line that returns an object of type Line. Therefore, either this code:

line.firstPoint = p;

Is meant to be as follows (which would make little sense, because you would be modifying a temporary):

line().firstPoint = p;

Or (most likely) the declaration above is just meant to be:

Line line; // Without parentheses

Moreover, the reason why you get an error here:

Line::firstPoint = point(0);

Is that firstPoint is not a static member variable of class Line. You first need an instance of Line whose firstPoint member you could modify.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • ok) thanks, it works) but i have now: Minimal rez; i get: undefined reference to `Minimal::Minimal()'|, how to fix it? – john Mar 02 '13 at 15:12
  • @john: 1. Provide a definition for it (you are only declaring it); 2. I think you should really read an introductory book on C++ or some tutorial at least :-) – Andy Prowl Mar 02 '13 at 15:17
  • @john: Have a look at this list: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Andy Prowl Mar 02 '13 at 16:04