1

私はばかげて奇妙な問題を抱えています。以下のプログラムを実行しようとすると、「エラー C2512: 'Record' : 適切な既定のコンストラクターがありません」というエラー メッセージが表示されます。それをダブルクリックすると、「xmemory0」という名前のプリコンパイル済み読み取り専用ヘッダー ファイルが表示されます。彼らは私が読み取り専用ファイルを変更することを期待していますか? ファイル内のコードのセグメントは次のとおりです。

void construct(_Ty *_Ptr)
{   // default construct object at _Ptr 
    ::new ((void *)_Ptr) _Ty();  // directs me to this line
}

プログラムは次のとおりです。

#include <iostream>
#include <vector>
#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}};

    vector<Record> records(5);


    return 0;
}
4

1 に答える 1

2

Record のデフォルトのコンストラクターはなく、必要vector<Record> records(5)です。Visual Studio は、そのテンプレート エラー メッセージが役に立たないことで有名です。[エラー] タブではなく [出力] タブに移動すると、エラー メッセージのカスケードが表示されます。最初に見つかったメッセージが表示され、最後にエラー メッセージのvector行が示されます。

エラー メッセージをクリックして [出力] タブに切り替えた場合は、長いエラー メッセージを下にスクロールして、独自のコードへの参照に到達し、修正方法を見つけます。

これが完全なエラーメッセージです。私のソースの44行目はあなたのvector行です:

D:\dev12\VC\INCLUDE\xmemory0(588) : error C2512: 'Record' : no appropriate default constructor available
        D:\dev12\VC\INCLUDE\xmemory0(587) : while compiling class template member function 'void std::allocator<_Ty>::c
nstruct(_Ty *)'
        with
        [
            _Ty=Record
        ]
        D:\dev12\VC\INCLUDE\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>:
construct(_Ty *)' being compiled
        with
        [
            _Ty=Record
        ]
        D:\dev12\VC\INCLUDE\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' bein
 compiled
        with
        [
            _Ty=Record
        ]
        D:\dev12\VC\INCLUDE\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being c
mpiled
        with
        [
            _Alloc=std::allocator<Record>
        ]
        z.cpp(44) : see reference to class template instantiation 'std::vector<Record,std::allocator<_Ty>>' being compi
ed
        with
        [
            _Ty=Record
        ]
于 2013-11-08T03:53:29.750 に答える