错误C2512:没有适当的默认构造函数可用(不是类) [英] error C2512: no appropriate default constructor available (not classes)

查看:270
本文介绍了错误C2512:没有适当的默认构造函数可用(不是类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始接触的结构,和我有问题,动态地分配我的结构数组。我在做什么,我在我的书,并在互联网上看到的,但我不能得到它的权利。

下面是两个完整的错误消息:

C2512:'记录':没有可用的适当的默认构造函数

智能感知:没有默认构造函数存在类记录

 的#include<&iostream的GT;
#包括LT&;串GT;
使用命名空间std;const int的NG = 4; //分数数结构记录
{
    字符串名称; // 学生姓名
    诠释得分[NG];
    双平均水平;    //计算平均
    //当比分是已知
    记录(的int [],双一)
    {
        双总和= 0;        对于(诠释计数= 0;计数= NG;!计数++)
        {
            得分[计数] = S [统计]
            总和+ =得分[计数]
        }        平均=一;
        平均=总和/ NG;
    }
};诠释的main()
{
    //类的名称
    字符串名称[] = {艾米·亚当斯,鲍勃·巴尔,卡拉·卡尔
                      丹多布斯,埃琳娜·埃文斯};    //根据每个学生考试成绩
    INT考试[] [NG] = {{98,87,93,88},
                        {78,86,82,91},
                        {66,71,85,94},
                        {72,63,77,69},
                        {91,83,76,60}};    记录*房间=新纪录[5];
    返回0;
}


解决方案

该错误是相当清楚的。到时候你试图分配一个数组:

 录制*房间=新纪录[5];

默认构造函数,即记录::记录(),必须实现这样录制的5个实例可以创建:

 结构记录
{
    ...
    记录():平均值(0.0){}
    记录(的int [],双A){...}
};

另外请注意,动态分配是要避免在C尽可能++(除的情况下,当你有很好的理由吧)的东西。在这种情况下,它会更合理地使用的std ::矢量而不是:

 的std ::矢量<记录和GT;记录(5);

I'm starting out with structures, and I'm having problems dynamically allocating my structure array. I'm doing what I see in my book and on the internet, but I can't get it right.

Here's both full error messages:

C2512: 'Record' : no appropriate default constructor available

IntelliSense: no default constructor exists for class "Record"

#include <iostream>
#include <string>
using namespace std;

const int NG = 4; // number of scores

struct Record
{
    string name;  // student name
    int scores[NG];
    double average;

    // Calculate the average
    // when the scores are known
    Record(int s[], double a)
    {
        double sum = 0;

        for(int count = 0; count != NG; count++)
        {
            scores[count] = s[count];
            sum += scores[count];
        }

        average = a;
        average = sum / NG;
    }
};

int main()
{
    // Names of the class
    string names[] = {"Amy Adams", "Bob Barr", "Carla Carr",
                      "Dan Dobbs", "Elena Evans"};

    // exam scores according to each student
    int exams[][NG]= {  {98, 87, 93, 88},
                        {78, 86, 82, 91},
                        {66, 71, 85, 94},
                        {72, 63, 77, 69},
                        {91, 83, 76, 60}};

    Record *room = new Record[5];


    return 0;
}

解决方案

The error is quite clear. By the time you are trying to allocate an array:

Record *room = new Record[5];

a default constructor, i.e. Record::Record(), must be implemented so that 5 instances of Record can be created:

struct Record
{
    ...
    Record() : average(0.0) { }
    Record(int s[], double a) { ... }
};

Also note that dynamic allocation is something you want to avoid as much as possible in C++ (except the situations when you have really good reason for it). In this case it would be more reasonable to use an std::vector instead:

std::vector<Record> records(5);

这篇关于错误C2512:没有适当的默认构造函数可用(不是类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