Constructor issues

Hey, Im a beginner and i have some issues with this program. This program takes 2 boxes and calculate the volume of the box and prints it to the screen.
I have included a constructor and some get/set functions. I have some issues with ERROR. The errors is: C2228 and expression must have class type

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
#include <iostream>

using namespace std;


class Box {

public:
	Box(double H, double L, double B); // Constructor

	
	void setheight(double H);
	double getheight();
	void setlength(double L);
	double getlength();
	void setwidth(double B);
	double getwidth();
	//Funktion 
	double volumen();
private:
	double length;   
	double with;  
	double height;   
};

int main() {
	
	Box Box1(5.0, 6.0, 7.0);        
	Box Box2(); // this should be my constructor


	// box 2 specification
	Box2.setheight = 10.0;
	Box2.setlength = 12.0;
	Box2.setwidth = 13.0;

	// volume of box 1

	cout << "Volume of Box1 : " << Box1.volumen() << endl;

	// volume of box 2
	cout << "Volume of Box2 : " << Box2.volumen() << endl;
	system("Pause");
	return 0;
}
//Set function

void Box::setheight(double H) {
	height = H;
}
void Box::setlength(double L) {
	length = L;
}
void Box::setwidth(double B) {
	width = B;
}
// Get functions
double Box::getheight() {
	return height;
}
double Box::getlength() {
	return length;
}
double Box::getwidth() {
	return width;
}
//Constructor
Box:: Box(double H, double L, double B) : height(H), width(B), length(L) {}
// Volume function
double Box::volumen() {
	return length*width*height;
}
Box Box2(); // this should be my constructor

That is a function declaration. Try this:

Box Box2;

But you don't have that constructor. You can default it:

Box() = default;

There are lots of other errors and warnings:

In function 'int main()':
33:7: error: request for member 'setheight' in 'Box2', which is of non-class type 'Box()'
34:7: error: request for member 'setlength' in 'Box2', which is of non-class type 'Box()'
35:7: error: request for member 'setwidth' in 'Box2', which is of non-class type 'Box()'
42:38: error: request for member 'volumen' in 'Box2', which is of non-class type 'Box()'
In member function 'void Box::setwidth(double)':
55:2: error: 'width' was not declared in this scope In member function 'double Box::getwidth()':
65:9: error: 'width' was not declared in this scope
In constructor 'Box::Box(double, double, double)':
68:54: error: class 'Box' does not have any field named 'width'
23:9: warning: 'Box::height' will be initialized after [-Wreorder]
21:9: warning: 'double Box::length' [-Wreorder]
68:1: warning: when initialized here [-Wreorder] In member function 'double Box::volumen()':
71:16: error: 'width' was not declared in this scope In member function 'double Box::getwidth()':
66:1: warning: control reaches end of non-void function [-Wreturn-type] In member function 'double Box::volumen()': 72:1: warning: control reaches end of non-void function [-Wreturn-type]


Some of them are typos, your IDE should help there.

In your constructor, initialise the variables in the same order as they are in the class definition.

You are not calling the set functions correctly.
Last edited on
Can u please show me how to correct them?
I do understand the errors, but I dont know how to correct them correctly
I just correct all the typos. Now I only have the 3 errors which are C2659.

Error accurs at line 33, 34, 35.
Can you tell me how to call a function? What is a function argument?
Topic archived. No new replies allowed.