Three errors on overload pb

Been working on this problem about overloading function and private function but I got a couple of errors that I trying to solve but it's not successful until now.

Here the pb :

Enhance the Rectangle class with a private member function
called initName(char *). It should be the only function which
dynamically allocates a char array to hold the name. Your constructor functions should be the only functions which call it, and each constructor should call it instead of allocating their own arrays.

Add a function called setName(char *) which sets the name of the
rectangle to a new name (it does not dynamically allocate memory)

Demonstrate your class works with a main function that instantiates
an array of three Rectangle objects, one for each room of a house,
using the initializer list below. Then, use your setName() function to fix the name of “Offce” to “Office”. Finally, tell the user the name and area of each room, and the name of the largest room.

1
2
3
  Rectangle house[] = { Rectangle(10, 12, "Kitchen"),
                        Rectangle(20, 20, "Bedroom"),
                        Rectangle(8, 12, "Offce") };


Here is my code :

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
#include <iostream> //
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;

class Rectangle
{
private:
    double width;
    double length;
    char *name;
    void Rectangle::initName(char *n);

public:
    //constructors
    Rectangle();
    Rectangle(double, double,
              char *);
    //destructor
    ~Rectangle();
    void setWidth(double);
    void setLength(double);
    void setWidth(char *);
    void setLength(char *);
    void setName(char *);
    int getWidth() const;
    int getLength() const;
    int getArea() const;
    void printName() const
    {
        cout << name;
    }
};

Rectangle::Rectangle()
{
    width = 0;
    length = 0;
    initName("Default");
}

Rectangle::Rectangle(double x, double y, char *z)
{
    width = x;
    length = y;
    initName(z);
}

void Rectangle::initName(char *n)
{
    name = new char[258];
    strcpy(name, n);
};

void Rectangle::initName(char *n)
{
    name = new char[258];
    strcpy(name, n);
};

void Rectangle::setWidth(double w)
{
    width = w;
}

void Rectangle::setLength(double l)

{
    length = l;
}

void setName(char *newname)
{
    newname.name = "Office";
}

int Rectangle::getArea() const
{
    return width * length;
}

int main()
{
    Rectangle house[] = {Rectangle(10, 12, "Kitchen"),
                         Rectangle(20, 20, "Bedroom"),
                         Rectangle(8, 12, "Offce")};

    for (int i = 0; i < 3; i++)
    {
        cout << "Area for " << house[i].printName() << "is :" << house[i].getArea();
    }

    if (house[1].getArea() > house[2].getArea() && house[1].getArea() > house[3].getArea())
    {
        cout << house[1].printName() << "has the biggest area";
    }
    else if (house[2].getArea() > house[1].getArea() && house[2].getArea() > house[3].getArea())
    {
        cout << house[2].printName() << "has the biggest area";
    }
    else
    {
        cout << house[3].printName() << "has the biggest area";
    }

    return 0;
}


I got the following three errors, can you help me fix it ?

Line 13 : rectangleEnhance.cpp:13:21: error: extra qualification on member 'initName'
void Rectangle::initName(char *n)

Line 27 : rectangleEnhance.cpp:67:12: error: member reference base type 'char *' is not a structure or union
newname.name = "Office";

Line 83 : rectangleEnhance.cpp:83:29: error: invalid operands to binary expression ('basic_ostream<char>' and 'void')
cout << "Area for " << house[i].printName() << "is :" << house[i].getArea();

For the line 83, I thought it has to do with the include <iostream> but not. And the others I have been looking how to fix it, but it not successful. Can you help me fic those 3 errors and give some explanation thank you.
Perhaps:

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
#include <iostream>
//#include <cstring>
#include <cstdio>

constexpr size_t maxstr {258};

class Rectangle
{
private:
	double width {};
	double length {};
	char* name {};
	void initName(const char* n);

public:
	Rectangle();
	Rectangle(double, double, const char*);
	~Rectangle();

	void setWidth(double);
	void setLength(double);
	void setName(const char*);
	const char* getName() const { return name; }
	double getWidth() const;
	double getLength() const;
	double getArea() const;
};

Rectangle::Rectangle()
{
	initName("Default");
}

Rectangle::~Rectangle() { delete[] name; }

