Good day, I've realized hierarchy of classes: Point -> LineSegment.
It's necessary to realize the following methods of the class: moving, stretching, rotating, turning, change on an axis. I'm trying to realize moving at a some distance on this code . What should I correct for in my code that it works?

#include <iostream.h>
#include <stdlib.h>
#include <cmath>
#include <conio.h>

// Classes
class Point
{
private:
  // Members
  double x;
  double y;

public:
  // Constructor
  Point (double _x, double _y) : x(_x), y(_y) {}
};

class LineSegment
{
private:
  // Members
  Point p1; 
  Point p2; 

public:
  // Constructor
  LineSegment (Point &_p1, Point &_p2) // Create a line from 2 points;
  : p1(_p1), p2(_p2)
  { }

  bool (LineSegment &ls) {
    int dist;
    ls.p1 = ls.p1 + dist;
    ls.p2 = ls.p2 + dist;
  }
  return true;
};

int main()
{
  Point A(1, 4), B(2, 3);
  LineSegment t1(A,B);

  cout << "Class hierarchy: Point -> Segment";

  return 0;
}

[C++ Error] ClassMethods.cpp(32): E2293 ) expected

Recommended Answers

All 4 Replies

Well, this function:

bool (LineSegment &ls) {
    int dist;
    ls.p1 = ls.p1 + dist;
    ls.p2 = ls.p2 + dist;
  }

If you look at a method, in terms of some pseudocode:

function [type] [name] (parameters)
[
//... implementation

return [typy]

]

Therefore, you can think of it like this:

bool method_name (LineSegment &ls){

  //... implementation 

  return true; 
}

Just remember where to put your return, in your code, it's in the wrong place. Needs to be in the brackets.

Oh.. Thanks! I'm sitting about it for a long time and haven't noticed. After this correction

bool moving(LineSegment &ls) {
int dist;
  ls.p1 = ls.p1 + dist;
  ls.p2 = ls.p2 + dist;
return true;
}

there are following errors appeared:
[C++ Error] ClassMethods.cpp(35): E2094 'operator+' not implemented in type 'Point' for arguments of type 'int'
[C++ Error] ClassMethods.cpp(36): E2094 'operator+' not implemented in type 'Point' for arguments of type 'int'

I've tried to use friends overloading inside the class LineSegment

friend LineSegment operator+ ( const Point &, const Point & );

but it's not change nothing. What should I change for in my code?

You're trying to add an integer to a "point"-class. As this is not standard c++, the compiler is complaining that you should implement a function to teach it how to do so. If operator-overloading sounds a bit to scary for now, you could also choose to implement a addInteger member function in your point class. Something like:

class Point
{
private:
    // Members
    double x;
    double y;
public:
    // Constructor
    Point (double _x, double _y) : x(_x), y(_y) {}
    addInteger(int _i) { // do stuff }
};

Thanks for that, Nick.

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.