简体   繁体   中英

Getting class type redefinition and a few other errors

I'm creating a student data management console application for a project. I created a class called Student which is storing all the data that a student needs to have, and it also has all the getters and setters associated with it. Here is how all my files are laid out:

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;
}

When I try to run my program, I get, amongst others, the following errors:

Error 1 error C2011: 'Student' : 'class' type redefinition

Error 2 error C2079: 'student1' uses undefined class 'Student'

Error 5 error C2228: left of '.set_name' must have class/struct/union

Error 9 error C2027: use of undefined type 'Student'

How can I go about fixing this issue?

I'm quite sure this is an error caused by the fact that student.h is included twice in a certain .cpp file. Thus you need to use so-called header guards to make sure the file is only included once in every .cpp file:

#ifndef STUDENT_H
#define STUDENT_H

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

class Student {

/* ... */

};

#endif

The idea behind this is that an #include is a preprocessor directive that results in the argument file being copied into the file where the #include was issued. Hence, if files A and B include Student.h , and file C includes both files A and B, then the declaration of class Student is going to end up duplicated. Hence the error. The above macros make sure that this doesn't happen.

Edit as per the question author's comment:

#pragma once is the same as #ifndef .. #define #endif but non-standard .

See #pragma once vs include guards? for reference.

I had the same error. I just clean and rebuild the solution and error resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM