我该如何修复这个错误 error"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... */
};
建议使用 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... */
};
start = arr;
应该可以解决问题。
- 您可以将数组分配给一个指针,并将指针设置为数组的开头。
此外,一个空数组规范:
Type arr[];
不确定那是什么意思。可能与:
Type arr[0];
更正常:
Type arr[asz];
当然数组大小需要是一个常量。
这与类似问题不同,因为我将指针值设置为地址,而不是尝试分配不兼容的类型...我认为。
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... */
};
建议使用 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... */
};
start = arr;
应该可以解决问题。
- 您可以将数组分配给一个指针,并将指针设置为数组的开头。
此外,一个空数组规范:
Type arr[];
不确定那是什么意思。可能与:
Type arr[0];
更正常:
Type arr[asz];
当然数组大小需要是一个常量。