错误:从'int'到'const char*'的转换无效
error: invalid conversion from 'int' to 'const char*'
我正在实现堆栈数据结构。当我在主函数中调用pop
函数时,出现了错误。它像这样说:
-
stack.h:13: error: invalid conversion from 'int' to 'const char*'
-
stack.h:13: error: initializing argument 1 of 'int remove(const char*)
'
问题是我没有使用 char
或 char*
类型参数编写 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->remove
或Vector<T>::remove
以表明您指的是成员。
相关文章:
- C++ PTHREADS - 无效转换无效*(*)()到无效*(*)(无效*)
- is_same和variadic模板编译时错误无效转换
- 如何将常量无效*转换为istream?
- 无效* 转换获得意外输出
- 无法确定为什么函数调用中从 char* 到 char 的无效转换
- 在这个简单函数中从'char'到'const char*'的无效转换
- C++从 'const char*' 到 'char*' Arduino Uno 的无效转换
- 如何修复C++中的"错误:从'int'到'const char*'[-fallowive]的无效转换?
- C 错误:从(基类模板)到子类的无效转换
- 如何将常量无效*转换为无符号字符*
- 函数模板中的无效转换错误,返回值取决于其泛型类型
- 常量正确性编译错误到模板函数中的无效转换错误
- 从"char"到"const char*"的无效转换[-fpermissive](idk why)
- 从“ void*”到“ pthread_t*”的无效转换
- 当函数调用时,试图阻止数组更改时发生无效转换错误
- 使用基类中定义的函数返回指向派生类实例的指针时发生无效转换错误
- C++从字符到常量字符*的无效转换
- ALL_OF从'char'到'const char*'的无效转换 [-允许]
- C和C++上的无效转换错误
- C++ 在模板专用化中从 strcmp() 中的 'char' 到 'const char*' 的无效转换