Good evening everyone,
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(145) : error C2440: 'return' : cannot convert from 'Rectangle' to 'Rectangle'
Cannot copy construct class 'Rectangle' due to ambiguous copy constructors or no available copy constructor
TestingSquare.cpp(56) : error C2679: binary '<' : no operator found which takes a right-hand operand of type 'Rectangle ^' (or there is no acceptable conversion)
Rectangle.h(109): could be 'bool Rectangle::operator <(Rectangle)' while trying to match the argument list '(Rectangle ^, Rectangle ^)'
Your guidance would be really appreciated
Thank you for your help,
John
My 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 this->area > rect.area || this->area == rect.area;
}
bool operator<=(Rectangle rect)
{
return this->area < rect.area || this->area == rect.area;
}
Rectangle operator+(Rectangle rect2)
{
int newarea = area + rect2.area; //get the existing areas and sum them
int newwidth = newarea / length; //calculate new width if the length was the same
Rectangle^ sumRect = gcnew Rectangle(lt, tp, rt, bm); //make copy of the first
sumRect->bm = m_Top + newwidth; //set bottom coordinate to accommodate the new width
return *sumRect; //return the updated rectangle
}
};
[U]My derived class:[/U]
#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);
}
};
[U]My CPP file:[/U]
// TestingSquare.cpp : main project file.
#include <iostream>
#include <cstring>
#include "stdafx.h"
#include "Rectangle.h"
#include "Square.h"
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 (Rect1 < rect2)
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;
}
Was This Post Helpful? 0