c++ - 当我将代码分离到头文件时出现问题

标签 c++ header-files name-lookup using-directives

这个程序可以正常工作,但是当我将“Class StudentsName”分开并将其放在头文件中时,它无法正常工作。我通过右键单击 header 文件夹并选择新项目和选择 header 在项目中添加 header 文件,但它无法正常工作。请帮助我!

主文件:

#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;
}

头文件(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;
};

错误:

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

最佳答案

using 指令

using namespace std;

放置在包含标题 "Student.h" 之后

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

所以名字string header "Students.h" 中未定义.

将该指令放在包含 header 之前。

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

尽管包含标题 <string> 会好得多和<iostream>在标题 "Students.h"并使用限定名称,如 std::string .

关于c++ - 当我将代码分离到头文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66605230/

相关文章:

c++ - 调试嵌入式 Lua

c++ - 为什么 C++ 编译器(VS2013)选择了错误的函数?

c++ - 不完整类型的调用运算符的 decltype 的特殊行为

c++ - 类中的函数定义需要其他类中的相同函数: compilation error

c++ - 当调用dll函数时,参数对象的成员变量的内存地址发生变化

c++ - 从 Luabind 调用 C++ 成员函数导致 "No matching overload found"

c++ - 如何通过引用访问结构内的结构属性

c - gcc不包含c头文件json-c

c++ - 在 C++ 中,如何在类和函数的 header 中编写完整的实现(可能是模板化的)

c - 当函数声明为传统风格时,为什么输出不正确?