错误:从'int'到'const char*'的转换无效

error: invalid conversion from 'int' to 'const char*'

本文关键字:无效 转换 char int 错误 const      更新时间:2023-10-16

我正在实现堆栈数据结构。当我在主函数中调用pop函数时,出现了错误。它像这样说:

  1. stack.h:13: error: invalid conversion from 'int' to 'const char*'
  2. stack.h:13: error: initializing argument 1 of 'int remove(const char*)

'

问题是我没有使用 charchar* 类型参数编写 remove 函数。所以我希望你们中的任何一个人能帮助我离开这里。感谢您的帮助!

template <typename T> 
class Stack : public Vector<T> {
public:
    Stack () {  Vector<T>(); }
    T pop() { return remove( this->size() - 1 ); } //stack.h:13
};
template <typename T> 
class Vector {
protected:
    int _size; 
    int _capacity;  
    T* _elem; 
    void shrink();   
public:    
    T remove ( int r ); 
    int remove ( int lo, int hi );  
};
template <typename T> 
int Vector<T>::remove ( int lo, int hi ) { 
    if(lo==hi) return 0;
    while( hi < _size ) _elem[ lo++ ] = _elem[ hi++ ];
    _size = lo; 
    shrink();
    return hi-lo; 
}
template <typename T> 
T Vector<T>::remove ( int r ) { 
    T e = _elem[r]; 
    remove ( r, r + 1 ); 
    return e; 
}

在主函数中,

Stack<int> S;
for(i = 0; i<n; i++) {
     S.push(i+1);
}
S.pop();

由于基类Vector<T>依赖于模板参数,因此具有在模板实例化之前未知的类型,因此非限定名称查找不会在此处查找。这意味着您对 remove 的非限定调用不会解析为基类成员,而是解析为其他一些重载(可能是这个重载)。

this->removeVector<T>::remove以表明您指的是成员。