The International Simutrans Forum

Development => Technical Documentation => Topic started by: hreintke on January 09, 2013, 08:06:25 PM

Title: Access to alle_wegtypen
Post by: hreintke on January 09, 2013, 08:06:25 PM
LS,

In wegbauer.cc there is the following declaration


static stringhashtable_tpl <const weg_besch_t *> alle_wegtypen;


I tried to access that in simworld.cc using :


FOR(stringhashtable_tpl<weg_besch_t const*>, const& i, alle_wegtypen) {

};


But got the errormessage "'alle_wegtypen' : undeclared identifier

Is there another way to access this hashtable ? (and other simular tables with generic info ?)

Herman
Title: Re: Access to alle_wegtypen
Post by: Dwachs on January 09, 2013, 08:26:29 PM
You have to declare it im simrowld.cc first before you can use it:

extern stringhashtable_tpl <const weg_besch_t *> alle_wegtypen;
...
FOR(stringhashtable_tpl<weg_besch_t const*>, const& i, alle_wegtypen) {

};

Title: Re: Access to alle_wegtypen
Post by: Ters on January 09, 2013, 09:21:17 PM
And remove the static keyword in wegbauer.cc, which limits the symbol's scope to that file.
Title: Re: Access to alle_wegtypen
Post by: hreintke on January 09, 2013, 10:37:09 PM
Thanks,

Works,

Further with experiments now.

Herman
Title: Re: Access to alle_wegtypen
Post by: prissi on January 10, 2013, 11:33:40 AM
However, the proper OOP way would to have a static function in wegbauer to return what you want.
Title: Re: Access to alle_wegtypen
Post by: Ters on January 10, 2013, 04:46:52 PM
Quote from: prissi on January 10, 2013, 11:33:40 AM
However, the proper OOP way would to have a static function in wegbauer to return what you want.

And turn alle_wegtypen into a static member variable in the same class.
Title: Re: Access to alle_wegtypen
Post by: hreintke on January 10, 2013, 09:30:30 PM
Having trouble to get it done the right way.

I interpreted your remarks to do :

Include in wegbauer.h

class wegbauer_t
{
public:
stringhashtable_tpl <const weg_besch_t *> wegbauer_t::get_alle_wegtypen();


include in wegbauer.cc

stringhashtable_tpl <const weg_besch_t *> wegbauer_t::get_alle_wegtypen() {
return alle_wegtypen;
}

And then use in this way:

FOR(stringhashtable_tpl<weg_besch_t const*>, const& i, wegbauer_t::get_alle_wegtypen()) {
buf.printf("ways %s %d \n ",i.value->get_name(),i.value->get_retire_year_month());
};


But I get already an error on the wegbauer.h change


1>\gui\../bauer/wegbauer.h(32): error C2143: syntax error : missing ';' before '<'
1>\gui\../bauer/wegbauer.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>\gui\../bauer/wegbauer.h(32): error C2238: unexpected token(s) preceding ';'


Can you point me to the mistake I am making ?

Herman
Title: Re: Access to alle_wegtypen
Post by: prissi on January 10, 2013, 10:39:33 PM
The function must be declared static. On a second note copying of the hashtable will fail be desing too. Either return a reference or a pointer to the hashtable:

public:
  static stringhashtable_tpl <const weg_besch_t *> *wegbauer_t::get_alle_wegtypen();
Title: Re: Access to alle_wegtypen
Post by: hreintke on January 11, 2013, 06:30:19 PM
LS,

Sorry to bother you again. I am not experienced in using templates etc and learning on the job. Should get better each time I learn more basics.

I managed to get the declarations in without getting errormessages. Needed to include "../tpl/stringhashtable_tpl.h" into wegbauer.h and then was able to define in wegbauer.h


public:
static stringhashtable_tpl <const weg_besch_t *> *wegbauer_t::get_alle_wegtypen();


and in wegbauer.cc


  stringhashtable_tpl <const weg_besch_t *> *wegbauer_t::get_alle_wegtypen() {
return &alle_wegtypen;
}


This compiles without errors.

But then using the function I did :


FOR(stringhashtable_tpl<weg_besch_t const*>, const i, wegbauer_t.get_alle_wegtypen()) {
buf.printf("ways %s %d \n ",i.value->get_name(),i.value->get_retire_year_month());
};

and also included "tpl/stringhashtable_tpl.h".

The compiler spits out errormessages starting with


1>simworld.cc(1903): error C2143: syntax error : missing ')' before '.'
1>simworld.cc(1903): error C2059: syntax error : '.'
1>simworld.cc(1903): warning C4552: '!' : operator has no effect; expected operator with side-effect


The tooltip above the FOR says :

No suitable constructor exists to convert from "stringhashtable_tpl<const weg_besch_t const*>*" to "stringhashtable_tpl<const weg_besch_t const*>"

Any hints appreciated

Herman
Title: Re: Access to alle_wegtypen
Post by: Ters on January 11, 2013, 06:45:01 PM
If I read the FOR macro right, this should work without copying the hashtable:


FOR(stringhashtable_tpl<weg_besch_t const*>, const i, *wegbauer_t.get_alle_wegtypen()) {
  buf.printf("ways %s %d \n ",i.value->get_name(),i.value->get_retire_year_month());
};

Note the dereferencing of the pointer returned by get_alle_wegtypen().
Title: Re: Access to alle_wegtypen
Post by: hreintke on January 11, 2013, 09:31:10 PM
That one of the option i tried but when using this.

I don't get a tooltip that an error is in the source but when compiling I get the error :


1>simworld.cc(1903): warning C4832: token '.' is illegal after UDT 'wegbauer_t'
1>          c:\utilities\mingw\msys\1.0\home\herman\simutest\trunk\bauer/wegbauer.h(30) : see declaration of 'wegbauer_t'
1>simworld.cc(1903): error C2275: 'wegbauer_t' : illegal use of this type as an expression
1>          c:\utilities\mingw\msys\1.0\home\herman\simutest\trunk\bauer/wegbauer.h(30) : see declaration of 'wegbauer_t'
1>simworld.cc(1903): error C2228: left of '.get_alle_wegtypen' must have class/struct/union


Herman
Title: Re: Access to alle_wegtypen
Post by: Ters on January 11, 2013, 10:08:51 PM
Sorry. I think there is another error I missed. My C++ is getting rusty. Try this:


FOR(stringhashtable_tpl<weg_besch_t const*>, const i, *wegbauer_t::get_alle_wegtypen()) {
  buf.printf("ways %s %d \n ",i.value->get_name(),i.value->get_retire_year_month());
};
Title: Re: Access to alle_wegtypen
Post by: hreintke on January 11, 2013, 10:20:40 PM
Thanks,

Wow, what a silly mistake I overlooked.

Thanks for helping. Hope I can give something back when finishing my experiments.

Herman
Title: Re: Access to alle_wegtypen
Post by: Ters on January 11, 2013, 10:46:55 PM
I find that many programming problems that drag out in time are because of some silly tiny mistake. (Today, I spent the day at work rewriting what I've been rewriting the previous last two days, just because I overlooked a tiny detail.)
Title: Re: Access to alle_wegtypen
Post by: prissi on January 11, 2013, 11:58:40 PM
In this case the compiler should be able to give a better error message imho. At least it would not impossible.
Title: Re: Access to alle_wegtypen
Post by: Ters on January 12, 2013, 09:28:23 AM
For the latter problem, it was the warning that held the clue, not the errors.