Good evening everyone,

I would like to ask about copy constructors.
I have been working on a base\derived class C++ program in which I am trying to compare the areas of a couple of Rectangles, and once I get past the errors I am currently having, I wouldn't mind comparing the Square to the rectangle, but first things first.

Now I am having trouble with the following error messages:

- Rectangle.h(121) : error C2664: 'Rectangle::operator >' : cannot convert parameter 1 from 'Rectangle' to 'Rectangle'
        Cannot copy construct class 'Rectangle' due to ambiguous copy constructors or no available copy constructor

- Rectangle.h(121) : error C2664: 'Rectangle::operator ==' : cannot convert parameter 1 from 'Rectangle' to 'Rectangle'
        Cannot copy construct class 'Rectangle' due to ambiguous copy constructors or no available copy constructor

- Rectangle.h(126) : error C2664: 'Rectangle::operator <' : cannot convert parameter 1 from 'Rectangle' to 'Rectangle'
        Cannot copy construct class 'Rectangle' due to ambiguous copy constructors or no available copy constructor

- Rectangle.h(126) : error C2664: 'Rectangle::operator ==' : cannot convert parameter 1 from 'Rectangle' to 'Rectangle'
        Cannot copy construct class 'Rectangle' due to ambiguous copy constructors or no available copy constructor

But I don't understand why I need to implement a copy constructor, and I really don't know how to implement it.

Your guidance would be really appreciated :)

Thank you,

John

My Code:
Base class:

#pragma once

ref class Rectangle
{
public:
    int m_Left;
    int m_Top;
    int m_Right;
    int m_Bottom;


    // Constructor definitions
    //no parameter constructor
Rectangle(){}

    //constructor with 4 parameters 
Rectangle(int lt, int tp, int rt, int bm)

{
m_Left = lt;
m_Top = tp;
m_Right = rt;
m_Bottom = bm;
}

//Defining the property for obtaining the LeftValue
    property int lt 
    {
        int get() 
        { 
            return m_Left;
        }
        void set(int value) { m_Left = value; }
    }

//Defining the property for obtaining the TopValue
    property int tp 
    {
        int get() 
        { 
            return m_Top;
        }
        void set(int value) {m_Top = value;}
    }

//Defining the property for obtaining the RightValue
    property int rt 
    {
        int get()
        { 
            return m_Right;
        }
        void set(int value) {m_Right = value;}
    }

//Defining the property for obtaining the BottomValue
    property int bm     
    {
        int get() 
        { 
            return m_Bottom; 
        }
        void set(int value) {m_Bottom = value;}
    }

//Defining the property for obtaining the length
    property int length 
    {
        int get()
        {
            return m_Right - m_Left; 
        }
    }

//Defining the property for obtaining the width
    property int width  
    {
        int get()
        {
            return m_Bottom - m_Top;
        }
    }

//Defining the property for obtaining the area
    property long area  
    {
        long get() 
        { 
            return (m_Right - m_Left)*(m_Bottom - m_Top);
        } 
    }

//define moveRect
   void moveRect (int lt, int tp )                 
   {
          m_Bottom = tp + (m_Bottom-m_Top); 
          m_Top = tp;
          m_Right = lt + (m_Right-m_Left);
          m_Left = lt;
   }


//function to compare a rectangle 
  bool operator>(Rectangle rect)
    {
        return this->area > rect.area;
    }

  bool operator<(Rectangle rect)
    {
        return this->area < rect.area;
    }

  bool operator==(Rectangle rect)
    {
        return this->area == rect.area;
    }

  bool operator>=(Rectangle rect)
    {
        return (operator>(rect) || operator==(rect));
    }

  bool operator<=(Rectangle rect)
    {
        return (operator<(rect) || operator==(rect));
    }

};

Derived Class:

#pragma once
#include <iostream>                    // For stream I/O
#include <cstring> 
#include "Rectangle.h"

ref class CSquare: Rectangle
{
public:
    int m_Left;
    int m_Top;
    int m_Right;
    int m_Bottom;

CSquare(int lt, int tp, int rt, int bm)
{
 m_Left = lt;
 m_Top = tp;
 m_Right = rt;
 m_Bottom = bm;
}

 //LEft value function
 long LeftValueS()
 {
 return (m_Left);
 }

 //top function value
 long TopValueS()
 {
 return (m_Top);
 }

 //Right value function
 long RightValueS()
 {
 return (m_Right);
 }

 //top function value
 long BottomValueS()
 {
 return (m_Bottom);
 }

};

My CPP:

