News:

Want to praise Simutrans?
Your feedback is important for us ;D.

Access to alle_wegtypen

Started by hreintke, January 09, 2013, 08:06:25 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

hreintke

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

Dwachs

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) {

};

Parsley, sage, rosemary, and maggikraut.

Ters

And remove the static keyword in wegbauer.cc, which limits the symbol's scope to that file.

hreintke

Thanks,

Works,

Further with experiments now.

Herman

prissi

However, the proper OOP way would to have a static function in wegbauer to return what you want.

Ters

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.

hreintke

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

prissi

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();

hreintke

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

Ters

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().

hreintke

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

Ters

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());
};

hreintke

Thanks,

Wow, what a silly mistake I overlooked.

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

Herman

Ters

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.)

prissi

In this case the compiler should be able to give a better error message imho. At least it would not impossible.

Ters

For the latter problem, it was the warning that held the clue, not the errors.