segmentation when cout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madshov
    New Member
    • Feb 2008
    • 20

    segmentation when cout

    Hi,

    I'm trying to overload the << operator. I have a point class which is represented by a vector of doubles. In this class I have overloaded the << operator with no problem:
    Code:
    class Point
    {
      public:
        //constructor
        Point();
        Point(vector<double> v);
    }
    
    Point::Point()
    {
       vector<double> vec(4,0);
       vec[3]= 1;
       point = vec;
       size  = vec.size();
    }
    Code:
    ostream& operator<<(ostream& os, const Point& p) 
    {
       int sz = p.get_size();
       os << "(";
       for (int i=0; i<sz; i++) {
          os << p.get_val(i) << " ";
       }
       os << ")" << endl;
       return os;
    }
    Now I have created another class, which instantiates 4 points of class Point:

    Code:
    fourpoints::fourpoints()
    {
        Point p1;
        Point p2;
        Point p3;
        Point p4;
    }
    When I try to overload the << operator here I get a segmentation fault:
    Code:
    ostream& operator<<(ostream& os, const fourpoints& p) 
    {
       os << "1. point: " << p.get_1();
       return os;
    }
    The function get_1() just returns the first point:
    Code:
    Point fourpoint::get_1() const
    {
        return this->p1;
    }
    Can anybody tell what I'm doing wrong? It fails when I try to print the coordinate in the point. The output is "1. point: (" and nothing more
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by madshov
    fourpoints::fou rpoints()
    {
    Point p1;
    Point p2;
    Point p3;
    Point p4;
    }
    These Point variables are local to this constructor. They will be destroyed when the constructor completes.

    You didn't post your fourpoints class so I can't see what is decared there.

    Comment

    • madshov
      New Member
      • Feb 2008
      • 20

      #3
      The fourpoints class:
      Code:
      class fourpoints
      { 
          public:
      
          //constructor
          fourpoints();
      	
          //destructor
      	~fourpoints();
      	
      	Point fourpoints::get_1() const;
      	friend ostream& operator<<(ostream& os, const fourpoints& p);
      	
      	private:
          Point p1, p2, p3, p4;
      };

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by madshov
        Point::Point()
        {
        vector<double> vec(4,0);
        vec[3]= 1;
        point = vec;
        size = vec.size();
        }
        Your vector vec is local to the Point constructor. It will be destroyed when the constrcutor completes.

        This is the second place you have done this. Your data members belong in the class as private members and not as local variables in your funcitons.

        Also, vec[3] will fail becuse there is no element there. You need to put elements in the vector before you can assign to them.

        Also, vector<double> vec(4,0) won't compile on my system.

        Also, point and size are not defined so these lines won't compile either.


        I suggest you fix your code so it compiles and post again.

        Comment

        • mschenkelberg
          New Member
          • Jun 2007
          • 44

          #5
          Originally posted by weaknessforcats
          Your vector vec is local to the Point constructor. It will be destroyed when the constrcutor completes.

          This is the second place you have done this. Your data members belong in the class as private members and not as local variables in your funcitons.

          Also, vec[3] will fail becuse there is no element there. You need to put elements in the vector before you can assign to them.

          Also, vector<double> vec(4,0) won't compile on my system.

          Also, point and size are not defined so these lines won't compile either.


          I suggest you fix your code so it compiles and post again.

          The two parameter constructor is defined here: http://cppreference.co m/cppvector/vector_construc tors.html

          If you left out any code post it.

          Max

          Comment

          • madshov
            New Member
            • Feb 2008
            • 20

            #6
            Hi,
            Sorry, I only pasted some parts of my code, but I found the reason for the problem myself.

            Comment

            Working...