2

this program works correctly but when I separate that "Class StudentsName" and put it on the header file it doesn't work correctly. I add the header file in my project by right click on the header folder and choosing the new item and the choosing header, but it doesn't work correctly .please help me!

main file:

#include "iostream"
#include "string"
#include "Students.h"
using namespace std;

int main()
{
    string nameOfStudent;
    StudentsName myStudentsName(" The student name is: Jason");
    cout<<myStudentsName.getMyName()<<endl;

    cout<<"please enter the name of the student: "<<endl;
    getline(cin, nameOfStudent);
    myStudentsName.setMyName(nameOfStudent);
    cout<<endl;

    myStudentsName.displayMyName();

    system("pause");
    return 0;
}

header file (Students.h):

class StudentsName
{
public:
    StudentsName (string  stdName)
    {
        setMyName(stdName);
    }
    void setMyName(string stdName)
    {
        myName=stdName;
    }
    string getMyName ()
    {
        return myName;
    }
    void displayMyName()
    {
        cout<<"The student name is: "<<getMyName()<<endl;
    }

private:
     string myName;
};

error:

students.h(5): error C2061: syntax error : identifier 'string'
students.h(9): error C2061: syntax error : identifier 'string'
students.h(13): error C2146: syntax error : missing ';' before identifier 'getMyName'
students.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
students.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
students.h(16): warning C4183: 'getMyName': missing return type; assumed to be a member function returning 'int'
students.h(23): error C2146: syntax error : missing ';' before identifier 'myName'
students.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
students.h(7): error C2065: 'stdName' : undeclared identifier
students.h(11): error C2065: 'myName' : undeclared identifier
students.h(11): error C2065: 'stdName' : undeclared identifier
students.h(15): error C2065: 'myName' : undeclared identifier
students.h(19): error C2065: 'cout' : undeclared identifier
students.h(19): error C2065: 'endl' : undeclared identifier
studentname.cpp(11): error C2664: 'StudentsName::StudentsName(const StudentsName &)' : cannot convert parameter 1 from 'const char [84]' to 'const StudentsName &'
1>          Reason: cannot convert from 'const char [84]' to 'const StudentsName'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
studentname.cpp(16): error C2660: 'StudentsName::setMyName' : function does not take 1 arguments
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
asipoolika
  • 41
  • 3

1 Answers1

2

The using directive

using namespace std;

is placed after including the header "Student.h"

#include "iostream"
#include "string"
#include "Students.h"
using namespace std;

So the name string is undefined within the header "Students.h".

Place the directive before the inclusion of the header.

#include "iostream"
#include "string"
using namespace std;
#include "Students.h"

Though it would be much better to include the headers <string> and <iostream> in the header "Students.h" and use qualified names like std::string.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Is there really no better way? Like, including the headers where they are actually used? This just makes the header nonportable – IWonderWhatThisAPIDoes Mar 12 '21 at 18:27
  • 2
    Wouldn't it be better to provide an answer that doesn't encourage this practice? The header file doesn't include what it uses and that's really the problem. – sweenish Mar 12 '21 at 18:27
  • @sweenish As you have said "this practice" is used in fact in all books for beginners. So while he is reading such a book it is enough to point to where the using directive must be placed. – Vlad from Moscow Mar 12 '21 at 18:29
  • At a minimum, I'd leave it unaddressed and fix the actual problem of just including `` in the header. And are you going through my history or something? I'm sure I've said that, but it also doesn't equate to me advocating it, certainly not now. – sweenish Mar 12 '21 at 18:57
  • @Vlad I hope you don't mind I dupe hammerd this question anyways. – πάντα ῥεῖ Mar 12 '21 at 19:04
  • @πάνταῥεῖ It does not matter. – Vlad from Moscow Mar 12 '21 at 19:16