classes

This is a discussion on classes within the C++ Programming forums, part of the General Programming Boards category; Hi, trying to teach myself C++ and have ran into the following problem...... I have created a class "point" to ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    classes

    Hi,
    trying to teach myself C++ and have ran into the following problem......

    I have created a class "point" to hold a coordinate. I then have another class "circle" which records a radius and a coordinate - the coordinate instantiated in the "point" class.

    The two classes are as follows:-

    class point {
    private:
    int x;
    int y;

    public:
    void init(int x_coord, int y_coord);
    void print();
    };

    class circle {
    private:
    int radius;
    point centre;

    public:
    void init(int new_radius, point new_centre);
    void print();
    int get_radius();
    point get_centre();
    };

    I then instantiate the point and the circle as follows....


    int main() {
    point point1;
    circle newCircle;

    point1.init(3,4);
    newCircle.init(5, point1);

    cout << "The coords are : ";
    point1.print();

    However, when I try to output the centre of the circle (the coordinate) using...

    cout << "\tcentre: " << newCircle.get_centre() << "\n";

    I get an error message reading...

    binary '<<' : no operator defined which takes a right-hand operand of type 'class point' (or there is no acceptable conversion) (new behavior; please see help)

    the member functions are as follows:-

    void circle::init(int new_radius, point new_centre) {
    radius = new_radius;
    centre = new_centre;
    }

    void point::init(int x_coord, int y_coord) {
    x = x_coord;
    y = y_coord;
    }

    void point:rint() {
    cout << x << "," << y << "\n";
    }

    int circle::get_radius() {
    return radius;
    }

    point circle::get_centre() {
    return centre;
    }

    Any ideas greatly appreciated!!!!!!!

    Bill

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    cout doesn't know how to print a class with two members, so it causes an error. So what you need to do is either overload the << operator for point or use its print method.
    Code:
    cout << "\tcentre: "; 
    newCircle.get_centre().print();

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    that brings up the same error message. Any other ideas?

    Thanks,

    Bill

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    Sorry, it does work!!!!!!!!!

    Thanks,

    Bill

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    As a matter of interest, how would I overload the << operator?

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    If you get the same error then you're not doing what I suggested. Here is the code that compiles and runs perfectly for me.
    Code:
    #include <iostream>
    using namespace std;
    
    class point { 
    private: 
      int x; 
      int y; 
      
    public: 
      void init(int x_coord, int y_coord); 
      void print();
    }; 
    
    class circle { 
    private: 
      int radius; 
      point centre; 
      
    public: 
      void init(int new_radius, point new_centre); 
      void print(); 
      int get_radius(); 
      point get_centre(); 
    };
    
    void circle::init(int new_radius, point new_centre) { 
      radius = new_radius; 
      centre = new_centre; 
    } 
    
    void point::init(int x_coord, int y_coord) { 
      x = x_coord; 
      y = y_coord; 
    } 
    
    void point::print() { 
      cout << x << "," << y << "\n"; 
    } 
    
    int circle::get_radius() { 
      return radius; 
    } 
    
    point circle::get_centre() { 
      return centre; 
    }
    
    int main() { 
      point point1; 
      circle newCircle; 
      
      point1.init(3,4); 
      newCircle.init(5, point1); 
      
      cout << "The coords are : "; 
      point1.print(); 
      
      cout << "\tcentre: "; 
      newCircle.get_centre().print();
    }

  7. #7
    Unregistered
    Guest
    in public access portion of point class add a line like this to declare the overloaded operator:

    friend ostream & operator<<(const ostream & _cout, const point & pt)

    and then outside of class provide a definition like one of the two provided below:

    ostream & operator<<(const ostream & _cout, const point & pt)
    {
    pt.print();

    or

    _cout << pt.getX() << ',' << pt.getY();
    }

    although the latter requires addition of public accessor functions as well. The latter is what I have seen more often however. Once you have this then you can do this in main() or wherever:

    point pt1;
    //initialize pt1 somehow;
    cout << pt1;

  8. #8
    Unregistered
    Guest
    oh yeach, and add line:

    return _cout;

    after whichever line you choose in definition of <<.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21