如何修复此错误错误"error C2440: '=' : cannot convert from 'int (*)[]' to 'int *' "?
How do I fix this error error"error C2440: '=' : cannot convert from 'int (*)[]' to 'int *' "?
这与类似的问题不同,因为我正在设置指向地址的指针值,而不是尝试分配不兼容的类型...我认为。
template <class Type>
class ArrayStack
{
private:
int sz; // stack size
int asz; // array size (implementation)
Type* start; // address of first element
Type arr[]; // Might need to intialize each element to 0!?
public:
ArrayStack() { sz = 0; arr[0] = 0; asz = 0; start = &arr; }
/* other code... */
};
start = arr;
应该可以解决问题。
- 您可以将数组分配给指针,并将指针设置为数组的开头。
此外,空数组规范:
Type arr[];
不知道这意味着什么。可能与:
Type arr[0];
更正常:
Type arr[asz];
当然,数组大小需要是一个常量。
建议使用 std::vector<Type> arr
而不是 Type arr[]
。
template <class Type>
class ArrayStack
{
private:
int sz; // stack size
int asz; // array size (implementation)
// Type* start; // address of first element
// Don't need this at all.
// You can use &arr[0] any time you need a pointer to the
// first element.
std::vector<Type> arr;
public:
// Simplified constructor.
ArrayStack() : sz(0), asz(0), arr(1, 0) {}
/* other code... */
};
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- C++错误:"error: int aaa::bbb is protected within this context"
- 错误:从"int"到枚举c++的转换无效
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 我的 int main() 中出现堆栈溢出错误
- std::cout输出int时出现编译错误
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么我会收到"invalid conversion from 'Queue*/Stack*' to 'int'"错误消息?
- 类型"const char[2]"的值不能隐式转换为"int"错误C++
- 如何在<int>错误 C2338 中组织 map<set<multiset>,int> C++
- C++ sendto() 给出"invalid conversion from ‘int (*)(int, int, int)throw ()’ to ‘int’"错误
- 缺少类型说明符-假定为int错误
- 使用int错误进行浮点变量计算
- 数组到数组 -- 将'int*'分配给 'int 错误C++中的不兼容类型
- 为什么我会收到"cannot convert from Dequeu<int> to int"错误?
- int* 转换不允许 int 错误的原因是什么?
- c++将void*强制转换为int错误
- 奇怪的QT无符号Int错误