2017-04-17 9 views
0

私は「think like a programmer」という本からいくつかのエクササイズをしています。 私はクラスの章を始めました。ここでは、私はコードをコンパイルしている間に私の頭を包み込むことができないので固執しているようです。学習C++:エラー:削除された関数の使用

ここにコードがあります。それは私のものではない、私はそれを理解しようとしている本に書いている。

struct studentRecord { 
    int studentId; 
    int grade; 
    string name; 
    studentRecord(int a, int b, string c); 
}; 

class studentCollection { 
    private: 
    struct studentNode { 
     studentRecord studentData; 
     studentNode *next; 
    }; 
    public: 
    studentCollection(); 
    void addRecord(studentRecord newStudent); 
    studentRecord recordWithNumber(int idNum); 
    void removeRecord(int idNum); 
    private: 
    //typedef studentNode *studentList; 
    studentNode *_listHead; 
}; 

studentRecord::studentRecord(int a, int b, string c) { 
    studentId = a; 
    grade = b; 
    name = c; 
} 

studentCollection::studentCollection() { 
    _listHead = NULL; 
} 


void studentCollection::addRecord(studentRecord newStudent) { 
    studentNode *newNode = new studentNode; 
    newNode->studentData = newStudent; 
    newNode->next = _listHead; 
    _listHead = newNode; 
} 

studentRecord studentCollection::recordWithNumber(int idNum) { 
    studentNode *loopPtr = _listHead; 
    while (loopPtr != NULL && loopPtr->studentData.studentId != idNum) { 
     loopPtr = loopPtr->next; 
    } 
    if (loopPtr == NULL) { 
     studentRecord dummyRecord(-1, -1, ""); 
     return dummyRecord; 
    } else { 
     return loopPtr->studentData; 
    } 
} 

int main() { 
    studentCollection s; 
    studentRecord stu3(84, 1152, "Sue"); 
    studentRecord stu2(75, 4875, "Ed"); 
    studentRecord stu1(98, 2938, "Todd"); 
    s.addRecord(stu3); 
    s.addRecord(stu2); 
    s.addRecord(stu1); 
} 

私は取得していますエラーは次のとおりです。

studentclass1.cpp: In member function ‘void studentCollection::addRecord(studentRecord)’: 
studentclass1.cpp:45:32: error: use of deleted function ‘studentCollection::studentNode::studentNode()’ 
    studentNode *newNode = new studentNode; 
           ^~~~~~~~~~~ 
studentclass1.cpp:17:12: note: ‘studentCollection::studentNode::studentNode()’ is implicitly deleted because the default definition would be ill-formed: 
    struct studentNode { 
      ^~~~~~~~~~~ 
studentclass1.cpp:17:12: error: no matching function for call to ‘studentRecord::studentRecord()’ 
+3

をデータメンバstudentRecord studentDataの初期化子を提供し、集計の初期化を使用してエラーを回避することができます生の所有者のポインタを使用して、メモリをリークして、それを投げ捨て、[より良い本を得る](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – nwp

+1

なぜ、他の多くの人がここに投稿しているのですが、実際のコードを投稿するのが難しく、プリプロセッサディレクティブを完成させるのはなぜですか? –

+0

'NULL'の代わりに' nullptr'を使用してください。 –

答えて

4

あなたのようなstructを定義する場合:

struct studentNode { 
    studentRecord studentData; 
    studentNode *next; 
}; 

それは同等です暗黙的に定義されたデフォルトコンストラクタを持っていますユーザーが定義したコンストラクタの存在のためにコンパイラによって

デフォルトのコンストラクタをstudentRecordに追加して、その問題を解決することができます。

struct studentRecord { 
    int studentId; 
    int grade; 
    string name; 
    studentRecord() = default; 
    studentRecord(int a, int b, string c); 
}; 

代わりのコンピュータを使用して= default;指定子でデフォルトコンストラクタを生成し、有効なデータを持つオブジェクトを初期化するほうが良いでしょう。

struct studentRecord { 
    int studentId; 
    int grade; 
    string name; 
    studentRecord() : studentRecord(0, 0, "") {} // Delegate to the other constructor. 
    studentRecord(int a, int b, string c); 
}; 
+0

このようなデフォルトコンストラクタは、 'studentId'と' grade'メンバを初期化しないので、エラーが発生しやすくなります。 – zett42

+1

@ zett42、true。私は答えを更新しました。 –

+0

コンパイラが生成したコンパイラは、実際にはデフォルトで 'studentData'と' next'を初期化しません。 –

2

studentRecordは、ユーザーのコンストラクタ(studentRecord(int a, int b, string c);)を提供しているので、構築可能デフォルトではありません。したがって、studentNodeには、コンパイラ生成のデフォルトのコンストラクタを含めることはできません。あなた自身のためにそれを提供するか、studentRecordにデフォルトのコンストラクタを与えます。 studentRecordのデフォルトコンストラクタが削除されているので問題あり

struct studentNode { 
    studentNode() : studentData(), next() {} 
    studentRecord studentData; 
    studentNode *next; 
}; 

1

この場合、コンパイラは、構造体の既定のコンストラクタを生成しないユーザ定義コンストラクタ

struct studentRecord { 
    int studentId; 
    int grade; 
    string name; 
    studentRecord(int a, int b, string c); 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
}; 

を有するstudentRecord構造。関数内で同時に

は構造studentRecordのデフォルトコンストラクタを使用しようとする試みがある

void studentCollection::addRecord(studentRecord newStudent) { 
    studentNode *newNode = new studentNode; 
          ^^^^^^^^^^^^^^^^ 
    newNode->studentData = newStudent; 
    newNode->next = _listHead; 
    _listHead = newNode; 
} 

をaddRecord。使用できないため、コンパイラは構造体のデフォルトのコンストラクタを定義しましたstudentNodeを削除しました。

あなたは機能を書き込むことができます

この本は本当ににあなたを教えている場合は、明示的に次のように

void studentCollection::addRecord(studentRecord newStudent) { 
    studentNode *newNode = new studentNode { newStudent, _listHead }; 
    _listHead = newNode; 
} 
関連する問題