Declaration of variables used within the operators overloading function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 4
Reputation: MKQ is an unknown quantity at this point 
Solved Threads: 0
MKQ MKQ is offline Offline
Newbie Poster

Declaration of variables used within the operators overloading function

 
0
  #1
Jan 29th, 2008
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.

  1.  
  2. #include<iostream.h>
  3. using std::cout;
  4. using std::cin;
  5. using std::ios;
  6. //Definition of Class
  7. class Height
  8. {
  9. // Class interface
  10. public:
  11. // Methods & Constructors of Class
  12. Height();
  13. Height(int, int);
  14. void h1(int);
  15. void h2(int);
  16. void h3(int, int);
  17. void h4(int,int);
  18. // Member Functions
  19. int getFeet();
  20. int getInches();
  21. void display(int, int);
  22. private:
  23. int HeightinFeet;
  24. int HeightinInches;
  25. }; // End of class
  26. // Constructors
  27. Height::Height()
  28. {
  29. getFeet(HeightinFeet==0);
  30. getInches(HeightinInches==0);
  31. } // End of default constructor
  32.  
  33. void Height::h1(int HeightinInches1)
  34. {
  35. if (HeightinInches1 >= 12)
  36. {
  37. HeightinInches1=HeightinInches1-12;
  38. HeightinInches=HeightinInches1;
  39. HeightinFeet=HeightinFeet + 1;
  40. }
  41. else
  42. {
  43. HeightinInches = HeightinInches1;
  44. } // end if
  45. } // End of constructor h1
  46.  
  47. void Height::h2(int heightinFeet2, int heightinInches2)
  48. {
  49. if (heightinInches2 >= 12)
  50. {
  51. heightinInches2=heightinInches2-12;
  52. HeightinInches=heightinInches2;
  53. HeightinFeet=HeightinFeet + 1;
  54. }
  55. else
  56. {
  57. HeightinInches = heightinInches2;
  58. } // end if
  59. } // End of constructor h2
  60.  
  61. void Height::h3(int heightinFeet3, int heightinInches3)
  62. {
  63. if (heightinInches3 >= 12)
  64. {
  65. heightinInches3=heightinInches3-12;
  66. HeightinInches=heightinInches3;
  67. HeightinFeet=HeightinFeet + 1;
  68. }
  69. else
  70. {
  71. HeightinInches = heightinInches3;
  72. } // end if
  73. } // End of constructor h3
  74.  
  75. void Height::h4(int heightinFeet4, int heightinInches4)
  76. {
  77. if (heightinInches4 >= 12)
  78. {
  79. heightinInches4=heightinInches4-12;
  80. HeightinInches=heightinInches4;
  81. HeightinFeet=HeightinFeet + 1;
  82. }
  83. else
  84. {
  85. HeightinInches = heightinInches4;
  86. } // end if
  87. } // End of constructor h4
  88.  
  89. // Get member Functions
  90. int Height::getFeet()
  91. {
  92. return HeightinFeet();
  93. } // End function getFeet
  94. int Height::getInches()
  95. {
  96. return HeightinInches();
  97. } // End function getInches
  98. // Function to display Height in Feet & Inches
  99. void Height::display(int heightinFeet, int heightinInches)
  100. {
  101. cout<<"\n\n Height = "<<HeightinFeet<<" Feet "<<HeightinInches<<" Inches";
  102. } // End display function
  103.  
  104. // HOW AND WHERE SHOULD I DELCARE THE HeightinInches & HeightinFeet;
  105. // Declaration (prototype) of overloaded sum operator
  106. int Height:: operator +(int h1)
  107. {
  108. int temp;
  109. temp.HeightinFeet = HeightinFeet + h1.HeightinFeet;
  110. temp.HeightinInches = HeightinInches + h1.HeightinInches;
  111. return temp;
  112. }
  113.  
  114. // HOW AND WHERE SHOULD I DELCARE THE HeightinInches & HeightinFeet;
  115. // Declaration (prototype) of overloaded pre-increament operator ++ operator
  116. int Height::operator++(int h4)
  117. {
  118. int temp1;
  119. temp1.HeightinFeet = HeightinFeet++;
  120. temp1.HeightinInches = HeightinInches++;
  121. return temp1;
  122. }
  123.  
  124. // HOW AND WHERE SHOULD I DELCARE THE HeightinInches & HeightinFeet;
  125. // Declaration (prototype) of overloaded sum operator with constant number 27
  126. int Height::operator+(int number, int h3)
  127. {
  128. int number = 27;
  129. int temp2;
  130. temp2.HeightinFeet = HeightinFeet + h3.HeightinFeet;
  131. temp2.HeightinInches = number + h3.HeightinInches++;
  132. return temp2;
  133. }
  134. // Main function starts
  135. int main()
  136. {
  137. Height h1;
  138. Height h2(25);
  139. Height h3(2,35);
  140. Height h4(2,25);
  141. cout<<"********************h1********************"<<endl;
  142. h1.display();
  143. cout<<"********************h2********************"<<endl;
  144. h2.display();
  145. cout<<"********************h3********************"<<endl;
  146. h3.display();
  147. cout<<"********************h4********************"<<endl;
  148. h4.display();
  149. cout<<"******************************************"<<endl;
  150. if(h2==h3)
  151. {
  152. cout<<"h2 is equal to h1"<<endl;
  153. else if(h2>h3)
  154. cout<<"h2 is greater than h3"<<endl;
  155. else
  156. cout<<"h2 is less then h3"<<endl;
  157. cout<<"****************h1=h2+h3*******************"<<endl;
  158. h1=h2+h3;
  159. h1.display();
  160. cout<<"*******************h4++********************"<<endl;
  161. h4++;
  162. h4.display();
  163. cout<<"*****************h4=27+h3******************"<<endl;
  164. h4=27+h3;
  165. h4.27+h3;
  166. h4.display();
  167.  
  168. return 0;
  169. }

Please feel free to enquire about any thing.
regards.
Last edited by Ancient Dragon; Jan 29th, 2008 at 6:56 am. Reason: replace icode tags with code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 459
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Declaration of variables used within the operators overloading function

 
0
  #2
Jan 29th, 2008
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?
Last edited by Agni; Jan 29th, 2008 at 6:21 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 459
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Declaration of variables used within the operators overloading function

 
0
  #3
Jan 29th, 2008
on my first glance i can c a lot more errors once you do this you might have to solve those too..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: MKQ is an unknown quantity at this point 
Solved Threads: 0
MKQ MKQ is offline Offline
Newbie Poster

Re: Declaration of variables used within the operators overloading function

 
0
  #4
Jan 29th, 2008
Originally Posted by chandra.rajat View Post
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,
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 459
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Declaration of variables used within the operators overloading function

 
0
  #5
Jan 29th, 2008
i think i gave the solution to your problem in my first post
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Declaration of variables used within the operators overloading function

 
0
  #6
Jan 29th, 2008
The problem is that type int does not have members named HeightinFeet and HeightinInches.

Starting at line 108:
  1. int temp;
  2. temp.HeightinInches = ...

An int 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 adding Heights.

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC