compiles well on borland but shows error in Dev C++...need solution .. i am experimenting with pointers on classes

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
//This Runs fine on Borland but errors are shown on Dev C++
//The errors in Dev C++
/*
Errors when compiled on Dev C++;
In function `int main()': 
83  no matching function for call to `polygon::polygon(polygon)' 
note  candidates are: polygon::polygon(polygon&)
note  polygon::polygon(int) 
83  initializing argument 1 of `polygon polygon::operator=(polygon)' 
 */

		#include <iostream.h>
		class pause{public:~pause(){cout << "\nPress Enter to continue.."; cin.get();}};
		class polygon
		{
		public:
			polygon(int);
			polygon(polygon&);
			void setSides(int);
			int getSides () const;
			polygon operator=(const polygon apolygon);
			~polygon();
			
		private:
			int * itsSides;

		};

		polygon::polygon(int sides)
		{
			cout << "CONSTRUCTOR " << this  << " is the address of polygon created\n";
			itsSides = new int;
			*itsSides = sides;
		}
		void polygon::setSides(int sides)
		{
			*itsSides = sides;
		}

		int polygon::getSides() const
		{
			return *itsSides;
		}

		polygon::~polygon()
		{
			cout << "DESTRUCTOR " << this << " is address of polygon being destroyed\n";
			delete itsSides;
		}
		
		polygon::polygon(polygon &aPolygon)
		{
			itsSides = new int;
			*itsSides = aPolygon.getSides();
			cout << "COPY CONSTRUCTOR 4 address " << this << "\n";
		}

		polygon polygon::operator=(const polygon aPolygon)
		{
			
			*(*this).itsSides = aPolygon.getSides();
			//or
			*this->itsSides = aPolygon.getSides();
			//or
			*(this->itsSides) = aPolygon.getSides();
			return *this;	
		}

		polygon withSides(int);

		polygon withSides(int sides)
		{
			polygon *temp = new polygon(sides);
			return *temp;
		}

		int main()
		{
			polygon traingle(5);
			cout << &traingle << "is the address of traingle\n\n";
			cout << "\nWrong traingle .. it has " << traingle.getSides() << " sides" << endl << endl;
			
			traingle = withSides(3); //ERROR no matching function for call to `polygon::polygon(polygon) 
			cout << &traingle << "is the address of traingle\n";
			
			cout << "\nMy traingle class is working on borland..it has " << traingle.getSides() << " sides" << endl;
			cout << &traingle << "is the address of traingle\n";
			cout << "ENd MaiN\n";

			pause here;
			return 0;
		}

 

The output on borland

CONSTRUCTOR 0x0012ff88 is the address of polygon created
0x0012ff88is the address of traingle


Wrong traingle .. it has 5 sides

CONSTRUCTOR 0x009029f8 is the address of polygon created
COPY CONSTRUCTOR 4 address 0x0012ff50
COPY CONSTRUCTOR 4 address 0x0012ff80
DESTRUCTOR 0x0012ff50 is address of polygon being destroyed
DESTRUCTOR 0x0012ff80 is address of polygon being destroyed
0x0012ff88is the address of traingle

My traingle class is working on borland..it has 3 sides
0x0012ff88is the address of traingle
ENd MaiN

Press Enter to continue..
1
2
//polygon(polygon&); //this is not a copy constructor
polygon(const polygon&);
Also it should be #include <iostream> (and now you've got namespaces)
Both compilers are outdated.

withsides() is leaking
Last edited on
Thanks......
I got it.........
polygon(const polygon&) is a copy constructor
borland takes polygon(polygon&) as a copy constructor
Also
1
2
3
4
5
polygon withSides(int sides)
{
    polygon temp(sides);
    return temp;
}
Last edited on
Topic archived. No new replies allowed.