2012-04-11 41 views
1

我有以下代碼:C++作用域錯誤

#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 ‘<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文件中。

UPDATE2:

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 
+1

你的意思是要包括'.cpp'文件一起? – GManNickG 2012-04-11 04:43:44

+0

你爲什麼包含cpp文件? – crazyjul 2012-04-11 04:43:51

+2

我們需要查看SortedList.h的內容 – 2012-04-11 04:49:14

回答

2

要包括的.h文件,而不是.cpp文件:

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

沒有更多的代碼,很難推測還有什麼問題。

1

您需要在您的cpp文件中包含定義類SortedList的標題。
在附註中,我不知道爲什麼在程序中包含cpp文件,請不要這樣做。

編輯:要更新的問:
如果您已經包括定義了cpp文件,它爲您提供了錯誤的類SortedList的geader文件,也許你已經在namespace.If包裹類以便,您應該爲該類使用完全限定名稱,或者在主cpp文件中使用類名稱的使用聲明。

+0

我還沒有在這個程序中創建一個名稱空間。我想不出哪個命名空間是合適的 – 2012-04-11 05:07:37

0

我懷疑SortedList已經在一個命名空間中聲明。

例子:

SortedList.h

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

的main.cpp

int main() { 
    MON::SortedList *list = new MON::SortedList(); //This is line 17 
    .... 
} 
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; 
} 
+0

仍然會出現相同的錯誤 – 2012-04-11 05:04:20

+0

@PatMurray:我修改了我的答案。 – 2012-04-11 05:11:07

+0

謝謝。我還更新了上面的錯誤消息。因爲即使您提供給我的代碼,我仍然會遇到錯誤。 – 2012-04-11 05:16:56

1

我懷疑SORTEDLIST_HSortedList.h被處理之前被定義..如果

檢查您所定義的Student.h相同SORTEDLIST_H ...

UPDATE1: 嘗試將這些標誌與你的G ++編譯聲明

g++ -USORTEDLIST_H -USTUDENT_H <fileNames> 
+0

我把Student.h丟失的頂部了。這兩個.h文件完成以上 – 2012-04-11 05:04:55

+0

如果您的評論是真實的,我該如何解決? – 2012-04-11 05:08:53

+0

上面的同一個程序對我來說很有效...... – 2012-04-11 05:27:43