Copy constructor for class "celsius" may not have a paramete

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
Raduku077
Junior
Junior
Posts: 20
Joined: 09 Jan 2014, 15:41
Judet: Iaşi

Copy constructor for class "celsius" may not have a paramete

Post by Raduku077 » 08 Apr 2014, 22:54

Problema este in clasa celsius la functia prieten.

Ma atentioneaza in legatura cu doua lucruri :
- No instance of overloaded function "fahrenheit::fahrenheit" matches the specified type
- Copy constructor for class "celsius" may not have a parameter of type "celsius"

Code: Select all

#include "stdafx.h"
#include <iostream>
using namespace std;

class celsius;
class fahrenheit {
	double fan;
public:
	fahrenheit(double f = 0); // Utilizat pentru conversia din tip predefinit in tip fahrenheit.
	fahrenheit(celsius); //Utilizat pentru conversia din tipul celsius in tipul fahrenheit.
	double retfahrenheit();
	operator celsius(); // Conversie din tipul fahrenheit in tipul celsius.
	fahrenheit operator-();
	friend fahrenheit operator+(fahrenheit f1, fahrenheit f2);
	friend fahrenheit operator-(fahrenheit f1, fahrenheit f2);
	fahrenheit operator*(double d);
	friend fahrenheit operator*(double d, fahrenheit f2);
	fahrenheit operator/(double d);
};

class celsius {
	double cel;
public:
	celsius(double d = 0); //Initializare si conversie din double in celsius.
	friend fahrenheit::fahrenheit(celsius); //Constructorul clasei fahrenheit ce face conversii din fahrenheit in celsius trebuie sa aiba acces la data membra cel .
	double retcelsius();
};
Daca las doar " friend fahrenheit(celsius); " , atentionarea este : " Invalid friend declaration . " .
(Microsoft Visual Studio 2010 Express)



User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: Copy constructor for class "celsius" may not have a para

Post by Marius Bancila » 09 Apr 2014, 09:43

Nu se poat ce vrei tu pentru ca celsius e un tip nedefinit la momentul cand e folosit in clasa fahrenheit. Compilatorul nu stie cum arata acest tip si ce membrii are, stie doar ca exista un tip cu acest nume.

Clasa fahrenheit nu are nevoie sa fie prietena cu celsius pentru a accesa variabila privata cel. Clasa ta celsius poate definit un operator de conversie la double si atunci nu mai e nevoie:

Code: Select all

operator double() const {return cel;}
Pentru a rezolva problema ar trebui sa inversezi ordinea definiri acestor doua clase:

Code: Select all

class fahrenheit;

class celsius {
   double cel;
public:
   celsius(double const d = 0): cel(d) {}
   operator double() const {return cel;}
};

class fahrenheit {
   double fan;
public:
   fahrenheit(double const f = 0) : fan(f) {}
   fahrenheit(celsius const & c):fan(c*1.8+32) {}
   operator double() const {return fan;}
   operator celsius() const { return (fan-32)/1.8;}
};
Si atunci merge asa ceva:

Code: Select all

   celsius c(20);

   fahrenheit f1(60);
   fahrenheit f2(c);

   double t1 = f1;
   celsius t2 = f2;
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

Raduku077
Junior
Junior
Posts: 20
Joined: 09 Jan 2014, 15:41
Judet: Iaşi

Re: Copy constructor for class "celsius" may not have a para

Post by Raduku077 » 09 Apr 2014, 19:31

Multumesc frumos !

Post Reply