-2

明日までコードはうまく機能しますが、奇妙なエラーが発生することを知ってください..デフォルトのコンストラクターがありません..
私はこのエラーを本当に理解していません..さらに、これはこのタイプのエラーの最初の経験です..質問を検索しましたが、コンストラクターに関する議論は上級レベルです..私は中級者です,,..私のコードをチェックするのを手伝ってください..!!!

// error.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

struct Student
{
    const char name[6][11];
    const int id[5];
};


void fetch_id(Student& s, const int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << "roll no of student: " << i+1 << endl;;
        cin >> s.id[i];
    }

}
void fetch_name(Student& s, const int size)
{

        for (int j = 0; j < size; j++)
        {

            cin.getline(s.name[j], 10);
            cout <<"name of student: " << j+1 << endl;  
        }

}

void display_name(Student s, const int size)
{
    cout << "Student Names Are" << endl;
    for (int i = 0; i < size; i++)
    {           
        if ( s.name[i] != '\0' )
        cout << s.name[i] << endl;
    }
}

void display_id(Student s, const int size)
{
    cout << "Roll Numbers Are" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << s.id[i] << " || ";
    }
}

int main()
{

    const int size = 5;
    Student s; //  error C2512: 'Student' : no appropriate default constructor available ??
    fetch_id(s, size);
    display_id(s, size);
    cout << '\n';
    fetch_name(s, size);
    cout << '\n';
    display_name(s, size);
    system("Pause");
    return 0;
}
4

2 に答える 2

5

これは、構造に定数配列が含まれているためです。それらは、コンストラクター初期化子リストで明示的に初期化する必要があります。

これらの定数メンバー変数のため、コンパイラはデフォルトのコンストラクターを生成できません。自分で作成する必要があります。

実際、コードの後半で配列に代入しようとすると、誤って配列を定数にしていると思いますが、これは定数であるため実行できません。

メンバー配列宣言の一部を削除するconstと、すべてがうまく機能するはずです。

于 2013-04-09T12:02:39.733 に答える
0

いくつかの簡単な質問...なぜ2次元のchar配列を使用しているのstd::stringですか?代わりに使用しないのはなぜですか? cin問題は、入力の長さを制御できないため、すぐにバッファオーバーフローが発生することです。スーロングザップ

于 2013-04-09T12:13:24.383 に答える