0

これは本当にイライラします。クラスとコンストラクターの複数のバリエーションを試し、Googleの結果のページとこのサイトの他の質問をふるいにかけましたが、わかりません. エラーは私が見落としている単純なものだと思います。

エラーコード:

1>main.cpp(60): error C2065: 'Student' : undeclared identifier
1>main.cpp(60): error C2146: syntax error : missing ';' before identifier 'newStudent'
1>main.cpp(60): error C3861: 'newStudent': identifier not found

生徒.h

#ifndef STUDENT_H
#define STUDENT_H
class Student {  
    private:
    static const int SIZE = 7;         // initial size of the array
    int *Students;                     // Students will point to the dynamically allocated array
    //int numStudents;                   // number of students currently in the list
    int rosterSize;                    // the current size of the roster
    int student_id;
    string name;
    string classification;
    public:
    Student();                         // constructor; initialize the list to be empty
    ~Student();
        void set_id(int);
        void set_name(string);
        void set_class(string);
            int print_id();
            string print_name();
            string print_class();
    //void enrollStudent();              
    //void Print(ostream &output) const; // print the list to output
};
#endif

学生.cpp

        #include <iostream>
#include <string>

#include "student.h"

#define PROMPT "class> "
using namespace std;

Student::Student(){    // declared here right?
    student_id = 0;
    name = "";
    classification = "";
}

Student::~Student(){
    //delete Student
}

void Student::set_id( int i ){
    student_id = i;
}

void Student::set_name( string n ){
    name = n;
}

void Student::set_class( string c ){
    classification = c;
}

int Student::print_id(){
    return student_id;
}

string Student::print_name(){
    return name;
}

string Student::print_class(){
    return classification;
}

main.cpp

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

#define PROMPT "class> "
using namespace std;


//**** Implement Error Handling ****\\

enum errorType {
    UNKNOWN_ERROR,
    INPUT_ERROR,
    HANDLER,
    NUM_ERRORS
};

// error messages

string errorMessage[NUM_ERRORS]= {
    "Unknown Error\n",
    "Input Error\n",
};

// error handler

void handleError(errorType err) {
    if(err > 0 && err < NUM_ERRORS)
        cout<< "Error: "<< errorMessage[err];
    else cout<< "Error: "<< errorMessage[UNKNOWN_ERROR];
}

//**** END Error Handling ****\\



void getInput() {


    int id; string n, c;
    cin>>id>>n>>c; 
    //cout<<id<<"-"<<n<<"-"<<c<<endl;
    Student newStudent();      //****WORK DAMN U!
    //set_id(id);
    //print_id();

    return;
}


int main() {
    //Student newStudent();   /* <-- why doesn't this work?!*/
    string input = "";
    bool finished = false;

    cout<<PROMPT; // prompt the user
    while(!finished) {
        if(input!="") cout<<PROMPT;
        cin>>input;
        if(input=="enroll") {
            cout<<PROMPT<<"Enroll student:"<<endl;
            getInput();
        }
        else if(input=="drop") {
            cout<<PROMPT<<"Enter ID:"<<endl;
        }
        else if(input=="roster") {
            cout<<"This will print formatted list of students"<<endl;
        }
        else if(input=="quit") {
            finished=true;
        }
        else handleError(errorType(1));
    }
}

メモとして、私の教授は #include student.h
を main.cpp に追加すると問題が解決すると言いました。明らかに、私はそれを私のstudent.cppに含めたので、そうではありません。ಠ_ಠ</p>

tl;dr ここでは、同じエラーが発生する小さなベアボーン バージョンを示します。

#include<iostream>
#include<string>
#define PROMPT "class> "

using namespace std;

class Student {
private:
int student_id;
string name;
string classification;
public:
Student();    // constructor
~Student();
void set_id(int);
int print_id();
};

Student::Student(){
    student_id = 0;
    name = "";
    classification = "";
}

Student::~Student(){
//delete Student
}

void Student::set_id( int i ){
    student_id = i;
}


int Student::print_id(){
    return student_id;
}


void messenger(){
int id;

   Student newStudent();

   cin>>id;      //this will drop the name and class
   newStudent.set_id(id);
   newStudent.print_id();
return;
}

void main() {
//Student newStudent();
string input="";
bool finished=false;

cout<<PROMPT; // prompt the user
        while(!finished) {
                if(input!="") cout<<PROMPT;
                cin>>input;
                if(input=="enroll") {
                        cout<<PROMPT<<"Enroll student:"<<endl;
                        messenger();
                        //cin>>input;
                        //newStudent.enroll(input );
                }
                else if(input=="drop") {
                        cout<<PROMPT<<"Enter ID:"<<endl;
                }
                else if(input=="roster") {
                        cout<<"This will print formatted list of students"<<endl;
                }
                else if(input=="quit") {
                        finished=true;
                }
        }
}
4

2 に答える 2

5

かっこなしで Student オブジェクトを初期化しようとしましたか?

Student newStudent;

これにより、デフォルトのコンストラクターが呼び出されます。あなたがしたこと

Student newStudent();

パラメータを取らず、Student オブジェクトを返す関数を宣言することです。

于 2012-06-21T06:53:31.847 に答える
4

メモとして、私の教授は、私に追加#include student.h
するmain.cppと問題が解決すると言いました。明らかにそうではありませんstudent.cpp

オブジェクトを作成できるようにするには、型が何でmain()あるかを知る必要があります。 だからあなたの教授は正しいです。Student

各ソース ファイルは個別にコンパイルされるため、コンパイラがコンパイルするとき、型の定義を確認main.cppする必要があります。型を定義するヘッダーを に含める場合にのみ、これを行うことができます。ヘッダーをインクルードしても、定義を表示する必要があるという事実には関係がないことに注意してください。これは、両方が別々のファイルとしてコンパイルされるためです。Studentstudent.main.cppstudent.cppmain.cpp

于 2012-06-21T06:49:33.053 に答える