Thread: storing pointers to a templated class in a vector

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    838
    you can store a pointer to a non-template base class from which the templates are derived.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 04: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, 08:50 PM