Yousif Dafalla
Yousif Dafalla

Reputation: 15

String type in header files

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'

Upvotes: 0

Views: 1621

Answers (2)

Neb
Neb

Reputation: 2280

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.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234635

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.

Upvotes: 1

Related Questions