带有类型指针的C++函数调用,参数混淆:不允许使用不完整的类型
C++ function call with type pointer, parameter confusion: incomplete type is not allowed
我正试着用C++来理解。我只给你一些小片段来帮助说明这个想法,而不会让事情变得复杂。顺便说一句,我只实现了这些方法,我不能更改设置或参数。
我有一个用于动态数组数据结构的类,它包含称为股票的对象:
typedef class Stock ArrayType;
class DynamicArray {
ArrayType** items;
int numberOfElements;
...
}
这是它的构造函数。我应该分配数组并添加一个项,然后设置元素的数量。
DynamicArray::DynamicArray(ArrayType* const item){
Stock *items = NULL; // ... i guess? pointers to pointers confuse me
// now im guessing i need to create a actual stock array and point the above pointer to it
items = new Stock[1]; // ERROR: incomplete type is not allowed? I've tried several things, and cant get rid of the red squiggles
this->numberOfElements = 1;
}
好的,有一些问题。首先,你必须包括Stock。编译器需要Stock
的完整定义才能编译DynamicArray
,因为我猜测内存分配的原因。
其次,您希望items
成员值包含对在构造函数中创建的数组的引用。因此,与其在构造函数中定义Stock *items[1]
,不如将new
语句的值直接分配给this->items
;只要不在正在处理的任何函数中定义同名变量,就可以提交this->
。
最后,您将分配一个指针数组,因此使用以下语法:new ArrayType*[1]
此外,正如编码实践所指出的,您不应该在同一个源中混合使用typedef及其原始类型。所以我建议您在整个过程中使用ArrayType
,或者根本不使用。
相关文章:
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- C++从外部类继承的嵌套类;不允许使用不完整的类型
- E0322:不允许使用抽象类类型 " " 的对象
- 为什么C++不允许两个同名的函数/类模板,区别仅在于非类型模板参数(整型)的类型?
- 不允许将SDL_Cursor与unique_ptr:error不完整类型一起使用
- HTTP POST 请求不允许我定义上下文类型
- 内建数组出现不允许的类型不完整错误
- 为什么 std::array 的演绎指南不允许不同的类型?
- 为什么标准不允许通过引用捕获不完整的异常类型?
- 数组错误(不允许类型不完整)
- 为什么函数返回类型中不允许参数推导?
- std::function 中不允许引用返回类型吗?
- 类中不允许使用不完整的类型,但在类模板中允许使用不完整的类型
- 为什么协程不允许使用占位符返回类型?
- 错误消息:不允许抽象类类型 "X" 的对象:纯虚拟"Y"没有覆盖器
- 错误:不允许使用抽象类类型的对象
- 奇怪的不完整类型不允许错误
- 不完整的类型不允许错误
- c++不完全类型不允许在类内部使用
- 不完全类型不允许出现错误和元组