C++编译问题:从堆中创建结构体数组

huangapple go评论92阅读模式
英文:

C++ Compilation problem with creating an array of struct from the heap

问题

以下是翻译好的部分:

  1. 构造函数:
  1. MyClass(int xx, int yy) : x(xx), y(yy)
  2. {
  3. m_BufferPtr = new struct BufferCustom[CONST_BUFFER_SIZE];
  4. }

编译错误:分配不完整类型 struct BufferCustom 的内存。

  1. 析构函数中的警告:
  1. if (m_BufferPtr){
  2. delete [] m_BufferPtr;
  3. m_BufferPtr = nullptr;
  4. }

警告:删除不完整类型 'struct BufferCustom' 的指针可能导致未定义行为。

我对为什么会出现这个错误感到困惑!

英文:

I'm getting a compilation error and warning when I compile this.

  1. #include <iostream>
  2. struct {
  3. uint8_t m_data;
  4. int size;
  5. } BufferCustom;
  6. class MyClass
  7. {
  8. private:
  9. int x, y;
  10. const int CONST_BUFFER_SIZE = 10;
  11. struct BufferCustom *m_BufferPtr;
  12. public:
  13. MyClass(int xx, int yy) : x(xx), y(yy)
  14. {
  15. m_BufferPtr = new struct BufferCustom[CONST_BUFFER_SIZE];
  16. }
  17. virtual ~MyClass() {
  18. if (m_BufferPtr){
  19. delete [] m_BufferPtr;
  20. m_BufferPtr = nullptr;
  21. }
  22. }
  23. // user defined copy constructor
  24. MyClass(const MyClass& rhs)
  25. : x{ rhs.x }, y{ rhs.y } // initialize members with other object's // members
  26. {
  27. std::cout << "User defined copy constructor invoked.";
  28. //m_BufferPtr = new struct B
  29. }
  30. MyClass& operator=(const MyClass &rhs)
  31. {
  32. std::cout << "Assignment operator called\n";
  33. return *this;
  34. }
  35. };
  36. int main()
  37. {
  38. MyClass o1{ 1, 2 };
  39. MyClass o2 = o1; // user defined copy constructor invoked
  40. o2 = o1;
  41. }

The error(s) are in these lines:

  1. Ctor
  1. MyClass(int xx, int yy) : x(xx), y(yy)
  2. {
  3. m_BufferPtr = new struct BufferCustom[CONST_BUFFER_SIZE];
  4. }

compilation error being Allocation of incomplete type struct BufferCustom

  1. The second warning is in the dtor
  1. if (m_BufferPtr){
  2. delete [] m_BufferPtr;
  3. m_BufferPtr = nullptr;
  4. }

saying that Deleting pointer to incomplete type 'struct BufferCustom' may cause undefined behavior

I am perplexed as to why this is an error!

答案1

得分: 6

这段代码:

  1. struct {
  2. uint8_t m_data;
  3. int size;
  4. } BufferCustom;

...定义了一个具有匿名类型的结构体,然后定义了一个名为BufferCustom的该类型的实例。所以BufferCustom是一个没有名称的类型的单个变量。

但是您后来的代码尝试将BufferCustom用作类型的名称。我猜您的本意可能是这样:

  1. struct BufferCustom {
  2. uint8_t m_data;
  3. int size;
  4. };

这将BufferCustom定义为类型的名称,因此您可以随后分配类型为BufferCustom的对象。

英文:

This:

  1. struct {
  2. uint8_t m_data;
  3. int size;
  4. } BufferCustom;

...defines a struct with an anonymous type, then defines one instance of that type named BufferCustom. So BufferCustom is a single variable of a type that doesn't have a name.

But your later code attempts to use BufferCustom as if it were the name of a type. I'd guess what you intended was something like this instead:

  1. struct BufferCustom {
  2. uint8_t m_data;
  3. int size;
  4. };

This defines BufferCustom as the name of the type, so you can later allocate objects of type BufferCustom.

huangapple
  • 本文由 发表于 2023年5月28日 11:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定