开发者

Inheritance from a template: scope error?

this is my code:

#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_sta开发者_运维百科rt(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;                                             
}                                                         

and i have this compiler error

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 *’

Why does the compiler emit " uno.cpp:16: error: no matching function for call to ‘trj< double, 3>::push_back(double*)" if i wrote "deque< T>::push_back(new T[dim]); " in that line?


Because deque<T>::push_back is expecting a const reference and you are passing a non-const pointer.

I can't be sure but I think you are just wanting to do a deque<T>::resize and pass in dim.


deque<T>::push_back wants T, not array of Ts.

By the way, I would strongly discourage deriving from deque, as its destructor is non-virtual. If some code deletes an instance of your class by a pointer to the base class, you'll get undefined behaviour.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