2016-09-25 7 views
-1

私はプロジェクト用の学生データ管理コンソールアプリケーションを作成しています。私は学生が必要とするすべてのデータを格納しているStudentというクラスを作成しました。また、getterとsetterもすべて関連付けられています。ここに私のすべてのファイルがレイアウトされている方法です。クラスタイプの再定義といくつかのエラーの取得

Student.h

#include <iostream> 
#include <string> 
using namespace std; 


class Student { 

private: 
    string name; 
    string id; 
    string email; 

    int presentation; 
    int essay1; 
    int essay2; 
    int project; 

public: 
    //constructor 
    //Student(); 
    //setters 
    void set_name(string); 
    void set_id(string); 
    void set_email(string); 
    void set_presentation(int); 
    void set_essay1(int); 
    void set_essay2(int); 
    void set_project(int); 
    //getters 
    string get_name(); 
    string get_id(); 
    string get_email(); 
    int get_presentation(); 
    int get_essay1(); 
    int get_essay2(); 
    int get_project(); 
}; 

Student.cpp

#include <iostream> 
#include <string> 
#include "Student.h" 
using namespace std; 


//constructor definition 
/* 
Student::Student(void) { 
    cout << "Student created" << endl; 
} 
*/ 

//setter definition 
void Student::set_name(string s) { 
    name = s; 
} 

void Student::set_id(string s) { 
    id = s; 
} 

void Student::set_email(string s) { 
    email = s; 
} 

void Student::set_presentation(int a) { 
    presentation = a; 
} 

void Student::set_essay1(int a) { 
    essay1 = a; 
} 

void Student::set_essay2(int a) { 
    essay2 = a; 
} 

void Student::set_project(int a) { 
    project = a; 
} 

//getter definition 
string Student::get_name() { 
    return name; 
} 

string Student::get_id() { 
    return id; 
} 

string Student::get_email() { 
    return email; 
} 

int Student::get_presentation() { 
    return presentation; 
} 

int Student::get_essay1() { 
    return essay1; 
} 

int Student::get_essay2() { 
    return essay2; 
} 

int Student::get_project() { 
    return project; 
} 

MAIN.CPPを

#include <iostream> 
#include <string> 
#include "Student.h" 
using namespace std; 


int main() { 

    cout << "Hello World!" << endl; 

    Student student1; 
    Student student2; 
    Student student3; 

    student1.set_name("John"); 
    student2.set_name("Bob"); 
    student3.set_name("Carl"); 


    return 0; 
} 

私は私を実行しようとする

プログラムでは、次のようなエラーが発生します。

エラー1つのエラーC2011: '学生': 'クラス' タイプの再定義

エラー2エラーC2079: 'STUDENT1は' 未定義のクラスを使用する「スチューデント

エラー5エラーC2228:.set_name「の左側「

クラス/構造体/共用

を持っている必要がありますエラー9エラーC2027:未定義のタイプを使用する「スチューデント

どのように私はこの問題を修正については行くことができますか?

+0

「他のものの中に」はおそらく真の問題がある場所です。ヘッダガードを省略してゲッターやセッターを書く以外に、明らかに間違ったものは何も表示されません。 –

+0

まずは実行時ではなくコンパイラエラーです。それ以外の場合は、MS v140コンパイラでエラーなく貼り付けてコンパイルしました。どのコンパイラを使用していますか?そうですね、表示されていないコードでstudent.hを2回インクルードしているようです。 – lakeweb

+0

[MCVE]を生成します。 –

答えて

0

これは、特定の.cppファイルにstudent.hが2回含まれていることが原因で発生したエラーです。したがって、あなたは必ずファイルのみすべての.cppファイルに一度に含まされていることを確認するために、いわゆるヘッダのガードを使用する必要があります。

#ifndef STUDENT_H 
#define STUDENT_H 

#include <iostream> 
#include <string> 
using namespace std; 

class Student { 

/* ... */ 

}; 

#endif 

この背後にある考え方は#includeがいる引数ファイルになりプリプロセッサディレクティブであるということです#includeが発行されたファイルにコピーされます。したがって、ファイルAとBがStudent.hを含み、ファイルCがファイルAとBの両方を含む場合、class Studentの宣言は重複して終了することになります。したがって、エラー。上記のマクロは、これが起こらないことを確認します。質問の作者のコメントどおり

編集:

#pragma once#ifndef .. #define #endifが、非標準のと同じです。

参照のため#pragma once vs include guards?を参照してください。

+0

説明なしのコードダンプ=悪い回答。私はあなたのコードダンプの何も問題を解決しないので、あなたが説明を見つけることができなかったと思いますか? :) –

+0

_ "student.hヘッダーが2回含まれているため" _そうではありません。 –

+0

です。一度student.cppに入ったら、main.cppに一度入ります。 – iksemyonov

関連する問題