classes in C++

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

Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

classes in C++

 
0
  #1
Feb 14th, 2005
alright, so i have two files now, main.cpp, and Rationall.h, when i compiled my code it gave me this....
/tmp/ccyeOf6F.o(.text+0x1a6): In function `main':
: undefined reference to `rational::~rational [in-charge]()'
/tmp/ccyeOf6F.o(.text+0x1c4): In function `main':
: undefined reference to `rational::~rational [in-charge]()'
collect2: ld returned 1 exit status

this is my code......i dont see how its undefined.

this is Rationall.h

  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. class rational {
  8.  
  9. public:
  10. rational();
  11. ~rational();
  12. int setrat( int x, int y);
  13. int getnum();
  14. int getdenom();
  15.  
  16.  
  17. private:
  18.  
  19. int numerator;
  20. int denominator;
  21.  
  22.  
  23. }; // end of rational class
  24.  
  25. rational::rational()
  26. {
  27. numerator = 1;
  28. denominator = 1;
  29. }
  30. int rational::setrat( int x, int y)
  31. {
  32. numerator = x;
  33. denominator = y;
  34. }
  35. int rational::getnum()
  36. {
  37. return (numerator);
  38. }
  39. int rational::getdenom()
  40. {
  41. return (denominator);
  42. }


and heres my main

  1. #include "Rationall.h"
  2.  
  3. int main()
  4. {
  5. using namespace std;
  6. int x, y;
  7. rational rationl;
  8.  
  9. cout << "Enter in a fraction" << endl;
  10. cout << "Enter in the numerator" << endl;
  11. cin >> x;
  12. cout << "Enter in the denominator" << endl;
  13. cin >> y;
  14.  
  15. rationl.setrat( x, y );
  16.  
  17. cout << "The fraction: " << endl;
  18. cout << rationl.getnum() << rationl.getdenom() << endl;
  19.  
  20. return 0;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: classes in C++

 
0
  #2
Feb 14th, 2005
You don't define your function in headers because headers don't get compiled.
Make a seperate file for your rational or define them in your main file(I wouldn't do this).
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: classes in C++

 
0
  #3
Feb 14th, 2005
Originally Posted by prog-bman
You don't define your function in headers because headers don't get compiled.
Make a seperate file for your rational or define them in your main file(I wouldn't do this).
What are you talking about? U can define functions in headers if u want to. Now if it's a good practice or not is a different issue. Headers can now be precompiled as far as i know.

To seeplusplus:
try to comment out ur destructor in the definition of ur rational class like this:
//~rational();
That helped me compile ur code without any errors. There were a few warnings though, like ur setrat() function should return an int.
As far as i can recall u sought help for this in another thread, why did u start a new one?
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

Re: classes in C++

 
0
  #4
Feb 14th, 2005
oh i started a new thread because people started fighting in the last one and they ended up closing it. thanks for the help guys.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: classes in C++

 
0
  #5
Feb 14th, 2005
Originally Posted by seeplusplus
oh i started a new thread because people started fighting in the last one and they ended up closing it. thanks for the help guys.
Yeah right! I think i rem something like that :lol:
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 3
Reputation: Tripoli178 is an unknown quantity at this point 
Solved Threads: 0
Tripoli178 Tripoli178 is offline Offline
Newbie Poster

Re: classes in C++

 
0
  #6
Feb 14th, 2005
Hey
Did u remeber to add ratinall to your project(Add to project)?
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC