templating with inheritance

This is a discussion on templating with inheritance within the C++ Programming forums, part of the General Programming Boards category; Hello, I attach you this code; i should have 2 problems: 1. (most important) the use of template gets these ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    templating with inheritance

    Hello,
    I attach you this code; i should have 2 problems:
    1. (most important) the use of template gets these errors; how come?
    Code:
    error LNK2019: unresolved external symbol "public: __thiscall Square<float>::~Square<float>(void)" (??1?$Square@M@@QAE@XZ) referenced in function _main
    main.obj : error LNK2019: unresolved external symbol "public: __thiscall Square<float>::Square<float>(float)" (??0?$Square@M@@QAE@M@Z) referenced in function _main
    ............\geometry\Debug\geometry.exe : fatal error LNK1120: 2 unresolved externals
    2. How can you see I need to do forward declaration with "vector" (but, because it doens't work, I opted to do #include <vector> in a temporaney way....
    Can you help me, please?

    Thanks.
    Attached Files Attached Files

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Without looking at your files, I guess you implemented the template in the cpp file, right?
    http://www.parashift.com/c++-faq-lit...html#faq-35.12
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by mickey0 View Post
    2. How can you see I need to do forward declaration with "vector" (but, because it doens't work, I opted to do #include <vector> in a temporaney way....
    Can you help me, please?
    As for this, since you can always include vector without troubles, using forward declarations for vector just isn't recommended.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    Quote Originally Posted by Elysia View Post
    As for this, since you can always include vector without troubles, using forward declarations for vector just isn't recommended.
    I now it by only now, but however is there any chance to do it ? (it just only for completeness)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Yes, there is.
    Code:
    template<typename T, typename Allocator> class vector;
    Should do it (not tested).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nope, that's invalid. You're not allowed to declare anything in the std namespace.

    Moreover, standard libraries are allowed to give their templates arbitrary additional template parameters, as long as they have defaults, which means that the argument list might simply be wrong.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,148
    Moreover, standard libraries are allowed to give their templates arbitrary additional template parameters, as long as they have defaults, which means that the argument list might simply be wrong.
    And don't you just love that oversight? *sigh*

    Soma

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by CornedBee View Post
    Nope, that's invalid. You're not allowed to declare anything in the std namespace.

    Moreover, standard libraries are allowed to give their templates arbitrary additional template parameters, as long as they have defaults, which means that the argument list might simply be wrong.
    Yes, I did think as much and I did say you should include proper headers instead of forward declaration, but the above might work. At least on VS's implementation. It might not be valid, but it might work.
    While possible, it's certainly not recommended.
    Last edited by Elysia; 05-26-2008 at 04:45 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Nope, that's invalid. You're not allowed to declare anything in the std namespace.
    More precisely, that results in undefined behaviour, so Elysia is correct to say that it might work depending on the implementation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    Quote Originally Posted by Elysia View Post
    Yes, there is.
    Code:
    template<typename T, typename Allocator> class vector;
    Should do it (not tested).
    It doens't work; try this other minimal example.......



    --
    For the question of template, I read
    this
    and It is more useful; I prefer add the line "template class Square<float>;" at the end of file; (my compiler seems not support explicit keyword...)
    Attached Files Attached Files

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    As they say, it may be implementation dependant.
    The second works (or would) fine, but is limited to float type only and is explicit instantiation (I believe), not a forward declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And don't you just love that oversight?
    Well, it was a deliberate decision. (If not a particularly good one.)

    What's an oversight is the lack of a header that provides forward declarations for the container library.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by CornedBee View Post
    What's an oversight is the lack of a header that provides forward declarations for the container library.
    Have you ever needed forward declarations for STL libraries, though?
    Aside from perhaps saving compile time, I have seen no reason to use forward declarations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,148
    Well, it was a deliberate decision. (If not a particularly good one.)
    An oversight may allow one to make a deliberate decision. I didn't mean to imply that the maintainers went: "Oops, forgot to specify an area of the standard library.". No, I think a lot of the C++ committee. I think they wasn't fully aware of the great potential of portable source facilities that could exist. As it is, every standard library distribution requires a specific implementation. It's a painful hack to deal with.

    What's an oversight is the lack of a header that provides forward declarations for the container library.
    At least we have forward declarations for (most of) the streams library.

    Aside from perhaps saving compile time, I have seen no reason to use forward declarations.
    If you measure compile time in hours that reason alone is sufficient.

    Soma

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by phantomotap View Post
    If you measure compile time in hours that reason alone is sufficient.
    Purely for compile speed then. How about PCH instead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Forms of Inheritance
    By kolucoms6 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2008, 03:09 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21