为什么我可以在这个独特的指针中声明一个新角色数组?

发布于 2025-02-08 17:12:56 字数 898 浏览 2 评论 0原文

我正在尝试为对象制作运行时ID。我已经使用常规指针完成了它,但是我需要通过独特的指针使其使用。为什么我不能在唯一的指针中使用?我明白了

错误e0079期望类型指定符。

在此行:

unique_ptr<char> idPtr(new char[DEVICE_ID_LENGTH]);

这是完整的文件:

#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <utility>


using namespace std;

constexpr auto DEVICE_ID_LENGTH = 10;
class Widget
{

public:
        //constructor
        explicit Widget(int nbr);

        ~Widget();
        
        void getID(char* s, const int len);
        
        std::string WhatIAm; 
        //char* idPtr = new char[DEVICE_ID_LENGTH];  //This needs to be initialized by the constructor


        unique_ptr<char> idPtr(new char[DEVICE_ID_LENGTH]);

        int getSize();

private:
        int widgetSize;
        int widgetNumber;
};

I'm trying to make a runtime id for an object. I have it done using a regular pointer, but I need to make it work with a unique pointer. Why can't I use new in the unique pointer? I get

Error E0079 expected a type specifier.

On this line:

unique_ptr<char> idPtr(new char[DEVICE_ID_LENGTH]);

Here is the full file:

#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <utility>


using namespace std;

constexpr auto DEVICE_ID_LENGTH = 10;
class Widget
{

public:
        //constructor
        explicit Widget(int nbr);

        ~Widget();
        
        void getID(char* s, const int len);
        
        std::string WhatIAm; 
        //char* idPtr = new char[DEVICE_ID_LENGTH];  //This needs to be initialized by the constructor


        unique_ptr<char> idPtr(new char[DEVICE_ID_LENGTH]);

        int getSize();

private:
        int widgetSize;
        int widgetNumber;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-02-15 17:12:56

来自 cppReference 关于默认成员初始化(BOLD PLOASESIS INICER):

非静态数据成员可以以两种方式初始化:

  1. ...
  2. 通过a&nbsp; 默认成员初始化器,它是 brace或equals&nbsp; initializer &nbsp; in ement声明中包括,如果会员从成员中省略该成员,则使用构造函数的初始化列表

请注意,此处不允许使用括号的默认会员初始化。相反,它被解释为成员函数声明,因此预期打开括号参数列表,但您提供new expression,因此您的编译器正确地抱怨参数列表时似乎没有空,它看不到第一个参数的类型说明符。

另请注意,您需要unique_ptr&lt&gh []&gt;,而不是unique_ptr&lt; char&gt; ,以管理由新表达式阵列创建的数组,以便其destructor知道它必须调用delete [],而不是delete,用于持有指针。

因此,要执行默认会员初始化,可以使用,例如,例如,

unique_ptr<char[]> idPtr{new char[DEVICE_ID_LENGTH]};

使用命名空间std; 是不良练习,通常是尤其是 。参见在这里

From cppreference about default member initializers (bold emphasis mine):

Non-static data members may be initialized in one of two ways:

  1. ...
  2. Through a default member initializer, which is a brace or equals initializer included in the member declaration and is used if the member is omitted from the member initializer list of a constructor.

Note that default member initialization using parentheses like in your code is not allowed here. Instead, it is interpreted as member function declaration, thus after opening bracket parameter list is expected, but you provide new-expression, so your compiler correctly complains that while your parameter list doesn't seem empty, it doesn't see type specifier for the first parameter.

Also note that you need unique_ptr<char[]>, not unique_ptr<char>, to manage arrays created by array form of new expression, so that its destructor knows it has to call delete[], not delete, for held pointer.

So, to perform default member initialization like you want, you can use, e.g., braced initializer like this:

unique_ptr<char[]> idPtr{new char[DEVICE_ID_LENGTH]};

P.S. using namespace std; is bad practice usually, especially in header files. See here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文