Problem with copy constructor in linked list

Hello !
I have problems to put a list inside a linked list. I appriciate any code given as help.
I have the following conditions to fulfill:
- main.cpp can NOT be changed
- There must be an abstract base class 'shape'
- Methods such as area() and print() must be in code
- Constructors is as follow:

1
2
3
4
Point( double x, double y, double size)
Circle( double x, double y, double radie)
Rectangle( double x, double y, double width, double height)
Polygon( double x, double y, Vertex *varr, int num)


- There must exist a Linked list 'ShapeList':
1
2
3
4
5
6
7
ShapeList()
ShapeList( const ShapeList &shapes)
~ShapeList()
add( const Shape& s )
remove( const Vertex &v)
area()
print()


This is what I've created so far (Shape, ShapeList, Vertex):

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
#include "shapelist.h"
#include "vertex.h"
#include "shape.cpp"

int main(){
  ShapeList list;
  Vertex varr[] = { Vertex(0,0), Vertex(10,0),
                    Vertex(5,2), Vertex(5,5) };
  list.add( Polygon( 1, 4, varr, 4 ) );
  list.add( Rectangle( 4, 10, 2, 4) );
  list.add( Circle( 5,5, 3)  );
  list.add( Point( 6, 7, 1 ) );
  list.print();
  cout << " Total area: " << list.area() << endl;

  ShapeList list2(list); <-- My problem is here !

  list2.print();
  cout << " Total area: " << list2.area() << endl;
  list.remove( Vertex(5,5) );
  list.print();
  cout << " Total area: " << list.area() << endl;
  list2.print();
  cout << " Total area: " << list2.area() << endl;
  return 0;
}


Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
#include <string>
#include <sstream>
#include "vertex.h"

class Shape{
protected:
Vertex *polSize, *fp, *Vx;
Shape* Nxt;

public:
    Shape() {}
    ~Shape() {}
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual double area() const = 0;figur
    virtual void Tabort(Vertex& Vrtx) const = 0;
    friend class ShapeNode;
};
#endif 


Shape.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include "vertex.h"
#include "Shape.h"
using namespace std;

class Polygon : public Shape {
public:
   Polygon(int Xpol = 0, int Ypol = 0, Vertex* varr = 0, int num = 0){
    if (num > 0) {
      polSize = new Vertex[1];
      polSize[0] = Vertex(num);
      fp = new Vertex[1];
      fp[0] = Vertex(Xpol, Ypol);
      Vx = new Vertex[num];
      for (int ix=0; ix<num; ix++){
          Vx[ix] = varr[ix];
	  }
    }
    else Vx = 0;
   }
   virtual ~Polygon() {
      delete[] polSize;
      delete[] Vx;
      delete[] fp;
   }
   virtual Shape* clone() const {
      return new Polygon(fp[0].postX(), fp[0].postY(), Vx, polSize[0].postZ());
   }
   virtual void print() const {
       cout << "POLYGON: (" << fp[0].postX() << ", " << fp[0].postY() << ")   { ";
       for (int ix=0; ix<polSize[0].postZ(); ix++){
          cout << "(" << Vx[ix].postX() << ", " << Vx[ix].postY() << ")  ";
       }
       cout << "}" << endl;
    }
   virtual double area() const {
      double s = 0.0;
      for (int ix=1; ix<polSize[0].postZ(); ix++){
         s += (Vx[ix -1].postX() * Vx[ix].postY()) - (Vx[ix].postX() * Vx[ix -1].postY());
      }
      return abs(s / 2);
   }
   virtual void Tabort(Vertex& Vrtx) const {
      for (int ix=0; ix<polSize[0].postZ(); ix++) {
        if (Vrtx.postX() == polSize[ix].postX() && Vrtx.postY() == polSize[ix].postY()) {
          delete polSize;
        }
      }
   }
};

class Rectangle : public Shape {
public:
  Rectangle(int p1 = 0, int p2 = 0, int p3 = 0, int p4 = 0){
    Vx = new Vertex[2];
    Vx[0] = Vertex(p1, p2);
    Vx[1] = Vertex(p3, p4);
  }
    virtual ~Rectangle() {
      delete[] Vx;
    }
    virtual Shape* clone() const {
      return new Rectangle(Vx[0].postX(), Vx[0].postY(), Vx[1].postX(), Vx[1].postY());
    }
    virtual void print() const {
      cout << "RECTANGLE: (" << Vx[0].postX() << ", " << Vx[0].postY() << ") (" << Vx[1].postX() << ", " <<  Vx[1].postY() << ")" << endl;
    }

   virtual double area() const {
   double a = 0.0;
   a = Vx[1].postY() * Vx[1].postX();
   return a;
   }

   virtual void Tabort(Vertex& Vrtx) const {
      for (int ix=0; 2; ix++){
        if (Vrtx.postX() == Vx[ix].postX() && Vrtx.postY() == Vx[ix].postY()) {
          delete Vx;
        }
      }
   }
};