// TestingSquare.cpp : main project file.
#include <iostream>                    // For stream I/O
#include <cstring>                     // For strlen() and strcpy()
#include "stdafx.h"
#include "Rectangle.h"
#include "Square.h"                  // For CBox and CCandyBox

using namespace System;

int main()
{
Rectangle^ Rect1 = gcnew Rectangle(11,22,33,44);

//Rect values displayed
Console::WriteLine("Rect1 Values displayed:");
Console::WriteLine("Rect1 LeftValue:{0}",Rect1->lt);
Console::WriteLine("Rect1 TopValue:{0}",Rect1->tp);
Console::WriteLine("Rect1 RightValue:{0}",Rect1->rt);
Console::WriteLine("Rect1 BottomValue:{0}",Rect1->bm);
Console::WriteLine("Rect1 Length:{0}",Rect1->length);
Console::WriteLine("Rect1 Width:{0}",Rect1->width);
Console::WriteLine("Rect1 Area:{0}",Rect1->area);
Console::WriteLine("");
Console::WriteLine(L" ");
Rect1->moveRect(55,66);

 // Rect values after being moved by moveRect function to (55,66)
      Console::WriteLine(L"Rect1 values after being moved to (55,66) are as follows:");
      Console::WriteLine(L"Rect1 TopValue is: {0}", Rect1->tp);
      Console::WriteLine(L"Rect1 LeftValue is: {0}", Rect1->lt);
      Console::WriteLine(L"Rect1 BottomValue is: {0}", Rect1->bm);
      Console::WriteLine(L"Rect1 RightValue is: {0}", Rect1->rt);
      Console::WriteLine(L"Rect1 LengthValue is: {0}", Rect1->length);
      Console::WriteLine(L"Rect1 WidthValue is: {0}", Rect1->width);
      Console::WriteLine(L"Rect1 area is: {0}",Rect1->area);
      Console::WriteLine();

//Square values displayed smaller than Rect1 in this case it's 100 the Rect1 is 8820
 //Rectangle rect2 = Rectangle(10, 10, 20, 20);
Rectangle^ rect2 = gcnew Rectangle(10, 10, 20, 20); 
      Console::WriteLine("Rect2 Values displayed:");
      Console::WriteLine(L"Rect2 TopValue is: {0}", rect2->tp);
      Console::WriteLine(L"Rect2 LeftValue is: {0}", rect2->lt);
      Console::WriteLine(L"Rect2 BottomValue is: {0}", rect2->bm);
      Console::WriteLine(L"Rect2 RightValue is: {0}", rect2->rt);
      Console::WriteLine(L"Rect2 LengthValue is: {0}", rect2->length);
      Console::WriteLine(L"Rect2 WidthValue is: {0}", rect2->width);
      Console::WriteLine(L"Rect2 area is: {0}",rect2->area);
      Console::WriteLine("");

//   Compare rectangles
    /*if (Rect1 == rect2)
        Console::WriteLine("The area of the Rectangles are equal");
    if (Rect1 > rect2)
        Console::WriteLine("Rect1 area is greater than Rect2 area");
    else if (rect2 > Rect1)
        Console::WriteLine("Rect2 area is greater than Rect1 area");*/


 //Square values displayed
 CSquare Square(5,5,10,10);
 Console::WriteLine("Square Left value:{0}",Square.LeftValueS());
 Console::WriteLine("Square Top value:{0}",Square.TopValueS());
 Console::WriteLine("Square Right value:{0}",Square.RightValueS());
 Console::WriteLine("Square Bottom value:{0}",Square.BottomValueS());

return 0;
}

Is this c++? I don't think ref class CSquare: Rectangle or property int lt are valid c++ statements.

Dave

a copy ctor takes an argument of the same type.
So for example,
say you have a Date object/class:

Date {
   public:
   Date(); // default ctor
   Date(const Date&) // copy ctor
   ...
   private:
      int day, month, year;
}

// Defines Date default ctor
Date::Date(){
   int day = 0;
   int month = 0;
   int year = 0;
}

// Defines Date copy ctor
Date::Date ( const Date& arg ) {
   day = arg.day;
   month = arg.month;
   year = arg.year;
}

Date( const Date &) allows you to do something like:

Date d1;
...
(assign a date to d1)
...
Date d2(d1); // D2 assigned D1 values.

If your functions pass by reference, then I think they make copies of the objects passed and probably require a copy ctor. But I could be wrong.

Is this c++? I don't think ref class CSquare: Rectangle or property int lt are valid c++ statements.

Dave

It's C++/CLI. If one needs .NET winforms in a C++ application it's basically the only avenue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.