Results 1 to 20 of 24

Thread: pointer in a class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer in a class

    Sorry, I made a mistake in my template declaration. Try:

    Qt Code:
    1. namespace std { template <class T> class vector; }
    To copy to clipboard, switch view to plain text mode 

    That compiles fine on my computer.

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

    Default Re: pointer in a class

    Quote Originally Posted by Shadowfiend View Post
    Sorry, I made a mistake in my template declaration. Try:
    Qt Code:
    1. namespace std { template <class T> class vector; }
    To copy to clipboard, switch view to plain text mode 
    That compiles fine on my computer.
    Maybe it can be the solution; but then, my problem is in a other place; try to compile this ,please (It doens't compile on .net2005)
    Qt Code:
    1. //main.cpp
    2. #include <iostream>
    3. #include "forward.h"
    4. #include <vector>
    5. using namespace std;
    6.  
    7. int main (int argc, char argv[]) {
    8.  
    9. Forward f;
    10. vector<float>* fvec = new vector<float>; //EDIT <----------------------------
    11.  
    12. return EXIT_SUCCESS;
    13. }
    14. //forward.h
    15. #ifndef FORWARD_H
    16. #define FORWARD_H
    17.  
    18. namespace std { template <class T> class vector; }
    19. // #include <vector> // with only this, all compiles
    20.  
    21. class Forward {
    22. std::vector<float>* _vec;
    23. public:
    24. Forward();
    25. ~Forward();
    26. };
    27.  
    28. #endif FORWARD_H
    29.  
    30. //forward.cpp
    31. #include "forward.h"
    32. #include <vector>
    33. using namespace std;
    34.  
    35. Forward::Forward() {
    36.  
    37. _vec = new vector<float>;
    38. }
    39. Forward::~Forward() {}
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 25th May 2008 at 22:25.
    Regards

  3. #3
    Join Date
    Jan 2006
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer in a class

    Ah, that's right. std::vector takes an std::allocator as its second parameter. Well that just gets nasty...

    Here's a place where this same issue is discussed: http://objectmix.com/c/251244-forwar...compliant.html

    Looks like it's possible, but the advantage is negligible and there is not necessarily a guarantee that it will work.

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

    Default Re: pointer in a class

    Sorry, I did a mistake; what I want to show is that the code doens't still work; see the code I correct it....what I say is that, with your solution, if I put #define <vector> in such file that include even "forward.h", the compiler gets errors; does anyone know how to correct the last code above ,please?
    Regards

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

    Default Re: pointer in a class

    Is it really worth the trouble? Use precompiled headers and your problem will disappear (along side a 100MB of your disk space).

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

    Default Re: pointer in a class

    Quote Originally Posted by wysota View Post
    Is it really worth the trouble? Use precompiled headers and your problem will disappear (along side a 100MB of your disk space).
    When I start to do this forward declaration, I didn't think it was "particularly". What I'd like understand to this point is:
    1. Why these all problems
    2. why 100MB !!!
    3. When Does the forward declaration is recommended

    Thanks in advance.
    Regards

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

    Default Re: pointer in a class

    Quote Originally Posted by mickey View Post
    1. Why these all problems
    You should know, it's your code

    2. why 100MB !!!
    When you use precompiled headers this is more or less the amount of spaces those headers take after precompilation.

    3. When Does the forward declaration is recommended
    We've been all over this question... It's not recommended in this particular case, just include the vector and skip to some more interesting work.

  8. #8
    Join Date
    Jan 2006
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer in a class

    Concerning (1) -- because STL container classes use enough template magic that forward-declaring them is a pain.

    As for (3) -- forward declaration is mostly to speed up compilation times. Precompiled headers take care of that problem to some extent by only compiling the header part once, eliminating most of the overhead (so that the vector header doesn't need to be parsed every time a file is compiled that includes your class header that uses it).

    In this case, obviously the effort to forward-declare a vector isn't worth the small time savings during compilation.

Similar Threads

  1. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  2. Saving object pointer
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 11th January 2008, 11:53
  3. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  4. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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
  •  
Qt is a trademark of The Qt Company.