gpt4 book ai didi

c++ - 从模板继承 : scope error?

转载 作者:行者123 更新时间:2023-11-28 08:18:43 25 4
gpt4 key购买 nike

这是我的代码:

#include<stdarg.h>
#include<deque>
using namespace std;
template<typename T,int dim>
class trj:public deque<T>{
public:
void push_back(T,...);
virtual void set_calculate_method(int)=0;
};

template<typename T,int dim>
class bb:public trj<T,dim>{
public:
void set_calculate_method(int);
};

template<typename T,int dim>
void trj<T,dim>::push_back(T in,...){
va_list ap;
T aux;
va_start(ap,in);
aux=in;
deque<T>::push_back(new T[dim]);
for(int i=0;i<dim;i++){
*(deque<T>::back()+i)=aux;
aux=va_arg(ap,T);
}
va_end(ap);
}

int main(){
bb<double,3> t;
t.push_back(2,3,4);
return 0;
}

我有这个编译器错误

uno.cpp: In member function ‘void trj<T, dim>::push_back(T, ...) [with T = double, int dim = 3]’:
uno.cpp:57: instantiated from here
uno.cpp:16: error: no matching function for call to ‘trj< double, 3 >::push_back(double*)’
/usr/include/c++/4.4/bits/stl_deque.h:1201: note: candidates are: void std::deque <_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = double, _Alloc = std::allocator<double>]
uno.cpp:57: instantiated from here
uno.cpp:18: error: invalid type argument of ‘unary *’

如果我写“deque< T>::push_back( new T[dim]); "在那一行?

最佳答案

因为 deque<T>::push_back 期待一个 const 引用,而你正在传递一个非常量指针。

我不能确定,但​​我想你只是想做一个 deque<T>::resize 并传入 dim .

关于c++ - 从模板继承 : scope error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6718677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com