I am trying to write code which will display the volume of three boxes. I have my code but am having problems getting it to compile. Any help would be greatly appreciated. Here is my code:

//Assignment Programming Using Structures and Classes
// Creating and using boxes
#include <iostream>
using std::cout;
using std::endl;
int volume(int& m_Length, int& m_Width, int& m_Height); //Function prototype
 
class CBox // Class definition at global scope
{
          public:
int m_Length; // Length of a box in inches
int m_Width; // Width of a box in inches
int m_Height; // Height of a box in inches
}

int main(void)
{
	int volume1;
	int volume2;
	int volume3;

         CBox box1; // Declare box1 of type CBox
         CBox box2; // Declare box2 of type CBox
         CBox box3; // Declare box3 of type CBox

         int box1 m_Height = 18.0; // Define the values
         int box1 m_Length = 78.0; // of the members of
         int box1 m_Width = 24.0; // the object box1
         int volume1 = volume;

         int box2 m_Height = 12.0;
         int box2 m_Length = 37.0;
         int box2 m_Width = 42.0;
         int volume2 = volume;

         int box3 m_Height = 15.9;
         int box3 m_Length = 28.0;
         int box3 m_Width = 58.2;
         int volume3 = volume;
		 int result = volume(m_Length, m_Width, m_Height);
	
         cout << endl
<<"volume(int m_Length, int m_Width,int  m_Height) = "<< volume1(int m_Length), int m_Width, int m_Height)
<<endl
<< "The volume of box 1 is "<< volume1;

          cout<<endl
<<"volume(int m_Length, int m_Width,int  m_Height) = "<< volume2(int m_Length, int m_Width, int  m_Height)
<<endl
<< "The volume of box 2 is "<< volume2;
         
           cout<<endl
<<"volume(int m_Length, int m_Width,int  m_Height) = "<< volume3(int m_Length, int m_Width, int m_Height)
<<endl
<< "The volume of box 3 is "<< volume3;
cout<<endl;       
;
return 0;
}

// Function to calculate volume
int volume(int& m_Length, int& m_Width,int&  m_Height)
{
int volume = m_Length*m_Width*m_Height;
return volume;
}

I did not go over the code really good, because I do not have time but the errors I see right away is that you don't specify variable type when you are passing it to a function. And can you please post the error messages you get?

Whoa, there is much more wrong stuff. When initializing a variable in a class you do not do this:
int box1 m_Height = 18.0;
m_height already exists and you cannot reference it like that. You need a dot so do it like this box1.m_Height = 18.0; and int does not have decimal points, so it will ignore everything after the decimal point but it will still compile. And what does volume1 = volume; mean? volume is a function, not a variable!

Edited 5 Years Ago by sergent: n/a

Now when using cout << you don't do this

cout << endl
<<"volume(int m_Length, int m_Width,int  m_Height) = "<< volume1(int m_Length), int m_Width, int m_Height)

no commas (,) you have to use different couts or at least another << but then you will not use dot.

Thanks for the input, it is appreciated. I made a few changes to the code, as you have suggested. It has reduced my errors, however I am still receiving a few error codes. This is the code that I now have:

//Assignment Programming Using Structures and Classes
// Creating and using boxes
#include <iostream>
using std::cout;
using std::endl;
int volume( int& m_Length, int& m_Width,int&  m_Height); //Function prototype
 
class CBox // Class definition at global scope
{
          public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
}

int main(void)
{
	CBox box1;
	CBox box2;
	CBox box3;

	double volume1;
	double volume2;
	double volume3;
	double result;
	
	     box1.m_Height = 18.0; // Define the values
         box1.m_Length = 78.0; // of the members of
         box1.m_Width = 24.0; // the object box1
         
         box2.m_Height = 12.0;
         box2.m_Length = 37.0;
         box2.m_Width = 42.0;
         
         box3.m_Height = 15.9;
         box3.m_Length = 28.0;
         box3.m_Width = 58.2;
	
         cout << endl
<< "The volume of box 1 is "<< volume1;

          cout << endl
<<"volume(int m_Length, int m_Width,int  m_Height) = "<< volume2;

		  cout << endl
<< "The volume of box 2 is "<< volume2;
         
           cout<<endl
<<"volume(int m_Length, int m_Width,int  m_Height) = "<< volume3;

		   cout << endl
<< "The volume of box 3 is "<< volume3;
cout<<endl;       
;
return 0;
}

// Function to calculate volume
int volume(int& m_Length,int& m_Width,int& m_Height)
{
int volume = m_Length*m_Width*m_Height;
return volume;
}

Additionally, here are the errors that I am receiving:

1>c:\documents and settings\schon\my documents\visual studio 2010\projects\assignment structures and classes\assignment structures and classes\assignment structures and classes.cpp(17): error C2628: 'CBox' followed by 'int' is illegal (did you forget a ';'?)
1>c:\documents and settings\schon\my documents\visual studio 2010\projects\assignment structures and classes\assignment structures and classes\assignment structures and classes.cpp(18): error C3874: return type of 'main' should be 'int' instead of 'CBox'
1>c:\documents and settings\schon\my documents\visual studio 2010\projects\assignment structures and classes\assignment structures and classes\assignment structures and classes.cpp(56): error C2440: 'return' : cannot convert from 'int' to 'CBox'
1> No constructor could take the source type, or constructor overload resolution was ambiguous


