Results 1 to 16 of 16

Thread: strange problem passing a vector

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default strange problem passing a vector

    Hello, see this below! Any suggest? Thanks.
    Qt Code:
    1. void Tree::createTree (vector<Node>*& vec) { }
    2.  
    3. void foo2 (vector<Node>*& vec) { }
    4.  
    5. int main () {
    6. vector<Node>* listNode = new vector<Node>();
    7. vector->push_back(Node(2));
    8. tree->createTree(listNode); // this is not ok: 10,000 compile errors
    9. foo2 (listNode); // this ok. Are they different? It seems the same thing to me.....
    10. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: strange problem passing a vector

    What is the first error you get?

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strange problem passing a vector

    Qt Code:
    1. error C2903: 'rebind' : symbol is neither a class template nor a function template
    2. c:\program files\microsoft visual studio 8\vc\include\vector(426) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
    3. with
    4. [
    5. _Ty=Node,
    6. _Alloc=Node
    7. ]
    8. ...........tree\main.cpp(41) : see reference to class template instantiation 'std::vector<_Ty,_Ax>' being compiled
    To copy to clipboard, switch view to plain text mode 
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strange problem passing a vector

    Quote Originally Posted by mickey View Post
    Hello, see this below! Any suggest? Thanks.
    Qt Code:
    1. void Tree::createTree (vector<Node>*& vec) { }
    2.  
    3. void foo2 (vector<Node>*& vec) { }
    4.  
    5. int main () {
    6. vector<Node>* listNode = new vector<Node>();
    7. vector->push_back(Node(2));
    8. tree->createTree(listNode); // this is not ok: 10,000 compile errors
    9. foo2 (listNode); // this ok. Are they different? It seems the same thing to me.....
    10. }
    To copy to clipboard, switch view to plain text mode 
    To note: if I do
    Qt Code:
    1. void Tree::createTree (vector<Node>** vec) { }
    2. .................................
    3. tree->createTree(&listNode);
    To copy to clipboard, switch view to plain text mode 
    it works fine!
    Hints ,please?
    Regards

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strange problem passing a vector

    Hello,
    doesn't anyone have an idea why code (#post1) doesn't work???

    thanks...
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: strange problem passing a vector

    I'd begin with the notice, that a construction like "xxx*&" seems... weird... but ok...

    How did you declare and create "tree"?

  7. #7
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: strange problem passing a vector

    Everything seems alright, pretty weird error indeed.

    Also one minor thing i noticed was tree object wasn't declared but i think that part was trimmed before posting here.

    Is the tree code in separate files ? Do have a look if there are any overloaded members that might be interfering.
    Also if possible can you post the complete code ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strange problem passing a vector

    I'm sorry but I had change it in the second manner (vector<Node>**) and now I try to change into original version to see the error:now it works. Could be a temporary .net problem (restart it I mean) ???
    I found the "*&" in Thinking in c++; why doesn't it "ok"? Does have any sense all or it shuold be better in main() declare vecNode on the stack instead on heap?
    thanks
    (if problem arise again I update post)
    Regards

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: strange problem passing a vector

    Quote Originally Posted by mickey View Post
    I'm sorry but I had change it in the second manner (vector<Node>**) and now I try to change into original version to see the error:now it works. Could be a temporary .net problem (restart it I mean) ???
    I found the "*&" in Thinking in c++; why doesn't it "ok"? Does have any sense all or it shuold be better in main() declare vecNode on the stack instead on heap?
    thanks
    (if problem arise again I update post)
    *& idiom is used to pass the pointer as reference. That is the pointer passed from the caller is also modified. This is usually used to return multiple values allocated on heap. I think it is ok to use it.
    Usually it is better to declare elements on stack rather that heap but it does depend on how you use it in you application.

    Here is a small example i tried to assert my assumption.
    Qt Code:
    1. void create(int *t)
    2. {
    3. t = new int(2);
    4. }
    5.  
    6. void createRef(int *&t)
    7. {
    8. t = new int(2);
    9. }
    10.  
    11. int main()
    12. {
    13. int *h, *k;
    14. h = k = 0;
    15. create(h);
    16. Q_ASSERT(h != 0); // fails
    17. createRef(k);
    18. Q_ASSERT(k != 0); // succeeds.
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: strange problem passing a vector

    I think that if you have a method that returns void, passing a reference to a pointer to return something from the method doesn't make much sense - it's better to use a more... traditional approach. But in the above situation it doesn't make sense to pass a reference to a pointer.

  11. #11
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: strange problem passing a vector

    Quote Originally Posted by wysota View Post
    I think that if you have a method that returns void, passing a reference to a pointer to return something from the method doesn't make much sense - it's better to use a more... traditional approach. But in the above situation it doesn't make sense to pass a reference to a pointer.
    Yes, but may be you can use them in helper functions like this.. It saves function calls as well as neatly modularizes the code. Ofcourse it is not conventional.

    void createResources(int *&handle, SomeResource *&s, Pen *& pen)
    {
    }
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: strange problem passing a vector

    Yes, maybe... But I wouldn't. Try maintaining such code... Besides, in most cases you'd pass a reference to an object, not to its pointer.

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strange problem passing a vector

    Quote Originally Posted by Gopala Krishna View Post
    Usually it is better to declare elements on stack rather that heap but it does depend on how you use it in you application.
    [/CODE]
    What do you mean? What's the reason lead me to allocate the vecNode(in the main) in on the stack or on heap?
    Regards

  14. #14
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: strange problem passing a vector

    Quote Originally Posted by mickey View Post
    What do you mean? What's the reason lead me to allocate the vecNode(in the main) in on the stack or on heap?
    I mean dont create the vecNode using new. that is, dont use
    Qt Code:
    1. vector<Node> *listNode = new vector<Node>;
    To copy to clipboard, switch view to plain text mode 
    Instead use
    Qt Code:
    1. vector<Node> listNode;
    To copy to clipboard, switch view to plain text mode 

    This removes explicit new and delete operations and simplifies code in many places. Ofcourse this is not mandatory, you might find the first solution suitable in your case.

    @Wysota: Yeah, that is the reason i told it is unconventional. But it is not "unmaintainable" - atleast in a group of experienced c++ programmers.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  15. #15
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strange problem passing a vector

    Quote Originally Posted by Gopala Krishna View Post
    Ofcourse this is not mandatory, you might find the first solution suitable in your case.
    when can it be suitable? Any examples? (for me there's no difference)...
    Regards

  16. #16
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: strange problem passing a vector

    Quote Originally Posted by mickey View Post
    when can it be suitable? Any examples? (for me there's no difference)...
    It might be usable when you have to share one vector amongst many instances of same or different objects. Instead of holding many copies you will be holding pointers to vectors.

    But you may note that qt's collection classes are implicitly shared and are more easier to use than stl classes. stl classes are powerful only if you know how to extract maximum from them. Otherwise qt's collection classes should suffice for most cases.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. Strange shortcut problem
    By blukske in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2007, 10:26
  2. Strange problem with events on Unix
    By sukanyarn in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2006, 02:45
  3. Strange Problem with JPEG Support on win XP
    By caligula in forum Installation and Deployment
    Replies: 3
    Last Post: 18th September 2006, 10:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.