storing pointers to a templated class in a vector

This is a discussion on storing pointers to a templated class in a vector within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a boost: tr_vector of a class with a templated member. I'm not sure of the correct ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    15

    storing pointers to a templated class in a vector

    I'm trying to create a boost:tr_vector of a class with a templated member. I'm not sure of the correct way to do this, but what I've tried is below. With this code I get a compiler error which says:

    Code:
    //GCC 4.3.2
    error: type/value mismatch at argument 1 in template parameter list for ‘template<class T, class CloneAllocator, class Allocator> class boost::ptr_vector’
    
    expected a type, got ‘Test’
    Is there some way to fix my implementation or, if this idea will not work, how would you suggest I do this?

    Thanks for any help

    Code:
    #include <iostream>
    #include <string>
    #include <boost/ptr_container/ptr_vector.hpp>
    
    using namespace std;
    
    template <typename T> class Test
    {
        private:
            T var;
    
        public:
            explicit Test(const T &t) : var(t) {}
    };
    
    int main()
    {
        boost::ptr_vector<Test> vec;
        vec.push_back(new Test<int>(123));
        vec.push_back(new Test<double>(1.23));
        vec.push_back(new Test<string>("test"));
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Test is a class template, so this line does not make sense as you are trying to store a pointer to Test:
    Code:
    boost::ptr_vector<Test> vec;
    You would need to decide whether you want to store pointers to Test<int>, or Test<double>, or Test<string>.

    Perhaps you should take a look at boost::any and boost::variant.
    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

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    15
    Thanks for the reply. I hadn't noticed boost::variant before; it does what I wanted.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you can store a pointer to a non-template base class from which the templates are derived.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    But that opens up its own bag of worms, so watch out.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector Addition Using OOP?
    By everydaybh in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2009, 05:09 AM
  2. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 03:44 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  5. New silly newb questoin. Returning vector from a class
    By Gatt9 in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2005, 07:50 PM

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