Author Topic: Access to alle_wegtypen  (Read 2605 times)

0 Members and 1 Guest are viewing this topic.

Offline hreintke

Access to alle_wegtypen
« on: January 09, 2013, 08:06:25 PM »
LS,

In wegbauer.cc there is the following declaration

Code: [Select]
static stringhashtable_tpl <const weg_besch_t *> alle_wegtypen;

I tried to access that in simworld.cc using :

Code: [Select]
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

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4251
  • Total likes: 177
  • Helpful: 149
  • Languages: EN, DE, AT
Re: Access to alle_wegtypen
« Reply #1 on: January 09, 2013, 08:26:29 PM »
You have to declare it im simrowld.cc first before you can use it:
Code: [Select]
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.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4856
  • Total likes: 200
  • Helpful: 108
  • Languages: EN, NO
Re: Access to alle_wegtypen
« Reply #2 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.

Offline hreintke

Re: Access to alle_wegtypen
« Reply #3 on: January 09, 2013, 10:37:09 PM »
Thanks,

Works,

Further with experiments now.

Herman

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 8819
  • Total likes: 322
  • Helpful: 229
  • Languages: De,EN,JP
Re: Access to alle_wegtypen
« Reply #4 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.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4856
  • Total likes: 200
  • Helpful: 108
  • Languages: EN, NO
Re: Access to alle_wegtypen
« Reply #5 on: January 10, 2013, 04:46:52 PM »
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.

Offline hreintke

Re: Access to alle_wegtypen
« Reply #6 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
Code: [Select]
class wegbauer_t
{
public:
stringhashtable_tpl <const weg_besch_t *> wegbauer_t::get_alle_wegtypen();

include in wegbauer.cc
Code: [Select]
stringhashtable_tpl <const weg_besch_t *> wegbauer_t::get_alle_wegtypen() {
return alle_wegtypen;
}
And then use in this way:
Code: [Select]
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

Code: [Select]
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

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 8819
  • Total likes: 322
  • Helpful: 229
  • Languages: De,EN,JP
Re: Access to alle_wegtypen
« Reply #7 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();

Offline hreintke

Re: Access to alle_wegtypen
« Reply #8 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

Code: [Select]
public:
static stringhashtable_tpl <const weg_besch_t *> *wegbauer_t::get_alle_wegtypen();

and in wegbauer.cc

Code: [Select]
  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 :

Code: [Select]
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

Code: [Select]
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

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4856
  • Total likes: 200
  • Helpful: 108
  • Languages: EN, NO
Re: Access to alle_wegtypen
« Reply #9 on: January 11, 2013, 06:45:01 PM »
If I read the FOR macro right, this should work without copying the hashtable:

Code: [Select]
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().

Offline hreintke

Re: Access to alle_wegtypen
« Reply #10 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 :

Code: [Select]
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

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4856
  • Total likes: 200
  • Helpful: 108
  • Languages: EN, NO
Re: Access to alle_wegtypen
« Reply #11 on: January 11, 2013, 10:08:51 PM »
Sorry. I think there is another error I missed. My C++ is getting rusty. Try this:

Code: [Select]
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());
};

Offline hreintke

Re: Access to alle_wegtypen
« Reply #12 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

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4856
  • Total likes: 200
  • Helpful: 108
  • Languages: EN, NO
Re: Access to alle_wegtypen
« Reply #13 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.)

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 8819
  • Total likes: 322
  • Helpful: 229
  • Languages: De,EN,JP
Re: Access to alle_wegtypen
« Reply #14 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.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4856
  • Total likes: 200
  • Helpful: 108
  • Languages: EN, NO
Re: Access to alle_wegtypen
« Reply #15 on: January 12, 2013, 09:28:23 AM »
For the latter problem, it was the warning that held the clue, not the errors.