c++ - It is said abstract class for a virtual function that is implemented. It works with three of four similar implementations -


it seems problem circle.cpp if code looks same in point.cpp. i've been looking @ code long time , haven't found different in 2 files. here error message:

>------ build started: project: ou5, configuration: release win32 ------ 1>  circle.cpp 1>  ou5.cpp 1>c:\users\jonas\documents\visual studio 2010\projects\ou5\ou5\shapeptr.h(52): error c2259: 'shape' : cannot instantiate abstract class 1>          due following members: 1>          'shape *shape::clone(void) const' : abstract 1>          c:\users\jonas\documents\visual studio 2010\projects\ou5\ou5\shape.h(21) : see declaration of 'shape::clone' 1>ou5.cpp(20): error c2259: 'shape' : cannot instantiate abstract class 1>          due following members: 1>          'shape *shape::clone(void) const' : abstract 1>          c:\users\jonas\documents\visual studio 2010\projects\ou5\ou5\shape.h(21) : see declaration of 'shape::clone' 1>ou5.cpp(27): error c2259: 'shape' : cannot instantiate abstract class 1>          due following members: 1>          'shape *shape::clone(void) const' : abstract 1>          c:\users\jonas\documents\visual studio 2010\projects\ou5\ou5\shape.h(21) : see declaration of 'shape::clone' 1>  point.cpp 1>  polygon.cpp 1>  rectangle.cpp     ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

the code base class pure virtual function:

#ifndef shape_h #define shape_h using namespace std; #include <iostream> #include <ostream>  class shape { public:     shape()     {         cout << "shape constructor" << endl;     }     shape(double xval, double yval)     {         x = xval;         y = yval;     }       virtual shape* clone() const = 0;     virtual double area()     {         return 1;     }     virtual void print() const{}     virtual ostream& printf(ostream &os){return os;}     /* // copy constructor     shape(const shape *sp)     {         // copy string our newly allocated memory         sp =this->clone();     }*/      /*shape & operator=(const shape & sptr)         {     if(this!= &sptr)     {         *this = sptr.clone();     }     return *this; }*/      double getx() const     {         return x;     }     double gety() const     {         return y;     }     virtual ~shape() {cout << "shape raderas av destruktor" <<endl; }         protected:     double x;       //attribut     double y;       //attribut     shape *sp;  };  #endif 

point.h working:

#ifndef point_h #define point_h #include "shape.h" using namespace std; #include <iostream> #include <fstream> class point : public shape { public:     point()     {         cout << "point constructor" << endl;     }     point(double x, double y, double sz) : shape(x,y), size(sz)     {         cout << "point constructor" << endl;     }     void las(istream &is)     {         >> x >> y >> size;     }     ostream& printf(ostream &os) ;     void print() const;      double area();     shape* clone() const;     virtual ~point(){cout << "point raderas av destruktor" << endl;} private:     double size;        //attribut }; #endif 

point.cpp:

#include "point.h" #include <ostream> using namespace std;  shape* point::clone() const { return new point( *this); }  void point::print() const     {      cout << "point: ";     cout <<"(" << this->x<<"," << this->y << ")" << this->size << endl;     } ostream& point::printf(ostream &os)      {     os << "point: ";     os <<"(" << this->x<<"," << this->y << ")" << this->size << endl;     return os;     }      double point::area()     {         return size;     } 

the files problem. first circle.h:

#ifndef circle_h #define circle_h #include "shape.h" using namespace std; #define m_pi 3.141592654 #include <fstream> #include <iostream> class circle : public shape { public:     circle()     {         cout << "circle constructor" << endl;     }     circle(double x, double y, double rad) : shape(x,y), radie(rad)     {         cout << "circle constructor" << endl;     }     void las(istream &is)     {         >> x >> y >> radie;     }     ostream& printf(ostream &os);     double area();      void print() const;     shape* clone() const;     virtual ~circle(){cout << "circle raderas av destruktor" << endl;} private:     double radie;       //attribut }; #endif 

then circle.cpp:

#include "circle.h" using namespace std; #include <ostream>  shape* circle::clone() const {return new circle( *this);} double circle::area()     {         double tmparea;         tmparea = m_pi * radie * radie;         return tmparea;     }     void circle::print() const     {     cout << "circle: ";     cout <<"(" << this->x<<"," << this->y << ")" << this->radie << endl;     }     ostream& circle::printf(ostream &os)      {     os << "circle: ";     os <<"(" << this->x<<"," << this->y << ")" << this->radie << endl;     return os;     } 

the problem clone , looks same in file working , not working file. working file point.cpp works 2 other files not posted here.

i add shapeptr.h here:

#ifndef shapeptr_h #define shapeptr_h #include "shape.h" #include "circle.h" #include "point.h" #include "polygon.h" #include "rectangle.h" #include <ostream> #include <istream> #include <vector> #include <fstream> #include <string> using namespace std;    class shapeptr {  public:      shapeptr()      {          sptr = 0;          numshapes = numshapes + 1;       }   shapeptr(shape * v)//: shape(v->getx(),v->gety())   {       sptr = v;       numshapes = numshapes + 1;    }   // copy constructor shapeptr(const shape &sp) {         // copy string our newly allocated memory         sptr = sp.clone(); } const shapeptr & shapeptr::operator=(const shape & sp) {     if(this->sptr!= &sp)     {         sptr = sp.clone();     }     return *this; }   ~shapeptr(){       numshapes = numshapes - 1;         cout << "shapeptr raderas av destruktor" << endl;   }     shape getshape()  {      return *sptr;  }  private:   shape *sptr; public:   static int numshapes;    friend istream& operator>>(istream &is, shapeptr & sp);   friend ostream& operator<<(ostream &os, const shapeptr & sp); };  int shapeptr::numshapes = 0;  ostream& operator<<(ostream &os, const shapeptr & sp) {     os << sp.sptr->printf(os);     return os; }  istream& operator>>(istream &is, shapeptr & sp) {     string check;     while(!is.eof())     {         >> check;         if(check == "circle")         {         circle cir;         cir.las( );         sp = shapeptr(new circle(cir));         return is;          }         if(check == "polygon")         {         polygon pol;         pol.las( );         sp = shapeptr(new polygon(pol));         return is;          }         if(check == "point")         {         point poi;         poi.las( );         sp = shapeptr(new point(poi));         return is;          }         if(check == "rectangle")         {         rectangle rec;         rec.las( );         sp = shapeptr(new rectangle(rec));         return is;          }     }      }   #endif 

based on comments attempting make instance of abstract class in shapeptr.h not valid. point attempt create instance here:

shape getshape() {      return *sptr; } 

as mark b. pointed out fix change return type shape&.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -