0

I created two classes and a constructor in each. Type followed a new class and constructors friends functions of the classes before.

#include <iostream>

using namespace std;

class clsAtmosfericConditions;

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{
      friend clsDynamicViscocity::clsDynamicViscocity(double viscocidadDinamica);
      friend clsDensity::clsDensity(float densidad);
public:
       float kinematicViscocity();
};

float kinematicViscocity(){
      float kinematicViscocity;
      kinematicViscocity = dynamicViscocity/density; //Here is where IDE gives me the error
      return kinematicViscocity;
}

The IDE displays an error in the function: error: 'dynamicViscocity' undeclares (first use this function)

I checked on some websites and I see no need to pass values ​​by reference builder when you do the operation.

  • Well I'm unsure of what other issues you may have but quick observation shows that kinematicViscocity() should have its definition as: float clsAtmosfericConditions::kinematicViscocity(){ – qeadz Jul 10 '15 at 20:30

1 Answers1

0

Couple of problems here.

dynamicViscocity is a member of class clsDynamicViscocity. kinematicViscocity is not a member of any class, but I suspect is is intended to be a member of clsAtmosfericConditions. Regardless, kinematicViscocity is not a member of clsDynamicViscocity, so in order to operate on dynamicViscocity, it needs an object of type clsDynamicViscocity to provide dynamicViscocity.

Second, the visibility (public, private, protected) of dynamicViscocity is not specified, so C++ defaults to the most restrictive, private. A private member cannot be seen except by the object and those the object has defined as friends.

So kinematicViscocity has no dynamicViscocity and even if it did, it cannot see dynamicViscocity.

Suggested solution

Change the definition of kinematicViscocity to

float clsAtmosfericConditions::kinematicViscocity(const clsDynamicViscocity & vis,
                                                  const clsDensity & den)
{    
    float kinematicViscocity;
    kinematicViscocity = vis.getDynamicViscocity() / den.getDensity(); 
    return kinematicViscocity;
}

to provide a clsDynamicViscocity to kinematicViscocity and add a getter function

double getDynamicViscocity() const
{
    return dynamicViscocity;
}

to clsDynamicViscocity.

The same needs to be done to access density from clsDensity.

EDIT

Waaaait a second...

Finally figured out what you are trying to do here:

friend clsDynamicViscocity::clsDynamicViscocity(double viscocidadDinamica);
friend clsDensity::clsDensity(float densidad);

A class declares who they will allow to see and use their hidden internals. A class cannot declare who's hidden internals they can see. Think of it this way, Bob can be friends with Alice and show her his secrets, but Bob cannot force Alice to be his friend and show him her secrets. Alice has to make that decision herself.

What the above means is the two methods can can violate encapsulation and see the hidden internals of clsAtmosfericConditions. clsAtmosfericConditions cannot however see the internals of clsDynamicViscocity or clsDensity.

clsDynamicViscocity and clsDensity have to friend class clsAtmosfericConditions to allow clsAtmosfericConditions to see into them, not the other way around.

So

clsDensity()
{
    friend class clsAtmosfericConditions;
    ...
}

and

clsDynamicViscocity()
{
    friend class clsAtmosfericConditions;
    ...
}

Now change kinematicViscocity to look like this:

float clsAtmosfericConditions::kinematicViscocity(const clsDynamicViscocity & vis,
                                                  const clsDensity & den)
{    
    float kinematicViscocity;
    kinematicViscocity = vis.dynamicViscocity / den.density; 
    return kinematicViscocity;
}

and you are golden.

The original solution with the getters is a better solution as it requires no friends whatsoever and is much less tightly coupled. The guts of clsDynamicViscocity and clsDensity can be radically changed without breaking clsAtmosfericConditions so long as the getter function prototypes remain the same and clsAtmosfericConditions is granted no more access to clsDynamicViscocity and clsDensity than is required to get data.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54