qq_45735316 2020-04-10 10:10 采纳率: 94.1%
浏览 463
已采纳

怎样才能调用string类?

图片说明
main.cpp

#include <iostream>
#include <string>
#include "ClassStudent.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    Student a(1,'mary','m');
    Student::showCount();
    Student b(2,'mary','m');
    Student::showCount();
    return 0;
}

ClassStudent.cpp

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

using namespace std;
    int Student::count=0;
    void Student::showCount()
    {
        cout<<count<<endl;
    }

ClassStudent,h

#include <string>
class Student{
    public:
        Student(int num=0,string name='alex',char sex='m'):num(num),name(name),sex(sex){
            count++;
        }
        static void showCount();
    private:
        int num;
        string name;
        char sex;
        static int count;
};
  • 写回答

2条回答 默认 最新

  • qtchen_1988 2020-04-10 10:40
    关注
    你只在.cpp文件加了
    #include <iostream>
    using namespace std;
    
    
    你应该在ClassStudent.h 头文件添加
    #include <iostream>
    using namespace std;
    其他地方可以不加,因为已经使用了
    #include "ClassStudent.h"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已采纳回答 9月25日

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发