I am trying to get a result with the attributes of two different classes.
Here is the code:
#include <iostream>
using namespace std;
/*Se hizo el uso de archvios tipo "header" para hacer el codigo mas entendible, tambien en este archivo .h
se omitio el uso de su respectivo .cpp ya que al compilar se generaba el error de "Redefinir cada una de
las clases" esto se presentaba ya que el .h que se incluia en el .cpp era el mismo codigo y hacia una
compilacion doble, la solucion fue copiar ese codigo y pegarlo en el .h eliminado asi el .cpp y el problema.*/
/*Se generan 5 clases ya que de esta forma la implementacion de las sobrecargas se hace de una forma mas sencilla*/
class clsDensity{
float density;
public:
clsDensity(){}
clsDensity(float densidad){
density = densidad;
}
friend istream& operator >>(istream &i, clsDensity &e);
friend ostream& operator <<(ostream &o, const clsDensity &s);
};
istream& operator >>(istream &i, clsDensity &e){
char sign;
i >> e.density >> sign >> sign >> sign >> sign >> sign >> sign;
return i;
}
ostream& operator <<(ostream &o, const clsDensity &s){
o << s.density << " Kg/m^3";
return o;
}
class clsDynamicViscocity{
double dynamicViscocity;
public:
clsDynamicViscocity(){}
clsDynamicViscocity(double viscocidadDinamica){
dynamicViscocity = viscocidadDinamica;
}
friend istream& operator >>(istream &i, clsDynamicViscocity &e);
friend ostream& operator <<(ostream &o, const clsDynamicViscocity &s);
};
istream& operator >>(istream &i, clsDynamicViscocity &e){
char sign;
i >> e.dynamicViscocity >> sign >> sign >> sign >> sign >> sign;
return i;
}
ostream& operator <<(ostream &o, const clsDynamicViscocity &s){
o << s.dynamicViscocity << " N/m^2";
return o;
}
class clsAtmosfericConditions{
clsAtmosfericConditions(){}
float kinematicViscocity(class clsDensity, class clsDynamicViscocity){
float kinematicViscocity;
kinematicViscocity = clsDensity::clsDensity(float)/clsDynamicViscocity::clsDynamicViscocity(double);
return kinematicViscocity;
}
};
I want to get the kinematic viscosity ratio of the density and dynamic viscosity, but I have a issue with this line:
kinematicViscocity = clsDensity::clsDensity(float)/clsDynamicViscocity::clsDynamicViscocity(double);
Xcode displays this message: "Expected '(' for función-style cast or tape construction" in when I define float and double.
I think the problem is when I pass by reference to clsDensity and clsDynamicViscocity, and so with the methods.
Ran it through gcc. For the line you state, it complains:
stuff.cpp: In member function ‘float clsAtmosfericConditions::kinematicViscocity(clsDensity, clsDynamicViscocity)’:
stuff.cpp:62: error: expected primary-expression before ‘(’ token
stuff.cpp:62: error: expected primary-expression before ‘float’
stuff.cpp:62: error: expected primary-expression before ‘(’ token
stuff.cpp:62: error: expected primary-expression before ‘double’
which makes a lot of sense. These functions (which happen to be constructors) take a float
and double
, respectively. So you need to pass them float
and double
objects. When you write float
, it's a type, not an object (ditto for double
), and you can't call a function with it.
Changing the offending line to
kinematicViscocity = clsDensity::clsDensity(0)/clsDynamicViscocity::clsDynamicViscocity(0);
(which is not what you want necessarily, but I can't guess further), changes the complaint to
stuff.cpp: In member function ‘float clsAtmosfericConditions::kinematicViscocity(clsDensity, clsDynamicViscocity)’:
stuff.cpp:62: error: no match for ‘operator/’ in ‘clsDensity(0.0f) / clsDynamicViscocity(0.0)’
which again makes sense - this constructs two objects, the combination of which is not supported by operator/
. If you would like to overload this operator for the types, see this tutorial or this one.