将模板类作为参数传递给类方法,并使用模板类作为向量模板类型 自从我用C++编写模板以来,已经有一段时间了,但现在我真的需要它们了。

将模板类作为参数传递给类方法,并使用模板类作为向量模板类型 自从我用C++编写模板以来,已经有一段时间了,但现在我真的需要它们了。,c++,C++,我重现了我遇到的一个问题,我不记得解决方案实际上是如何进行的 #include <iostream> #include <vector> namespace problem { template <typename T> class data { public: inline data(T var) { this->var = var; }

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

#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;
}
#包括
#包括
名称空间问题{
模板
类数据{
公众:
内联数据(T变量){
这->var=var;
}
私人:
T-var;
};
类存储{
公众:
内联无效推送(问题::数据*数据){
此->VData.push_back(数据);
}
私人:
std::矢量数据;
};
};
int main(){
问题::存储*测试存储=新问题::存储();
问题::数据*测试数据=新问题::数据(256);
testStorage->push(testData);
删除测试数据;
删除测试存储;
返回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*’
问题。cpp:17:35:错误:未在此作用域中声明“T”
问题。cpp:17:36:错误:模板参数1无效
问题。cpp:21:30:错误:“T”未在此范围内声明
问题。cpp:21:31:错误:模板参数1无效
问题。cpp:21:34:错误:模板参数1无效
问题。cpp:21:34:错误:模板参数2无效
problem.cpp:在成员函数“void problem::storage::push(int*)”中:
problem.cpp:18:17:错误:请求成员“push_back”in'((problem::storage*)this)->problem::storage::VData',它是非类类型“int”
problem.cpp:在函数“int main()”中:
problem.cpp:29:28:错误:调用“problem::storage::push(problem::data*&)”时没有匹配的函数
问题。cpp:29:28:注:候选人是:
问题.cpp:17:16:注意:无效问题::存储::推送(int*)
problem.cpp:17:16:注意:参数1从'problem::data*'到'int*'的转换未知
我知道我可以使用成员模板,但是我如何处理向量呢

template <typename T>
inline void push(problem::data<T> * data) {
    this->VData.push_back(data);
}
模板
内联无效推送(问题::数据*数据){
此->VData.push_back(数据);
}
如果我使用成员模板,那么向量定义将留下这些错误

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’
问题。cpp:22:30:错误:未在此作用域中声明“T”
问题。cpp:22:31:错误:模板参数1无效
问题。cpp:22:34:错误:模板参数1无效
问题。cpp:22:34:错误:模板参数2无效
problem.cpp:在成员函数“void problem::storage::push(problem::data*)”中:
问题.cpp:19:17:错误:请求'this->.VData'中的成员'push_back',该成员为非类类型'int'

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

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;
};

可能尝试使用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;
}
#包括
#包括
#包括
名称空间问题{
模板
类数据{
公众:
内联数据(T变量){
这->var=var;
}
私人:
T-var;
};
类存储{
公众:
模板
内联无效推送(问题::数据*数据){
此->VData.push_back(数据);
}
私人:
std::矢量数据;
};
};
int main(){
问题::存储*测试存储=新问题::存储();
问题::数据*测试数据=新问题::数据(256);
testStorage->push(testData);
问题::数据*testData1=新问题::数据(1.);
testStorage->push(testData1);
删除测试数据;
删除testData1;
删除测试存储;
返回0;
}

但是,在使用vector中的数据之前,您需要将boost::any转换为您的类型如果您想要能够存储多种类型的值的存储,您可以尝试以下方法:

类存储{
结构数据库{};
模板
结构数据:数据库{
数据(K值):值(值){
K值;
};
typedef std::向量容器_类型;
公众:
~storage(){
而(!this->VData.empty()){
删除此->VData.back();
此->VData.pop_back();
}
}
模板
内联无效推送(PV){
这->VData.push_back(新数据

(v)); } 模板 P&get(inti){returnstatic_cast(this->VData[i])->value_;} 私人: 容器类型VData; };


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

我找到了一种解决方案,这正是我所寻找的,但我不确定这是否是一种好的做法: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;
};
类存储{
公众:
模板
内联无效推送(问题::数据*数据){
这->VData.push_back(重新解释(数据));
}
模板
内联问题::数据*draw(){
问题::data*data=reinterpret_cast(VData.back());
返回数据;
}
私人:
std::矢量数据;
};

如果您有一个依赖于它的数据成员,为什么不将您的整个
存储也制作成a类模板呢?很抱歉没有提及,但如果我认为这是个好主意,我会这么做的。事实上,我拥有的实际“存储”类将使用10多个不同的模板作为数据成员<代码>模板
这在我看来不太好/@Ternek所有这些投诉都有标准的解决方案,而不是重新解释!!!请参见
Boost::Any
(和/或键入已擦除的容器)、
template
std::tuple
(使用数据成员类型的长列表)将存储设置为模板是可行的,但这只是我遇到的问题的再现,存储需要有多个模板类型才能作为模板。我可以为存储T使用特定类型,但当我尝试推送具有T类型而不是用于存储T的类型的类模板时,会出现故障。@Temek如果您有模板数据成员,则该类需要是模板。没有一个简单的方法可以解决这个问题
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;
};
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;
};