Structure required on left side of . or *
Structure required on left side of . or *
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
|
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class box
{
float length,breadth,height;
public:
box()
{
cout<<"Default Constructor"<<endl;
length=2;
breadth=3;
height=4;
}
box(float l,float b,float h)
{
cout<<"Parametrized"<<endl;
length=l;
breadth=b;
height=h;
}
box(box &ob)
{
cout<<"Copy constructor"<<endl;
length=ob.length;
breadth=ob.breadth;
height=ob.height;
}
void volume()
{
float volume;
volume=length*breadth*height;
cout<<"The voume is "<<volume<<endl;
}
~box()
{
cout<<"Destructor invoked";
}
};
void main()
{
cout<<" Program for constructor overloading "<<endl;
float a,b,c;
int ch;
char rep;
cout<<"Enter choice "<<endl;
cout<<"1 Default 2 Parametrized 3 Copy"<<endl;
cin>>ch;
while (rep=='y')
{ box gk;
switch(ch)
{
case 1: box ob1();
ob1.volume();
break;
case 2: {cout<<"Enter length "<<endl;
cin>>a;
cout<<"Enter breadth "<<endl;
cin>>b;
cout<<"Enter height "<<endl;
cin>>c;
box ob2(a,b,c);
ob2.volume();
}
break;
case 3: {
box ob3(gk);
ob3.volume();
}
break;
default:
{
cout<<"Invalid choice check!";
}
}
cout<<"continue? ";
cin>>rep;
}
getch();
}
|
in line number 55 i get the error. Kindly guide
box ob1();
declares a function named obj1 that takes no arguments and returns a box.
To fix this problem you only need to remove the parentheses.
Please use indentation and think about the logic of the main while loop.
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
|
#include <iostream> // <===== use standard headers
//#include <conio.h>
#include <cstdlib>
using namespace std; // <===== either add this line or qualify evrything necessary with std::
class box
{
float length, breadth, height;
public:
box()
{
cout << "Default Constructor" << endl;
length = 2;
breadth = 3;
height = 4;
}
box( float l, float b, float h )
{
cout << "Parameterised Constructor" << endl;
length = l;
breadth = b;
height = h;
}
box( box &ob )
{
cout << "Copy constructor" << endl;
length = ob.length;
breadth = ob.breadth;
height = ob.height;
}
void volume()
{
float volume;
volume = length * breadth * height;
cout << "The volume is " << volume << endl;
}
~box()
{
cout << "Destructor invoked" << endl;
}
};
int main() // <===== int, not void
{
cout << " Program for constructor overloading " << endl;
float a, b, c;
int ch;
char rep;
rep = 'y'; // <===== HAVE TO ADD THIS, OR NOTHING WILL HAPPEN
while ( rep == 'y' )
{
cout << "\nEnter choice " << endl; // <==== put these 3 lines inside the while loop
cout << "1 Default 2 Parameterised 3 Copy" << endl;
cin >> ch;
box gk;
switch( ch )
{
case 1:
{ // <===== Add brace, because of limited scope of ob1
box ob1; // <===== No brackets
ob1.volume();
}
break;
case 2:
{
cout << "Enter length " << endl;
cin >> a;
cout << "Enter breadth " << endl;
cin >> b;
cout << "Enter height " << endl;
cin >> c;
box ob2( a, b, c );
ob2.volume();
}
break;
case 3:
{
box ob3( gk ); // <===== obscure; gk was a no-parameters box
ob3.volume();
}
break;
default:
{
cout << "Invalid choice check!" << endl;
}
}
cout << "Continue (y/n)? "; // <==== give a choice to reply
cin >> rep;
}
// getch(); // <==== uncomment if desperate
}
|
Last edited on
Topic archived. No new replies allowed.