Rectangle::Rectangle(double x, double y, const char* z) : width(x), length(y)
{
	initName(z);
}

void Rectangle::initName(const char* n) {
	name = new char[maxstr];
	setName(n);
}

void Rectangle::setWidth(double w) { width = w; }
void Rectangle::setLength(double l) { length = l; }
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
void Rectangle::setName(const char* newname)
{
	//strcpy_s(name, maxstr - 1, newname);
	snprintf(name, maxstr, "%s", newname);
}

double Rectangle::getArea() const { return width * length; }

int main()
{
	Rectangle house[] {{10, 12, "Kitchen"},
			{20, 20, "Bedroom"},
			{8, 12, "Offce"}};

	house[2].setName("Office");

	for (size_t i = 0; i < 3; ++i)
		std::cout << "Area for " << house[i].getName() << " is " << house[i].getArea() << '\n';

	if (house[0].getArea() > house[1].getArea() && house[1].getArea() > house[2].getArea())
		std::cout << house[0].getName() << " has the biggest area";
	else if (house[1].getArea() > house[0].getArea() && house[0].getArea() > house[2].getArea())
		std::cout << house[1].getName() << " has the biggest area";
	else
		std::cout << house[2].getName() << " has the biggest area";
}

Last edited on
Hello siid14,

Given your code:
1
2
3
4
5
6
7
8
9
10
11
void Rectangle::initName(char *n)
{
    name = new char[258];
    strcpy(name, n);
};

void Rectangle::initName(char *n)
{
    name = new char[258];
    strcpy(name, n);
};

This is not an overloaded function just a redefinition of what you already did.

In an overloaded function the parameters have to be different like:
1
2
3
4
5
void Rectangle::initName(char *n, int size)
{
    name = new char[size];
    strcpy(name, n);
};

This would be considered an overloaded function because the parameters are different.

Also void Rectangle::initName(char *n); you do not need the part in bold. That is used when you define the function outside the class.

Also since you do not define a different dtor the compiler will provide a default dtor.

And please come up with better names for your variables other than single letters. A good name makes the code much easier to follow without having to scroll back and forth to check what a variable is and what it is being used for.

Good job on the code it is nicely spaced, personally I would add a couple of blank lines in some of the functions, but it works. Just need better variable names.

That is what I see for now. I will know more when I get a chance to compile the code.

Andy
Hello siid14,

Going over your code I came up with this:
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
#define _CRT_SECURE_NO_WARNINGS  // <--- Needed to use "strcpy()". Your compiler may be different.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>

using namespace std;

class Rectangle
{
    private:
    double width;
    double length;
    char *name;
    void initName(const char *n);  // <--- Changed. Made "char*" "const".
    void initName(const char *n, int size);  // <--- Added.

    public:
    //constructors
    Rectangle();
    Rectangle(double, double, const char *);  // <--- Changed. Made "char*" "const".

    //destructor
    ~Rectangle() { delete[] name; };  // <--- Changed.

    void setWidth(double);
    void setLength(double);
    void setWidth(char *);
    void setLength(char *);
    void setName(char *);

    int getWidth() const;
    int getLength() const;
    double getArea() const;  // <--- Changed to double.

    char* printName() const { return name; }  // <--- Changed. Could also be a "getName" function.
};

Rectangle::Rectangle()
{
    width = 0;
    length = 0;
    initName("Default");
}

Rectangle::Rectangle(double newWidth, double newLength, const char *newName)  // <--- Changed.
{
    width = newWidth;

    length = newLength;

    initName(newName);
}

void Rectangle::initName(const char *newName)  // <--- Changed.
{
    //newName = new char[258];
    //strcpy(newName, newName);
    name = strcpy(new char[258], newName);  // <--- From seeplus's example. Either way works.

};

void Rectangle::initName(const char *newName, int size)  // <--- Changed.
{
    //newName = new char[size];
    //strcpy(newName, newName);
    name = strcpy(new char[258], newName);  // <--- From seeplus's example. Either way works.
};

void Rectangle::setWidth(double newWidth)
{
    width = newWidth;
}

void Rectangle::setLength(double newLength)

{
    length = newLength;
}

void Rectangle::setName(char *newname)
{
    //newname.newName = "Office";
    strcpy(name, newname);
}

