[Help] cos() function with Classes

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

Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

[Help] cos() function with Classes

 
0
  #1
Dec 7th, 2007
Code:


NumeroReal.h

  1.  
  2.  
  3. #pragma once
  4.  
  5. #include <iostream>
  6. #include <cmath>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. class NumeroReal
  12. {
  13. private:
  14. double n;
  15.  
  16. public:
  17. NumeroReal(); // default constructor
  18. NumeroReal( double n ); // parameter constructor
  19. NumeroReal( const NumeroReal &unNumeroReal ); // copy constructor
  20. ~NumeroReal(); // destructor
  21.  
  22. void setNumeroReal( double n );
  23. double getNumeroReal() const;
  24.  
  25. friend ostream &operator << ( ostream &output, const NumeroReal &unNumeroReal );
  26. friend istream &operator >> ( istream &input, NumeroReal &unNumeroReal );
  27.  
  28. NumeroReal cos() const;
  29.  
  30. };




NumeroReal.cpp

  1.  
  2.  
  3. #include "NumeroReal.h"
  4.  
  5. NumeroReal::NumeroReal(void)
  6. {
  7. this->n = 1.0;
  8. }
  9.  
  10. NumeroReal::NumeroReal( double n )
  11. {
  12. this->n = n;
  13. }
  14.  
  15. NumeroReal::NumeroReal( const NumeroReal &unNumeroReal )
  16. {
  17. this->n = unNumeroReal.n;
  18. }
  19.  
  20. NumeroReal::~NumeroReal(void)
  21. {
  22. }
  23.  
  24. //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  25.  
  26.  
  27. void NumeroReal::setNumeroReal( double n )
  28. {
  29. this->n = n;
  30. }
  31.  
  32. double NumeroReal::getNumeroReal() const
  33. {
  34. return ( this->n );
  35. }
  36.  
  37. //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  38.  
  39. ostream &operator << ( ostream &output, const NumeroReal &unNumeroReal )
  40. {
  41. output << unNumeroReal.n;
  42.  
  43. return ( output );
  44. }
  45.  
  46. istream &operator >> ( istream &input, NumeroReal &unNumeroReal )
  47. {
  48. cout << "Entre un numero real: ";
  49. cin >> unNumeroReal.n;
  50.  
  51. return ( input );
  52. }
  53.  
  54.  
  55. NumeroReal NumeroReal::cos() const
  56. {
  57. return ( this->cos() );
  58. }


Main.cpp


  1.  
  2. #include "NumeroReal.h"
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. #include <conio.h>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. int main ()
  12. {
  13.  
  14. NumeroReal n1, n2( 4.0 );
  15.  
  16. cout << "Numero Real # 1" << endl;
  17. cin >> n1;
  18.  
  19. cout << "\nNumero Real # 2" << endl;
  20. cin >> n2;
  21.  
  22.  
  23. cout << cos( n1 ) << "\n\n";
  24.  
  25. system("PAUSE");
  26. return 0;
  27. }


I want to find the cos( n1 ) but when i try to print it it gives me this error:

------ Build started: Project: CalculosNumeros, Configuration: Debug Win32 ------
Compiling...
Main.cpp
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\calculosnumeros\calculosnumeros\main.cpp(78) : error C2665: 'cos' : none of the 3 overloads could convert all the argument types
c:\program files\microsoft visual studio 8\vc\include\math.h(116): could be 'double cos(double)'
c:\program files\microsoft visual studio 8\vc\include\math.h(503): or 'float cos(float)'
c:\program files\microsoft visual studio 8\vc\include\math.h(551): or 'long double cos(long double)'
while trying to match the argument list '(NumeroReal)'
Build log was saved at "file://c:\Documents and Settings\MPayne007\My Documents\Visual Studio 2005\Projects\CalculosNumeros\CalculosNumeros\Debug\BuildLog.htm"
CalculosNumeros - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,654
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: [Help] cos() function with Classes

 
0
  #2
Dec 7th, 2007
>cos( n1 )
n1 is an object of NumeroReal, but it doesn't support an implicit conversion to any of the types that cos expects. Perhaps you meant n1.cos() ? That should give you an interesting result too, but it's more likely to take you in the right direction than trying to convert an object to floating-point.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: [Help] cos() function with Classes

 
0
  #3
Dec 7th, 2007
I was trying to do an overloading to cos() to be able to use it without the dot.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,654
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: [Help] cos() function with Classes

 
0
  #4
Dec 7th, 2007
>I was trying to do an overloading to cos() to be able to use it without the dot.
You never defined a global cos function, so naturally your compiler will find the ones from cmath. Anyway, naming your global functions the same as a standard library function is flirting with danger. I'd suggest you over it and use the dot syntax.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: [Help] cos() function with Classes

 
0
  #5
Dec 7th, 2007
Ok..
Thanks 4 your help...
Last edited by Max_Payne; Dec 7th, 2007 at 1:01 pm.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC