Jump to content


[Help] cos() function with Classes


  • Please log in to reply
4 replies to this topic

#1 Guest_Max_Payne_*

Guest_Max_Payne_*
  • Guests

Posted 07 December 2007 - 04:12 PM

Code:


NumeroReal.h

#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

#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


#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 ==========

#2 Max_Payne

Max_Payne
  • Members
  • 5 posts
  • Location:Lost in an Island with Claire

Posted 07 December 2007 - 04:19 PM

I'm the one who posted this topic... I just registered...
Posted Image

#3 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 07 December 2007 - 07:44 PM

There isn't an overload of the cos function that takes your data type. You need to call cos with the double that is within your class.
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#4 Max_Payne

Max_Payne
  • Members
  • 5 posts
  • Location:Lost in an Island with Claire

Posted 07 December 2007 - 08:56 PM

It works for me this way
cout << cos( n1.getNumeroReal() ) << "\n\n";

But my instructor wants me to make an overloaded function in order to be able to use like this
cout << cos( n1 ) << "\n\n";

Got any ideas ???
Posted Image

#5 CodeCat

CodeCat

    Half fox, half cat, and all insanity!

  • Members
  • 3,768 posts
  •  Fighting for equality of all species

Posted 07 December 2007 - 10:05 PM

NumeroReal NumeroReal::cos() const
{
	return ( this->cos() );
}


You realise that's an infinite loop? Calling cos on a NumeroReal object will call that function, which then calls cos on the same object again, which calls cos again, and again, and again...

What you want is

return std::cos(n);

CodeCat

Posted Image
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users