-1

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?

FlameDra
  • 1,239
  • 5
  • 20
  • 36
  • "Amongst others" is probably where the true problem lies. Can't see anything obviously wrong with what you've posted (other than omitting header guards and writing getters/setters; neither of those things will cause this problem) – Lightness Races in Orbit Sep 25 '16 at 00:41
  • First, those are compiler errors, not runtime. Otherwise, I pasted and compiled without errors on the MS v140 compiler. Which compiler are you using? Yea, you seem to be including student.h twice in code you are not showing. – lakeweb Sep 25 '16 at 00:45
  • 1
    Voted to close as lacking reproducible example. – Cheers and hth. - Alf Sep 25 '16 at 00:47
  • 1
    @FlameDra: Do consider adding a `#pragma once` at the top of your header. It's evidently included twice somewhere. But not in the code you've shown. – Cheers and hth. - Alf Sep 25 '16 at 00:48
  • Also, a tip: a compilation error or warning typically tells you also the **source file** where the error occurred, and the **line number** in that source file. That is useful information. The cause of the error can be anywhere before that, but usually it pinpoints things. – Cheers and hth. - Alf Sep 25 '16 at 00:50

2 Answers2

0

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.

Community
  • 1
  • 1
iksemyonov
  • 3,923
  • 1
  • 19
  • 37
-1

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