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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?