0

my question is how can i put classes in different files and make the program work?I get error when i try to build and run it at the Student.h file ---> string does not name a type

main.cpp :

#include <iostream>
#include "Student.h"

using namespace std;

int main()
{
    Student *a;

    a = new Student(1,"Astudent");
    a->printStudent();

    system("PAUSE");
    return 0;
}

Student.h:

#ifndef STUDENT_H
#define STUDENT_H

class Student
{
    private:
        int id;
        string name;

    public:
        Student(int id,string name);
        void printStudent();
};

#endif

Student.cpp:

#include <iostream>
#include "Student.h"

using namespace std;

Student::Student(int id,string name)
{
    this->id = id;
    this->name = name;
}

Student::printStudent()
{
    cout << id << "." << name << endl;
}
Jaspar
  • 23
  • 2

1 Answers1

1

Student.h needs to include string to be able to use string as a type. e.g.:

#include <string>
tdk001
  • 1,014
  • 1
  • 9
  • 16
  • 1
    Thank you , it was tha simple , i also needed to type ---> void Student::printStudent() in the .cpp file and not ---> Student::printStudent() – Jaspar May 07 '18 at 20:05
  • Not the most elegant solution, but quite acceptable. I much prefer declaring it as `std::string name` or, even though it's not a good practice, put `using namespace std` inside the `.h` – ionizer May 07 '18 at 20:07