954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Declaration of variables used within the operators overloading function

Hello everyone,

I am trying to make a program which takes two integer parameters (HeightinInches,HeightinFeet) i.e. (7,30) and then displays the output by converting it into feet and inches. I am using three operators i.e. +,++, a constant integer number=27.

After compilation I got following major errors in which I stuck up and need your help :


106 D:\MCS Work\Assignment-6-NEW-C++\mc070401526.cpp `HeightinFeet' is not a type
106 D:\MCS Work\Assignment-6-NEW-C++\mc070401526.cpp request for member of non-aggregate type before '=' token
107 D:\MCS Work\Assignment-6-NEW-C++\mc070401526.cpp `HeightinInches' is not a type
107 D:\MCS Work\Assignment-6-NEW-C++\mc070401526.cpp request for member of non-aggregate type before '=' token


So, my question is how and where to describe my integer variables "heightinFeet" and "heightinInches". Note, I have described these two variables in "Private:" but it is not working.
Here is the code:
It's a time limit case guys, please response quicky. I will be waiting for your quick response.

#include<iostream.h>
using std::cout;
using std::cin;
using std::ios;
//Definition of Class
class Height
{
      // Class interface
      public:
             // Methods & Constructors of Class
             Height();
             Height(int, int);
             void h1(int);
             void h2(int);
             void h3(int, int);
             void h4(int,int);
             // Member Functions
             int getFeet();
             int getInches();
             void display(int, int);
      private:
              int HeightinFeet;
              int HeightinInches;
}; // End of class
// Constructors
Height::Height()
{
                getFeet(HeightinFeet==0);
                getInches(HeightinInches==0);
} // End of default constructor

void Height::h1(int HeightinInches1)
{
     if (HeightinInches1 >= 12)
     {
                   HeightinInches1=HeightinInches1-12;
                   HeightinInches=HeightinInches1;
                   HeightinFeet=HeightinFeet + 1;
     }
     else
     {
                   HeightinInches = HeightinInches1;
     } // end if
} // End of constructor h1

void Height::h2(int heightinFeet2, int heightinInches2)
{
     if (heightinInches2 >= 12)
     {
                   heightinInches2=heightinInches2-12;
                   HeightinInches=heightinInches2;
                   HeightinFeet=HeightinFeet + 1;
     }
     else
     {
                   HeightinInches = heightinInches2;
     } // end if
} // End of constructor h2

void Height::h3(int heightinFeet3, int heightinInches3)
{
     if (heightinInches3 >= 12)
     {
                   heightinInches3=heightinInches3-12;
                   HeightinInches=heightinInches3;
                   HeightinFeet=HeightinFeet + 1;
     }
     else
     {
                   HeightinInches = heightinInches3;
     } // end if
} // End of constructor h3
     
void Height::h4(int heightinFeet4, int heightinInches4)
{
     if (heightinInches4 >= 12)
     {
                   heightinInches4=heightinInches4-12;
                   HeightinInches=heightinInches4;
                   HeightinFeet=HeightinFeet + 1;
     }
     else
     {
                   HeightinInches = heightinInches4;
     } // end if
} // End of constructor h4

// Get member Functions
int Height::getFeet()
{
    return HeightinFeet();
} // End function getFeet
int Height::getInches()
{
    return HeightinInches();
} // End function getInches
// Function to display Height in Feet & Inches
void Height::display(int heightinFeet, int heightinInches)
{
     cout<<"\n\n Height = "<<HeightinFeet<<" Feet "<<HeightinInches<<" Inches";
} // End display function

// HOW AND WHERE SHOULD I DELCARE THE HeightinInches & HeightinFeet;
// Declaration (prototype) of overloaded sum operator
int Height:: operator +(int h1)
{
    int temp;
    temp.HeightinFeet = HeightinFeet + h1.HeightinFeet;
    temp.HeightinInches = HeightinInches + h1.HeightinInches;
    return temp;
}

// HOW AND WHERE SHOULD I DELCARE THE HeightinInches & HeightinFeet;
// Declaration (prototype) of overloaded pre-increament operator ++ operator
int Height::operator++(int h4)
{
    int temp1;
    temp1.HeightinFeet = HeightinFeet++;
    temp1.HeightinInches = HeightinInches++;
    return temp1;
}

// HOW AND WHERE SHOULD I DELCARE THE HeightinInches & HeightinFeet;
// Declaration (prototype) of overloaded sum operator with constant number 27
int Height::operator+(int number, int h3)
{
    int number = 27;
    int temp2;
    temp2.HeightinFeet = HeightinFeet + h3.HeightinFeet;
    temp2.HeightinInches = number + h3.HeightinInches++;
    return temp2;
}
// Main function starts
int main()
{
    Height h1;
    Height h2(25);
    Height h3(2,35);
    Height h4(2,25);
    cout<<"********************h1********************"<<endl;
    h1.display();
    cout<<"********************h2********************"<<endl;
    h2.display();
    cout<<"********************h3********************"<<endl;
    h3.display();
    cout<<"********************h4********************"<<endl;
    h4.display();
    cout<<"******************************************"<<endl;
    if(h2==h3)
    {
              cout<<"h2 is equal to h1"<<endl;
    else if(h2>h3)
              cout<<"h2 is greater than h3"<<endl;
    else
              cout<<"h2 is less then h3"<<endl;
    cout<<"****************h1=h2+h3*******************"<<endl;
    h1=h2+h3;
    h1.display();
    cout<<"*******************h4++********************"<<endl;
    h4++;
    h4.display();
    cout<<"*****************h4=27+h3******************"<<endl;
     h4=27+h3;
     h4.27+h3;
     h4.display();
     
     return 0;
}


Please feel free to enquire about any thing.
regards.

MKQ
Newbie Poster
4 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

dude your code is too difficult to read, next time please indent it properly and put in [CODE][\CODE] tags

however i can make out that you have not overloaded the operators in your class. The operator funtions need to be declared as member functions of your class

got it?

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

on my first glance i can c a lot more errors once you do this you might have to solve those too..

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 
on my first glance i can c a lot more errors once you do this you might have to solve those too..


That's right I am getting too many more errors but, my basic problem is the one which I mentioned above.

If you or anyone else could please describe me about this problem by giving code, it would be very helpful for me.

regards,

MKQ
Newbie Poster
4 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

i think i gave the solution to your problem in my first post

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

The problem is that type int does not have members named HeightinFeet and HeightinInches.

Starting at line 108:

int temp;
  temp.HeightinInches = ...


Anint is not a Height.

Chandra.rajat hit it right on: your operators are supposed to work on your class. So, the + operator should have the following prototype:
Height Height::operator + ( Height &h )
Remember, you are supposed to be addingHeights.

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You