开发者

more problem with nested structure

开发者 https://www.devze.com 2023-02-07 14:07 出处:网络
object *head = NULL, *tail = NULL;// 开发者_运维问答EDIT struct object { vector <int> data;
object *head = NULL, *tail = NULL;  // 开发者_运维问答EDIT
struct object { 
           vector <int> data;
           object * read ( void ) ;
           struct obj {
                obj *next; 
                object * brach ( object * ) ;
           };
};

object * object :: read ( void ) {
     ... // some code to read and return  (dynamically token space) pointer
}
object * object :: obj :: brach ( object * p ) {
     ... // some code to make link list and pointer to middle 
}

void show ( object * p ) {
     ... // to show data, from head to tail
}

A lot of question about nested structure, but I think all of them have similiar answer

  • If I want to put show function in read function, how can I use global show function in it?

    some effort ;

    object * object :: read ( void ) {
    
     ... // some code to read and return  (dynamically token space) pointer
    
     :: show ( head ) ;  ( ! ) 
    
     head :: obj :: show ( head ) ; ( ! )
    
     head . obj . show ( head ) ;   ( ! )
    
     }  
    

    all of the line marked with ( ! ), gives error, WHY

  • in main function

    object *p = new object ;
    ... // some code to read data
    object *tmp = NULL;
    tmp = p -> obj . brach ( p ) ; ( ! ) 
    
    **how** can I fix it ? 
    


Need more source code. Here are not declared variables after that can guess, which name belongs o which type. But:

The show(object*) function is a global function, so the call do not needs any prefix.

The object structure has any obj type member variable. Only a structure is declared and defined some methods of own and of its structure. So in the main function it cannot call the obj::brach method only if it would be a static function.

Declare a obj* head; member in object structure, create it (p->head = new object::obj()), and then call p->head->brach(..);


Seems you are from a non-C++ background (C#?). In response to some of your queries: Correct way to address a global from your scope of object::read() is either simply Show(Head) or ::Show(head) - I suspect the reason you are having problems here is because head is not defined.

In you main procedure you need to use the -> operator to dereference pointer specifically instead of: p.obj.brach(p) you are wanting p->obj->brach(p)

There seems to be a lot else wrong in the code and ideas expressed here but it's difficult to advise you without more source code and an idea of what you are trying to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消