1

次のコードがあります。

#include <iostream>
#include "Student.h" <-- fixed the problem
#include "SortedList.h" <-- fixed the problem

using namespace std;

int main() {
    // points to the sorted list object
    SortedList *list = new SortedList;   //This is line 17

    // array to hold 100 student objects
    Student create[100];

    int num = 100000;   // holds different ID numbers

    // fills an array with 100 students of various ID numbers
    for (Student &x : create) {
        x = new Student(num);
        num += 100;
    }

    // insert all students into the sorted list
    for (Student &x : create)
        list->insert(&x);

    delete list;
    return 0;
}

そして、コンパイル時エラーが発生し続けます:

main.cpp: In function ‘int main()’:
main.cpp:17: error: ‘SortedList’ was not declared in this scope
main.cpp:17: error: ‘list’ was not declared in this scope
main.cpp:17: error: expected type-specifier before ‘SortedList’
main.cpp:17: error: expected `;' before ‘SortedList’
main.cpp:20: error: ‘Student’ was not declared in this scope
main.cpp:20: error: expected primary-expression before ‘]’ token
main.cpp:20: error: expected `;' before ‘create’
main.cpp:25: error: expected `;' before ‘x’
main.cpp:31: error: expected primary-expression before ‘for’
main.cpp:31: error: expected `;' before ‘for’
main.cpp:31: error: expected primary-expression before ‘for’
main.cpp:31: error: expected `)' before ‘for’
main.cpp:31: error: expected `;' before ‘x’
main.cpp:34: error: type ‘&lt;type error>’ argument given to ‘delete’, expected pointer
main.cpp:35: error: expected primary-expression before ‘return’
main.cpp:35: error: expected `)' before ‘return’

私の Student.cpp と SortedList.cpp ファイルは問題なくコンパイルされます。どちらにも .h ファイルが含まれています。その行でエラーが発生する理由がわかりません。小さな問題に思えますが。任意の洞察をいただければ幸いです。

UPDATE1: もともと .h ファイルが含まれていましたが、エラーの原因を突き止めようとして変更しました。ただし、含まれている.hファイルにはエラーが残ります。

更新 2:

SortedList.h

#ifndef SORTEDLIST_H
#define SORTEDLIST_H

#include "Student.h"

/*
 * SortedList class
 *
 * A SortedList is an ordered collection of Students.  The Students are ordered
 * from lowest numbered student ID to highest numbered student ID.
 */
class SortedList {

  public:

    SortedList();
    // Constructs an empty list.

    SortedList(const SortedList & l);
    // Constructs a copy of the given student object

    ~SortedList();
    // Destructs the sorted list object

    const SortedList & operator=(const SortedList & l);
    // Defines the assignment operator between two sorted list objects

    bool insert(Student *s);
    // If a student with the same ID is not already in the list, inserts 
    // the given student into the list in the appropriate place and returns
    // true.  If there is already a student in the list with the same ID
    // then the list is not changed and false is returned.

    Student *find(int studentID);
    // Searches the list for a student with the given student ID.  If the
    // student is found, it is returned; if it is not found, NULL is returned.

    Student *remove(int studentID);
    // Searches the list for a student with the given student ID.  If the 
    // student is found, the student is removed from the list and returned;
    // if no student is found with the given ID, NULL is returned.
    // Note that the Student is NOT deleted - it is returned - however,
    // the removed list node should be deleted.

    void print() const;
    // Prints out the list of students to standard output.  The students are
    // printed in order of student ID (from smallest to largest), one per line

  private:

    // Since Listnodes will only be used within the SortedList class,
    // we make it private.
    struct Listnode {    
      Student *student;
      Listnode *next;
    };

    Listnode *head; // pointer to first node in the list

    static void freeList(Listnode *L);
    // Traverses throught the linked list and deallocates each node

    static Listnode *copyList(Listnode *L);
    // Returns a pointer to the first node within a particular list
};

#endif

Student.h

#ifndef STUDENT_H
#define STUDENT_H
/*
 * Student class
 *
 * A Student object contains a student ID, the number of credits, and an
 * overall GPA.
 */
class Student {

  public:

    Student();
    // Constructs a default student with an ID of 0, 0 credits, and 0.0 GPA.

    Student(int ID);
    // Constructs a student with the given ID, 0 credits, and 0.0 GPA.

    Student(int ID, int cr, double grPtAv);
    // Constructs a student with the given ID, number of credits, and GPA.\

    Student(const Student & s);
    // Constructs a copy of another student object

    ~Student();
    // Destructs a student object

    const Student & operator=(const Student & rhs);
    // Defines the assignment operator between two student objects

    // Accessors
    int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA

    // Other methods

    void update(char grade, int cr);
    // Updates the total credits and overall GPA to take into account the
    // additions of the given letter grade in a course with the given number
    // of credits.  The update is done by first converting the letter grade
    // into a numeric value (A = 4.0, B = 3.0, etc.).  The new GPA is 
    // calculated using the formula:
    //
    //            (oldGPA * old_total_credits) + (numeric_grade * cr)
    //   newGPA = ---------------------------------------------------
    //                        old_total_credits + cr
    //
    // Finally, the total credits is updated (to old_total_credits + cr)

    void print() const;  
    // Prints out the student to standard output in the format:
    //   ID,credits,GPA
    // Note: the end-of-line is NOT printed after the student information 

  private:
    int studentID;
    int credits;
    double GPA;
};

#endif
4

5 に答える 5

2

あなたの主な機能には多くの問題があるようです。これはより正しいです:

int main() {
    SortedList *list = new SortedList; //1
    Student create[100]; //2
    int num = 100000;

    for (Student &x : create) { //3
        x = Student(num); //4
        num += 100;
    }

    for (Student &x : create) //5
        list->insert(&x);

    delete list;
    return 0;
}
于 2012-04-11T04:56:55.107 に答える
2

.cpp ファイルではなく、.h ファイルを含めたい場合:

#include "Student.h"
#include "SortedList.h"

これ以上のコードがなければ、他に何が問題なのかを推測するのは困難です。

于 2012-04-11T04:44:06.733 に答える
1

SORTEDLIST_H処理される前に定義されていると思わSortedList.hれます..

SORTEDLIST_HStudent.hで同じものを定義しているかどうかを確認してください...

Update1: g++ コンパイル ステートメントと共にこれらのフラグを追加してみてください

   g++ -USORTEDLIST_H -USTUDENT_H <fileNames>
于 2012-04-11T05:01:48.910 に答える
1

SortedListクラスを定義するヘッダーをcpp ファイルに含める必要があります。
余談ですが、プログラムにファイルを含める理由がわかりませcppん。そうしないでください。

編集: 更新された Q に。エラーを与えるファイルにクラスを定義するgeaderファイルが既に含まれている場合は、おそらくクラスを名前空間にラップしています。そうであれば、クラスに完全修飾名を使用する必要があり
ますまたは、メインの cpp ファイルでクラス名に using 宣言を使用します。SortedListcpp

于 2012-04-11T04:44:12.770 に答える
0

SortedList名前空間で宣言されていると思われます。

例:

SortedList.h

namespace MON {
class SortedList { ... };
} // << MON

main.cpp

int main() {
    MON::SortedList *list = new MON::SortedList();  //This is line 17
    ....
}
于 2012-04-11T04:50:02.173 に答える