c++ - 不同文件的类,如何让它工作?

标签 c++

<分区>

我的问题是如何将类放在不同的文件中并使程序运行?当我尝试在 Student.h 文件中构建和运行它时出现错误 ---> string does not name a type

main.cpp :

#include <iostream>
#include "Student.h"

using namespace std;

int main()
{
    Student *a;

    a = new Student(1,"Astudent");
    a->printStudent();

    system("PAUSE");
    return 0;
}

学生.h:

#ifndef STUDENT_H
#define STUDENT_H

class Student
{
    private:
        int id;
        string name;

    public:
        Student(int id,string name);
        void printStudent();
};

#endif

学生.cpp:

#include <iostream>
#include "Student.h"

using namespace std;

Student::Student(int id,string name)
{
    this->id = id;
    this->name = name;
}

Student::printStudent()
{
    cout << id << "." << name << endl;
}

最佳答案

Student.h 需要包含字符串才能将 string 用作类型。例如:

#include <string>

关于c++ - 不同文件的类,如何让它工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221547/

相关文章:

c++ - cl.h 是否适用于 C++?

c++ - 指针可以放在堆内存上吗(C++)?

c++ - 如何解释VtkCamera viewTransformMatrix

c++ - 像 C++ 这样的 Delphi 模板?

关于 pow 函数的 C++ 问题

c++ - 另一种二叉搜索树的节点公式

c++ - 根据不同函数排序的 std::vectors 的交集

c++ - “未在此范围内声明”错误

c# - UDP 套接字 : recvfrom() returns -1 (SOCKET_ERROR)

c++ - 如何在 Windows 上修复 VS 2019 中的 SFML 错误?