将模板类作为参数传递给类方法,并使用模板类作为矢量模板类型

时间:2012-07-19 11:41:30

标签: c++

自从我使用C ++模板已经有一段时间了,但现在我真的需要它们。

我重现了我遇到的一个问题,我不记得解决方案是如何实现的。

#include <iostream>
#include <vector>

namespace problem {
    template <typename T>
    class data {
        public:
            inline data(T var) {
                this->var = var;
            }
        private:
            T var;
    };

    class storage {
        public:
            inline void push(problem::data<T> * data) {
                this->VData.push_back(data);
            }
        private:
            std::vector<problem::data<T> *> VData;
    };
};

int main() {
    problem::storage * testStorage = new problem::storage();
    problem::data<int> * testData = new problem::data<int>(256);

    testStorage->push(testData);

    delete testData;
    delete testStorage;
    return 0;
}

g ++ -Wall problem.cpp给出了以下错误。

problem.cpp:17:35: error: ‘T’ was not declared in this scope
problem.cpp:17:36: error: template argument 1 is invalid
problem.cpp:21:30: error: ‘T’ was not declared in this scope
problem.cpp:21:31: error: template argument 1 is invalid
problem.cpp:21:34: error: template argument 1 is invalid
problem.cpp:21:34: error: template argument 2 is invalid
problem.cpp: In member function ‘void problem::storage::push(int*)’:
problem.cpp:18:17: error: request for member ‘push_back’ in ‘((problem::storage*)this)->problem::storage::VData’, which is of non-class type ‘int’
problem.cpp: In function ‘int main()’:
problem.cpp:29:28: error: no matching function for call to ‘problem::storage::push(problem::data<int>*&)’
problem.cpp:29:28: note: candidate is:
problem.cpp:17:16: note: void problem::storage::push(int*)
problem.cpp:17:16: note:   no known conversion for argument 1 from ‘problem::data<int>*’ to ‘int*’

我知道我可以使用成员模板,但是我对矢量做了什么?

template <typename T>
inline void push(problem::data<T> * data) {
    this->VData.push_back(data);
}

如果我使用成员模板,那么矢量定义将留下这些错误。

problem.cpp:22:30: error: ‘T’ was not declared in this scope
problem.cpp:22:31: error: template argument 1 is invalid
problem.cpp:22:34: error: template argument 1 is invalid
problem.cpp:22:34: error: template argument 2 is invalid
problem.cpp: In member function ‘void problem::storage::push(problem::data<T>*)’:
problem.cpp:19:17: error: request for member ‘push_back’ in ‘this->.VData’, which is of non-class type ‘int’

4 个答案:

答案 0 :(得分:2)

也许尝试使用boost :: any?

#include <iostream>
#include <vector>
#include <boost/any.hpp>

namespace problem {
    template <typename T>
    class data {
        public:
            inline data(T var) {
                this->var = var;
            }
        private:
            T var;
    };

    class storage {
        public:
            template<class T>
            inline void push(problem::data<T> * data) {
                this->VData.push_back(data);
            }
        private:
            std::vector<boost::any> VData;
    };
};

int main() {
    problem::storage * testStorage = new problem::storage();
    problem::data<int> * testData = new problem::data<int>(256);

    testStorage->push<int>(testData);

    problem::data<float> * testData1 = new problem::data<float>(1.);
    testStorage->push<float>(testData1);

    delete testData;
    delete testData1;
    delete testStorage;
    return 0;
}

但是在使用vector

中的数据之前,你需要从boost :: any转换为你的类型

答案 1 :(得分:2)

如果您希望存储可以存储多种类型的值,您可以尝试以下内容:http://ideone.com/jjuVq

class storage {
    struct data_base {};

    template <class K> 
    struct data: data_base {
        data(K value): value_(value) {}
        K value_;
    };

    typedef std::vector<data_base*> container_type;

public:
    ~storage() {
        while(!this->VData.empty()) {
            delete this->VData.back();
            this->VData.pop_back();
        }
    }
    template <class P>
    inline void push(P v) {
        this->VData.push_back(new data<P>(v));
    }
    template <class P>
    P &get(int i) { return static_cast<data<P>*>(this->VData[i])->value_; }
private:
    container_type VData;
};

或者只使用boost :: any作为容器的值类型。

答案 2 :(得分:1)

您的存储类有一个依赖于模板参数的数据成员,因此您应该将其设为类模板:

template <typename T>
class storage {
    public:
        inline void push(problem::data<T> * data) {
            this->VData.push_back(data);
        }
    private:
        std::vector<problem::data<T> *> VData;
};

否则,您可以使storage适用于特定类型T

class storage {
    public:
        inline void push(problem::data<int> * data) {
            this->VData.push_back(data);
        }
    private:
        std::vector<problem::data<int> *> VData;
};

答案 3 :(得分:-3)

我找到了一个解决方案,这是我想要的,但我不确定这是不是很好的做法。 :P

class storage {
    public:
        template <typename T>
        inline void push(problem::data<T> * data) {
            this->VData.push_back(reinterpret_cast<char*>(data));
        }
        template <typename T>
        inline problem::data<T> * draw() {
            problem::data<T> * data = reinterpret_cast<problem::data<T>*>(VData.back());
            return data;
        }
    private:
        std::vector<char*> VData;
};