double Rectangle::getArea() const  // <--- Changed to double.
{
    return width * length;
}

int main()
{
    Rectangle house[]  // <--- The (=) is not needed.
    {
        Rectangle(10.0, 12.0, "Kitchen"),  // <--- ctor expects a double. You were sending an "int". Not a problem, but should match.
        Rectangle(20.0, 20.0, "Bedroom"),
        Rectangle(8.0, 12.0,  "Offce")
    };

    std::cout << std::fixed << std::setprecision(2) << '\n';  // <--- Added.

    for (int i = 0; i < sizeof(house) / sizeof(house[0]); i++)  // <--- Changed.
    {
        cout << "Area for" << std::setw(8) << house[i].printName() << " is: "
            << std::setw(7) << house[i].getArea() << " square feet.\n";
    }

    if (house[1].getArea() > house[2].getArea() && house[1].getArea() > house[3].getArea())
    {
        cout << '\n' << house[1].printName() << " has the biggest area.";
    }
    else if (house[2].getArea() > house[1].getArea() && house[2].getArea() > house[3].getArea())
    {
        cout << '\n' << house[2].printName() << " has the biggest area.";
    }
    else
    {
        cout << '\n' << house[3].printName() << " has the biggest area.";
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}

In a line like: cout << '\n' << house[1].printName() . "printName" is a "void" function that returns nothing and the "cout" does not know what to do with it. Hence the error.

If the comments do not explain enough let me know.

If you have studied "vector"s "house" would work better as a "vector".

In line 103 the "sizeof()" function works better than a magic number. This way you could add the the array and still come up with the correct size and avoid missing changing the magic number in the for loop.

Andy
@Andy. I've changed my code above to avoid buffer overflow issues.

L109 - 120. There is array overflow here as house only has 3 elements (0 - 2)...
@seeplus,

Good catch. I missed that until you mentioned it as the program appeared to work.

Still lines 109 - 120 need to be reworked to deal with an array of any size.

Andy
Hello siid14,

Understanding the problems better I came up with this:
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
iint main()
{
    size_t houseIdx{};
    double houseArea{};
    Rectangle house[]  // <--- The (=) is not needed.
    //{
    //    Rectangle(10.0, 12.0, "Kitchen"),  // <--- ctor expects a double. You were sending an "int". Not a problem, but should match.
    //    Rectangle(20.0, 20.0, "Bedroom"),
    //    Rectangle(8.0, 12.0,  "Office")
    //};

    //{
    //    Rectangle(10.0, 12.0, "Kitchen"),  // <--- ctor expects a double. You were sending an "int". Not a problem, but should match.
    //    Rectangle(8.0, 12.0, "Bedroom"),
    //    Rectangle(20.0, 20.0,  "Office")
    //};

    {
        Rectangle(20.0, 20.0, "Kitchen"),  // <--- ctor expects a double. You were sending an "int". Not a problem, but should match.
        Rectangle(38.0,42.0, "Attic"),
        Rectangle(8.0, 12.0, "Bedroom"),
        Rectangle(10.0, 12.0,  "Office")
    };

    std::cout << std::fixed << std::setprecision(2) << '\n';  // <--- Added.

    for (size_t idx = 0; idx < sizeof(house) / sizeof(house[0]); idx++)  // <--- Changed.
    {
        cout << "Area for" << std::setw(8) << house[idx].printName() << " is: "
            << std::setw(7) << house[idx].getArea() << " square feet.\n";

        if (house[idx].getArea() > houseArea)
        {
            houseIdx = idx;

            houseArea = house[idx].getArea();
        }
    }

    cout << '\n' << house[houseIdx].printName() << " has the biggest area.";

    return 0;  // <--- Not required, but makes a good break point for testing.
}

The 3 sections that initialize the array are just a quick way for testing. By switching the comments you can run different tests to see how it works. The last section demonstrates how you can add to the array and it still works.

This is the output I get:

Area for Kitchen is:  400.00 square feet.
Area for   Attic is: 1596.00 square feet.
Area for Bedroom is:   96.00 square feet.
Area for   Office is:  120.00 square feet.

Attic has the biggest area.


Sorry about the numbers. Originally I accounted for a 3 digit whole number not 4, but it is an easy fix. Line 30.

Andy
Topic archived. No new replies allowed.