0

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.

clemensf
  • 21
  • 1
  • 5
  • Assuming `InletConditions` class has a separate file, wouldn't including `Eingabe_Konstanten.h`before `InletConditions.h` solve it? – Leśny Rumcajs Nov 13 '15 at 08:25
  • No, unfortunately not. Thank you anyway! – clemensf Nov 13 '15 at 08:40
  • Try move the initialization steps to same cpp file, this will allow you to set the initialization order explicitly. "Eingabe_Konstanten::Tt0 =" and then "double InletConditions::Tt1 =" – jenkas Aug 01 '18 at 21:14

2 Answers2

2

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

101010
  • 41,839
  • 11
  • 94
  • 168
  • Thanks for your response, but that doesn't help, too. I still get `Tt1 = 0` at the output. – clemensf Nov 13 '15 at 08:52
  • @ClemensFörster if you assign the variables in `main` you get the desired effect, look at the demo. – 101010 Nov 13 '15 at 09:17
  • I don't know what I am doing wrong. When I assign it in InletConditions.cpp, it doesn't work. – clemensf Nov 13 '15 at 10:19
  • I guess I've been hiding something important, see the definition of Tt0 in my question, which I've recently edited. Does it change anything in your answer? Thanks in advance and sorry for the mistake. – clemensf Nov 16 '15 at 10:24
0

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;
}
EkcenierK
  • 1,429
  • 1
  • 19
  • 34
  • Thanks for your response! That was just a minimal example of the program. And since I don't want to get the main()-function to big and other classes need the value of Tt1 aswell, this might not be the best solution of the problem. Otherwise this would help. – clemensf Nov 13 '15 at 08:27
  • You could just create a new function that does the assignment, and use the scope resolution operator as in the answer by @101010 below. See here for an explanation of static variables in C++ - you do not need to create a class instance to use them. http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ – EkcenierK Nov 13 '15 at 08:32
  • This one might also help... http://stackoverflow.com/questions/12161697/accessing-a-static-member-function-from-another-class?rq=1 – EkcenierK Nov 13 '15 at 08:33
  • Thanks for the advice, I will take at look at it. – clemensf Nov 13 '15 at 08:34