Reputation: 81
I am having trouble using string variables inside of a class in c++. I have tried using include but that doesn't appear to have any noticeable effect. Here are the contents of the header file the class is declared in:
#include <string.h>
#ifndef STUDENT_H
#define STUDENT_H
class Student
{
private:
int iStudentAge;
int iStudentBirthDay;
int iStudentBirthMonth;
int iStudentBirthYear;
string sStudentName;
string sStudentCourse;
public:
Student();
Student(int iValue1, int iValue2, int iValue3, int iValue4, string sValue5, string sValue6);
void setAge(int iNewValue);
int getAge();
void setBirthDay(int iNewValue);
int getBirthDay();
void setBirthMonth(int iNewValue);
int getBirthMonth();
void setBirthYear(int iNewValue);
int getBirthYear();
void setName(string iNewValue);
string getName();
void setCourse(string iNewValue);
string getCourse();
};
#endif // !STUDENT_H
The specific errors I am having are all on the lines that involve strings, except for the include line.
Upvotes: 0
Views: 428
Reputation: 23832
For C++ you should use the according library #include <string>
not #include <string.h>
witch is a C library.
For variable declaration use scope
std::string sStudentName;
or
using namespace std;
before the class declaration for string
to be recognized as a type.
The first option is recommended, to know why see Why is “using namespace std;” considered bad practice?
Upvotes: 3
Reputation: 1057
As a compromise between littering "std::" all over the file, and using the potentially dangerous "using namespace std", you can write:
using std::string;
Upvotes: 1
Reputation: 409442
<string.h>
is the C header file for string handling null-terminated byte strings. std::string
is defined in the header file <string>
.
So first change your include to the correct file, then you need to use the std::
scope prefix for all strings.
Upvotes: 6