strange problem...


Imagine the following:

1
2
3
4
5
6
7
8
...

void f() {
    list<_Node*> list1;
    list<_Node*> list2;
}

...



Then i compile it fails because it says _Node is undeclared in the second list, however it runs well in the first list, why this happens ?
Problem solved now, although i don't understand why exactly, so im gonna post a new example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct A {
    struct S {
        int _M_data;
        S* _M_left;
        S* _M_right;
    };
};


class B 
    : protected A {

    typedef A::S S;

    void f();
};


void B::f() {
    list<S*> list1;
    list<S*> list2;
}


the underlined code was what i added in order to B::f() work, but still strange because if i declare just 1 list, i would not need the typedef declaration.
What compiler are you using?
g++, can it be a bug ? i think not...
Last edited on
Weird. Compiles fine for me in g++, even after commenting out the typedef line.
What version of g++?

Is this the exact code, or are there templates involved? (ie, struct A is not a template in
your actual code)?
I found the problem, but even, it's still strange, i think it's some kind of name conflict with struct _Node, however it doesn't report as a name conflict., i'm going to post exactly the code that gives an error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <list>

using namespace std;

struct _Bst_base {
  struct _Node {
    int _M_value;
    _Node* _M_left;
    _Node* _M_right;
  };

  _Node* _M_top;
  int _M_size;
  int _M_depth;


  inline _Bst_base() 
  : _M_top(0), _M_size(0), _M_depth(0)
  {}
};

class binary_search_tree
  : protected _Bst_base {

public:
  inline binary_search_tree() 
  : _Bst_base()
  {}

  void print() const;
};


void binary_search_tree::print() const {
  list<_Node*> a;
  list<_Node*> b;
}


int main() {

}



Try to compile this code and it will fail, however if u change _Node to _node or another name, it compiles !!
Last edited on
It's likely '_Node' is used by the implementation for something. You should avoid identifiers beginning with underscore, at least for the global namespace.
This doesn't compile because there are syntax errors:

line 10 probably is supposed to have };

line 21 shouldn't have an open brace

After making those two changes it compiles fine for me.
I fixed that on this post, this was handwritten, on my real code it doesnt have any kind of syntax errors. I compiled exactly this code (already fixed) and it keeps to give the _Node error.
Last edited on
Names starting with two underscores or one underscore and a capital letter are reserved for the compiler.
u mean, i can't use variables starting with underscore and a capital letter ??
You should avoid them on the global namespace.
Topic archived. No new replies allowed.