class Circle : public Shape{
public:
  Circle(int c1 = 0, int c2 = 0, int c3 = 0){
    Vx = new Vertex[2];
    Vx[0] = Vertex(c1, c2);
    Vx[1] = Vertex(c3);
  }
   virtual Shape* clone() const {
     return new Circle(Vx[0].postX(), Vx[0].postY(), Vx[1].postZ());
   }
   virtual void print() const {
     cout << "CIRCLE: (" << Vx[0].postX() << ", " << Vx[0].postY() << ") " << Vx[1].postZ() << endl;
   }
   virtual double area() const {
   double a;
   a = Vx[1].postZ() * Vx[1].postZ() * M_PI;
   return a;
   }
   virtual void Tabort(Vertex& Vrtx) const {
        if (Vrtx.postX() == Vx[0].postX() && Vrtx.postY() == Vx[0].postY()) {
          delete Vx;
        }
      }
};

class Point : public Shape{
public:
  Point(int c1 = 0, int c2 = 0, int c3 = 0){
    Vx = new Vertex[2];
    Vx[0] = Vertex(c1, c2);
    Vx[1] = Vertex(c3);
  }
    virtual Shape* clone() const {
      return new Point(Vx[0].postX(), Vx[0].postY(), Vx[1].postZ());
    }
    virtual void print() const {
       cout << "POINT: (" << Vx[0].postX() << ", " << Vx[0].postY() << ") " << Vx[1].postZ() << endl;
    }

    virtual double area() const {
      return Vx[1].postZ();
    }
   virtual void Tabort(Vertex& Vrtx) const {
        if (Vrtx.postX() == Vx[0].postX() && Vrtx.postY() == Vx[0].postY()) {
          delete Vx;
        }
   }
};


ShapeList.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef SHAPELIST_H
#define SHAPELIST_H
#include "vertex.h"
#include "Shape.h"

class ShapeNode;
class ShapeList{

protected:
    ShapeNode* ny;
    ShapeList* NwLst;
    Shape* NewSh;

public:
    static int Rkn;
    static int AntLst;
    ShapeList();
    ShapeList(const ShapeList &listofShapes);
    ~ShapeList();
    void add(const Shape &s);
    void remove(const Vertex &v);
    double area();
    void print();
    ShapeList& operator=(const ShapeList &SpLst);
};

#endif 


ShapeList.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <string>
#include <iostream>
#include "vertex.h"
#include "ShapeList.h"
using namespace std;

class ShapeNode {
public:
  const Shape* s;
  ShapeNode* bak;
  ShapeNode* fram;
  ShapeNode(){
      fram = NULL;
      s = NULL;
  }
  ShapeNode(const Shape* _s, ShapeNode* _bak, ShapeNode* _fram) : s(_s), bak(_bak), fram(_fram) {}
  ~ShapeNode() {}

/*  void radera(const Vertex& v) const { <-- Does not work
    Vertex vx = v;
    Shape* pk;
    pk->Tabort(vx);*/
  }
};

int ShapeList::Rkn = 0;

ShapeList::ShapeList() {
  ny = new ShapeNode(0, 0, 0);
  ny->fram = ny->bak = ny;
}
ShapeList::ShapeList(const ShapeList &listofShapes){} //<- Copy constructor is needed !!!


ShapeList::~ShapeList() {}

void ShapeList::add(const Shape& s){
  Rkn++;
  const Shape* tmpPek = &s;
  tmpPek = tmpPek->clone();
  ShapeNode *tmp = ny;
  ny = new ShapeNode(tmpPek, 0, tmp);
  tmp->bak = ny;
}
/*void ShapeList::remove(const Vertex& v) { <- Does not work !
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      Cnt++;
      p->radera(v);
      p->s->Tabort(v);
  }
} */
double ShapeList::area() {
  double TotArea;
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      TotArea += p->s->area();
      Cnt++;
  }
  return TotArea;
}
void ShapeList::print() {
int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      p->s->print();
      Cnt++;
  }
}


vertex.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef VERTEX_H
#define VERTEX_H
#include <iostream>

class Vertex{
private:
    double pos_x, pos_y, pos_z;
public:
    Vertex();
    Vertex(double z);
    Vertex(double x, double y);
    double postX(){return pos_x;}
    double postY(){return pos_y;}
    double postZ(){return pos_z;}
    ~Vertex();
};

#endif 


vertex.cpp:
1
2
3
4
5
6
7
8
#include "vertex.h"
#include <iostream>
using namespace std;

Vertex::Vertex() : pos_x(0), pos_y(0) {}
Vertex::Vertex(double z) : pos_z(z){}
Vertex::Vertex(double x, double y) : pos_x(x), pos_y(y) {}
Vertex::~Vertex() {}
Last edited on
http://www.cplusplus.com/forum/general/112111/

- missing virtual destructor in `Shape'
- `Point', `Circle', `Rectangle', `Polygon' lack a proper copy constructor and assignment operator
- ¿why does a Shape have a `Shape* Nxt;' member?
- your `ShapeList' copy constructor is empty...


> main.cpp can NOT be changed
you've got #include "shape.cpp" in main.cpp
that's a conceptual error about how the build process work http://www.cplusplus.com/forum/general/113904/


Also, I'm too lazy to reproduce your file structure. As an alternative you could use github, upload a zip with all the files, or try to reproduce your problem in just one file
Topic archived. No new replies allowed.