Re: Globals
This problem is really annoying, so here it is
testTrace.cpp:
--8<---------------cut here---------------start------------->8---
// -*- compile-command: "g++ testTrace.cpp Trace.cpp"-*-
#include <iostream>
#include "Globals.h"
#include "Trace.h"
using namespace std;
int main()
{
GLOBALS::trace_length = 10;
cout << GLOBALS::trace_length << endl;
Trace t;
return 0;
}
--8<---------------cut here---------------end--------------->8---
Trace.h
--8<---------------cut here---------------start------------->8---
#ifndef TRACE_H
#define TRACE_H
#include <map>
#include <vector>
#include "RingBuffer.h"
#include "PadNodeID.h"
#include "Globals.h"
class Trace
{
private:
RingBuffer<PadNodeID> empty;
std::map<PadNodeID, PadNodeID> trace;
public:
Trace();
};
#endif /* TRACE_H */
--8<---------------cut here---------------end--------------->8---
Trace.cpp
--8<---------------cut here---------------start------------->8---
#include <iostream>
#include "Globals.h"
#include "Trace.h"
#include "PadNodeID.h"
Trace::Trace () : empty(GLOBALS::trace_length)
{
std::cout << GLOBALS::trace_length;
}
--8<---------------cut here---------------end--------------->8---
Globals.h
--8<---------------cut here---------------start------------->8---
#ifndef GLOBALS_H
#define GLOBALS_H
#include <iostream>
#include "Environment.h"
#include "Globals.h"
namespace GLOBALS
{
static int num_landmarks;
static int trace_length;
static int history_size;
static int distribution_size;
static Environment *environment;
// see why this error
static ostream *out;
}
#endif /* GLOBALS_H */
--8<---------------cut here---------------end--------------->8---
The guards are there, so it should be actually included only once, BUT
the second time I print the variable in the constructor is ALWAYS 0!!
Anything else or a completely different approach I could follow?
And the second problem is that I can't declare the map like that,
because the RingBuffer constructor is
RingBuffer(size_t max_size) : max_size(max_size) {};
The only way I see now is to add a default null constructor and set the
value later, but I don't really like it that much...
I solved that for other types using the initialization in the
Constructor but I don't understand how to do it here...