C++可以初始化结构的动态阵列

发布于 2025-01-26 06:14:22 字数 678 浏览 1 评论 0原文

struct article {
    int uId;
    char uName[201];

    article(int Id, char* name) { uId = Id;
        for(int i=0;name[i]!='\0';i++)
        {
            uName[i]=name[i];
        }

    }
};

int main()
{
char Name[201];
int ID;
int N;
cin>>N;
article* articleArr = new article[N];  //error thrown on this line
/*Some code that creates ID and Name*/
articleArr[index] = article(Id, Name);
cout<<articleArr[index].uId<<' '<<articleArr[index].uName<<endl;


}

我的代码问题是我无法创建一系列结构。编译器抛出了一个错误“ call to to to to artical :: article()”(文章* articlearr = new Artial [n];)行的错误。在我开始实施结构阵列的动态初始化之前,它效果很好。我必须使用char阵列,不允许使用字符串。

struct article {
    int uId;
    char uName[201];

    article(int Id, char* name) { uId = Id;
        for(int i=0;name[i]!='\0';i++)
        {
            uName[i]=name[i];
        }

    }
};

int main()
{
char Name[201];
int ID;
int N;
cin>>N;
article* articleArr = new article[N];  //error thrown on this line
/*Some code that creates ID and Name*/
articleArr[index] = article(Id, Name);
cout<<articleArr[index].uId<<' '<<articleArr[index].uName<<endl;


}

The problem I have with my code is that I can't dinamically create an array of structs. Compiler throws out an error "no matching function for call to 'article::article()" at the (article* articleArr = new article[N];) line. Before I started implementing dynamic initialization of array of structs it worked fine. I have to use char arrays, not allowed to use strings.

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

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

发布评论

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

评论(2

少年亿悲伤 2025-02-02 06:14:22

这条线

article* articleArr = new article[N];  

试图调用默认构造函数,但您没有一个。

您需要添加

struct article {
    int uId;
    char uName[201];
    article() {  
         /// whatever code is needed to initalize an empty article
    }
    article(int Id, char* name) { uId = Id;
        for(int i=0;name[i]!='\0';i++)
        {
            uName[i]=name[i];
        }

    }
};

C ++通常会自动为YO执行此操作,但是由于您给了构造函数C ++没有创建任何其他

btw-您应该真正使用std :: vector而不是'new''' 它更容易和更安全使用相同的使用是正确的。

,对于std :: String而不是裸体char *

This line

article* articleArr = new article[N];  

Is trying to invoke the default constructor but you do not have one.

You need to add

struct article {
    int uId;
    char uName[201];
    article() {  
         /// whatever code is needed to initalize an empty article
    }
    article(int Id, char* name) { uId = Id;
        for(int i=0;name[i]!='\0';i++)
        {
            uName[i]=name[i];
        }

    }
};

c++ would normally automatically do this for yo , but because you gave a constructor c++ did not create any others

BTW - you should really use std::vector rather that a 'new'ed array, its much easier and safer to use

same is true for std::string instead of naked char *

仙女山的月亮 2025-02-02 06:14:22
 文章(int id,char*名称)
 

您已经声明了接受参数的构造函数。结果,该类将不会具有隐式生成的默认构造函数。由于您也没有定义构造函数,因此类也不是默认的构造。

 新文章[n]`
 

该默认值构造了n文章的实例的数组。这是不正确的,因为该类不是默认的构造。

潜在解决方案:

  • 定义默认构造函数。
  • 或者不要默认类的构造实例。
article(int Id, char* name)

You have declared a constructor that accepts parameters. As a consequence, the class will not have an implicitly generated default constructor. Since you haven't defined a constructor either, the class is not default constructible.

new article[N]`

This default constructs an array of N instances of the class article. This is ill-formed because the class is not default constructible.

Potential solutions:

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