| | |
[Help] cos() function with Classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Code:
NumeroReal.h
NumeroReal.cpp
Main.cpp
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 ==========
NumeroReal.h
C++ Syntax (Toggle Plain Text)
#pragma once #include <iostream> #include <cmath> #include <cstdlib> using namespace std; class NumeroReal { private: double n; public: NumeroReal(); // default constructor NumeroReal( double n ); // parameter constructor NumeroReal( const NumeroReal &unNumeroReal ); // copy constructor ~NumeroReal(); // destructor void setNumeroReal( double n ); double getNumeroReal() const; friend ostream &operator << ( ostream &output, const NumeroReal &unNumeroReal ); friend istream &operator >> ( istream &input, NumeroReal &unNumeroReal ); NumeroReal cos() const; };
NumeroReal.cpp
C++ Syntax (Toggle Plain Text)
#include "NumeroReal.h" NumeroReal::NumeroReal(void) { this->n = 1.0; } NumeroReal::NumeroReal( double n ) { this->n = n; } NumeroReal::NumeroReal( const NumeroReal &unNumeroReal ) { this->n = unNumeroReal.n; } NumeroReal::~NumeroReal(void) { } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx void NumeroReal::setNumeroReal( double n ) { this->n = n; } double NumeroReal::getNumeroReal() const { return ( this->n ); } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ostream &operator << ( ostream &output, const NumeroReal &unNumeroReal ) { output << unNumeroReal.n; return ( output ); } istream &operator >> ( istream &input, NumeroReal &unNumeroReal ) { cout << "Entre un numero real: "; cin >> unNumeroReal.n; return ( input ); } NumeroReal NumeroReal::cos() const { return ( this->cos() ); }
Main.cpp
C++ Syntax (Toggle Plain Text)
#include "NumeroReal.h" #include <iostream> #include <cmath> #include <conio.h> #include <cstdlib> using namespace std; int main () { NumeroReal n1, n2( 4.0 ); cout << "Numero Real # 1" << endl; cin >> n1; cout << "\nNumero Real # 2" << endl; cin >> n2; cout << cos( n1 ) << "\n\n"; system("PAUSE"); return 0; }
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 ==========
>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 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.
>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.
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.
![]() |
Similar Threads
- Problem with arrays or objects and classes (C++)
- Invalid conversions while using other people's software (C++)
- pointers to windows forms (C)
- virtual string ToFileFormat() (C++)
- Dr Scheme Tutorial (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: search word in file
- Next Thread: Parsing question
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






