0

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

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it happens that i inherit from public deque<T> a i was passing to push back a T*. The compiler comfuse me! thanks,
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.