clemensf
clemensf

Reputation: 21

How to instantiate a variable from a class with a variable from another class with C++

I'm trying to instantiate a variable Tt1 from a class called ÌnletConditions with the variable Tt0 from another class with the object ein_in.

double InletConditions::Tt1 = ein_in.Tt0;

The variable Tt1 is declared as public static double in the headerfile of the class InletConditions.

class InletConditions {
public:
    static double Tt1;
}

The variable Tt0 is declared and instantiated like this:

\\ file Eingabe_Konstanten.h
class Eingabe_Konstanten {
public:
    static double Tt0;
}

\\ file Eingabe_Konstanten.cpp
double Eingabe_Konstanten::Tt0 = io_ein.read(1);

io_ein.read(int) refers to a method, which reads in a value from the specified line number (int) from a file. The value should become 293.15.

How can I achieve that the value of Tt1 also becomes 293.15? In the output it is just 0.

int main() {
    Eingabe_Konstanten ein;
    InletConditions in;
    std::cout << ein.Tt0 << endl;
    std::cout << in.Tt1 << endl;
}

Output:

293.15
0

I would be pleased if anyone could help me, since I am new to programming and don't know what topic this problem is related to.

Thanks in advance.

Upvotes: 0

Views: 89

Answers (2)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

Static variables refer to the class itself and not to a particular object of that class. As such you must call them using the scope resolution operator of the class:

InletConditions::Tt1 = Eingabe::Tt0;
std::cout << Eingabe::Tt0 << endl;
std::cout << InletConditions::Tt1 << endl;

LIVE DEMO

Upvotes: 2

EkcenierK
EkcenierK

Reputation: 1439

Why not just use:

int main() {
    Eingabe_Konstanten ein;
    InletConditions in;
    in.Tt1 = ein.Tt0; //You need to assign the value of ein.Tt0 to in.Tt1 here
    std::cout << ein.Tt0 << endl;
    std::cout << in.Tt1 << endl;
}

Upvotes: 0

Related Questions