长庚之上 2024-03-14 16:26 采纳率: 50%
浏览 4
已结题

c++类使用函数使用总报错

img


不知道咋回事,求解;还有我之前类封装时,它说string不能声明name等等,求解答

img

img

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-03-14 16:27
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的错误信息和代码片段,存在几个问题。我将逐一分析并给出相应的解决方案。

    1. 缺少头文件引用:在使用string类型时出现错误,提示“string does not name a type”,这表明在.cpp.h文件中未包含必要的#include <string>。请确保在每个需要使用string的源文件开头添加此行。
    #include <string>
    
    1. 函数声明与实现不匹配

      • student.h中的函数声明没有返回值,例如stu9(),而在实现中它有返回类型int。请确保函数声明与实现一致。
      // student.h
      int stu9(manageSystem *p);
      
    2. 语法错误

      • main.cpp中的switch case语句里,您的字符串拼接有误,请修正为:
      case 4:
          manageSystem.stu3(p);
          cout << "请输入您接下来要进行的操作" << endl;
          break;
      // ... 其他case也类似修复 ...
      
    3. "." 操作符错误

      • main.cpp的第37行报错“expected unqualified-id before '.' token”,这是因为您可能在调用成员函数后误用了.操作符,应删除不必要的.
    4. 类定义不完整

      • manageSystem类似乎是在student.h中被部分定义了,但您提供的代码片段并不完整。确保整个类(包括其成员函数)都已正确地在类定义或者对应的.cpp文件中实现。
    5. 成员变量声明

      • Student的成员变量如name等应在类体内部声明,并且确保string已经正确包含。

    整理后的student.h应该看起来像这样:

    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include <string>
    
    class Student {
    public:
        std::string name;
        std::string stuNumble; // 可能是笔误,应该是stuNumber?
        float score;
        std::string phoneNumble; // 可能是笔误,应该是phoneNumber?
        std::string homeTown;
        std::string address;
    };
    
    class manageSystem {
    public:
        struct Student personArray[50];
        int currentSize = 0;
    
        void stu3(manageSystem* p, std::string stuNumber);
        // ... 其他stuX函数的声明 ...
        int stu9(manageSystem* p);
    };
    
    #endif // STUDENT_H
    

    同时,请确保所有在student.h中声明的函数,在对应的student.cpp或其他合适的.cpp文件中有正确的实现。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月22日
  • 已采纳回答 3月14日
  • 创建了问题 3月14日