简体   繁体   中英

Problem with variables using a namespace at C++

I am trying to write a relativity big code. The main problem, for awhile, is that there is a problem occurring when I define a dictionary.

I have a .hh file named Electric.hh . This is what is inside of it:

#ifndef Electric_h
#define Electric_h 1

namespace Ele{
    static constexpr double zfield = 80;
}

#endif

I have defined the Ele namespace, and the zfield variable, so that I can use it in another folder.

Now, the problem is: I have a file called Tracker.cc which uses this variable:

#include "Electric.hh"
...
auto hx = Ele::zfield;
std::ofstream cFile (std::to_string(hx)+".txt", std::ofstream::app);
...

Now, when I do make at the terminal so that the program runs, the first time it runs well, that is, it creates a file named 80.txt . The problem is, now suppose I change the content inside of Electric.hh , so that instead of:

static constexpr double zfield = 80;

I use:

static constexpr double zfield = 40;

When I do make again (after do cmake ) on the terminal, and try to run the program again, it seems that the file Tracker.cc has stored the value 80 , that is, it does not create any file named 40.txt , but creates another file named 80.txt . I am extremely confused why. Shouldn't it forget the value 80 after I do make ? What is wrong with my code?

You forgot to tell in the Makefile that Tracker.cc depends on Electric.hh. So on the second make call nothing gets recompiled.

With gcc/clang you can create those dependencies on included files automatically, which is well worth it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM