
Enrico Oliosi wrote:
I used the function set_output_function() how you suggest me. When I use it in a single program like in your tests example it works correctly, but when I use it in a class I must declare it static; I used it in a my_print function in order to print the ph for each comand.
I think that this happens for this reason: since the namespace Variable have static member when I used the functions of this namespace in a class (an object which can be initialized more times) I must declare them static.
Dear Enrico,
what you say above makes little sense to me, and the code you attached does not clarify the situation. In general, if you can send us a complete example (i.e., one that compiles) there are more chances we can be helpful. My suggestion is to forget about what you wrote above and declare `static' what you want to be static and non-static what you want to be non-static. Then we will see what the actual problem is.
The problem is in this class:
class Cmd{
public:
//ph which I have in each statement NNC_Polyhedron ph_in;
//these are Hashmap the first element is the dimension //and the second the variable to print static map<dimension_type, string> var_to_print; static map<dimension_type, string>::iterator iter;
Why is `iter' a member of the class?
public:
static void Cmd::new_output_function(ostream& s, const Variable v) { iter = var_to_print.find(v.id()); s << iter->second; }
Why isn't `iter' local to this function? (And, by the way, why don't you check whether `v' was actually found or not?)
void Cmd::print_constraints(const ConSys& cs, const string& intro, ostream& s){ using namespace IO_Operators;
// Save the default output function. Variable::Output_Function_Type* p_default_output_function = Variable::get_output_function(); // Install an alternate output function. Variable::set_output_function(new_output_function); /* Your code of print_constraints() */ /* Code to restore the default output function. */ [...]
};
Looks reasonable.
when I compile it the g++ gives me these errors:
/tmp/ccOzbfS7.o(.gnu.linkonce.t._ZN3Cmd19new_output_functionERSoN23Parma_Polyhedra_Library8VariableE+0x24): In function `Cmd::new_output_function(std::basic_ostream<char, std::char_traits<char> >&, Parma_Polyhedra_Library::Variable)': : undefined reference to `Cmd::var_to_print' [...] : undefined reference to `Cmd::iter' [...]
This seems to be a FAQ, indeed: see http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.10
I hope this helps. If not, please come back to us. All the best,
Roberto