0

I am working on a project and i have three header files each defining a seperate struct with some function for accessing the variables this is an example of the student struct:

#include<string.h>
//using namespace std;
struct student
{
    int studentId;
    string fname;
    string lname;
};
void SetId(student * stu,int id)
{
    stu->studentId=id;
}
void SetFirstName(student * stu,string name)
{
    stu->fname=name;    
}
void SetLastName(student * stu,string name)
{
    stu->lname=name;
}
int GetId(student * stu)
{
    return stu->studentId;
}
string GetFirstName(student * stu)
{
    return stu->fname;
}
string GetLastName(student * stu)
{
    return stu->lname;
}

when i compile this file i get two errors: 1. [Error] unknown type name 'string' 2. [Error] unknown type name 'student'

  • 1
    `#include` includes a header of the C library that should be included as `` in C++. –  Sep 09 '17 at 14:26

2 Answers2

1

Replace string with std::string.

You've done the good thing and got rid of the intrusive using namespace std;.

Finally, why not make the "global functions" members of the student class itself? Then you wouldn't need that student* pointer. C++ ain't C you know.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Since you are using C++, you should avoid to include .h header. There's the <string> header in C++ to manipulate string, so use it.

Then, you have commented the using namespace std which is a good practice. See here why. But now you need to specify which namespace string objects belong to, so it's necessary to explicity write std::string istead of string.

Finally, I quote @Bathsheba answer. You should create a class student.

Neb
  • 2,270
  • 1
  • 12
  • 22