Thanks again for your patience, as I am a novice, and thanks for your input.

Thank you, I made that change and my code compiles. However, it gives me a volume that doesn't equal what it should and it prints the same volume for all three boxes. What am I missing, here?

//Assignment Programming Using Structures and Classes
// Creating and using boxes
#include <iostream>
using std::cout;
using std::endl;
int volume( int& m_Length, int& m_Width,int&  m_Height); //Function prototype
 
class CBox // Class definition at global scope
{
          public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};

int main(void)
{
	CBox box1;
	CBox box2;
	CBox box3;
	
	
	     box1.m_Height = 18.0; // Define the values
         box1.m_Length = 78.0; // of the members of
         box1.m_Width = 24.0; // the object box1
       
         box2.m_Height = 12.0;
         box2.m_Length = 37.0;
         box2.m_Width = 42.0;
		         
         box3.m_Height = 15.9;
         box3.m_Length = 28.0;
         box3.m_Width = 58.2;
		 

         cout << endl
<< "The volume of box 1 is "<< volume;

          cout << endl
<< "The volume of box 2 is "<< volume;
         
           cout<<endl
<< "The volume of box 3 is "<< volume;
cout<<endl;       
;
return 0;
}

// Function to calculate volume
int volume(int& m_Length,int& m_Width,int& m_Height)
{
int volume = m_Length * m_Width * m_Height;
return volume;
}

Edited 5 Years Ago by Dante2002: n/a

Lines 37,40 and 43 are just using the variable volume not the function. To use the function volume you need to do

cout << endl << "The volume of box 1 is "<< volume(var1, var2, var3);

You might want to rethink your volume function though. It would look cleaner if you were to just pass the CBox object to the function and have it call the members from there. IMHO I would make volume() a class member function and then you could just do

CBox box;
// set sizes;
cout << "The volume is: "; << box.volume();

Edited 5 Years Ago by NathanOliver: n/a

Thanks for your advice. It now compiles, but it now prints the same value for all three boxes.

Edited 5 Years Ago by Dante2002: n/a

I didn't quite understand how to make volume() a class member function. So, I didn't include it. I just need the seperate values to print accordingly.

To make volume a member function you simply need to do this

class CBox
{
   // stuff you already have
   double Volume();
};

double CBox::Volume()
{
    return m_Length * m_Height * m_Width;
}

//now in your code

cout << "The volume is: " << box.Volume();

Edited 5 Years Ago by NathanOliver: n/a

I am greatful for your attempt, but I guess that I'm missing something. I took your advice and received a considerable amount of errors. I know that it's something that I'm not getting on my end. I changed it back to what I had which prints the same volume for all three boxes.

I have this as my code:

//Assignment Programming Using Structures and Classes
// Creating and using boxes
#include <iostream>
using std::cout;
using std::endl;
int volume( int& m_Length, int& m_Width,int&  m_Height); //Function prototype
 
class CBox // Class definition at global scope
{
          public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};

int main(void)
{
	CBox box1;
	CBox box2;
	CBox box3;
	
	int var1;
	int var2;
	int var3;
	
	     box1.m_Height = 18.0; // Define the values
         box1.m_Length = 78.0; // of the members of
         box1.m_Width = 24.0; // the object box1
       
         box2.m_Height = 12.0;
         box2.m_Length = 37.0;
         box2.m_Width = 42.0;
		         
         box3.m_Height = 15.9;
         box3.m_Length = 28.0;
         box3.m_Width = 58.2;
		 

         cout << endl
<< "The volume of box 1 is "<< volume(var1, var2, var3);

          cout << endl
<< "The volume of box 2 is "<< volume(var1, var2, var3);
         
           cout<<endl
<< "The volume of box 3 is "<< volume(var1, var2, var3);
cout<<endl;       
;
return 0;
}

// Function to calculate volume
int volume(int& m_Length,int& m_Width,int& m_Height)
{
int volume = m_Length * m_Width * m_Height;
return volume;
}

var1, var2, var3 were just example names. for each cout line you need to use the proper variables from the correct class. So line 40 would look like

<< "The volume of box 1 is "<< volume(box1.m_Length, box1.m_Width, box1.m_Height);

And line 43 should be

<< "The volume of box 2 is "<< volume(box2.m_Length, box2.m_Width, box2.m_Height);

I will leave line 46 for you to do. This is why I suggested that you make volume a member function of the CBox class. It is much easier to code if you just have to use box1.volume() .

I thank you truly!! The problem that I'm having is at times I think too much about a small situation and quite honestly, I am working on getting the vocabulary understood, which in my opinion is MOST KEY and my weakest attribute at this point. But again, I thank you.

This article has been dead for over six months. Start a new discussion instead.