解决c++静态成员编译报错:'xxx' is not a member of 'xxx' 问题

发布时间:2023-12-19 22:00:25 作者:yexindonglai@163.com 阅读(404)

问题描述

今天自己用c++写了一个静态的工具类,用来处理字符串切割用的,发现写好之后,出现一个很奇葩的问题,死活编译不过去,就是一个劲地报错;

但我看了代码这样写是没问题的;

源码

StringUtils.h

  1. #ifndef FLOW_SERVER_STRINGUTILS_H
  2. #define FLOW_SERVER_STRINGUTILS_H
  3. #include "string"
  4. #include "vector"
  5. #include <sstream>
  6. #include "iostream"
  7. using namespace std;
  8. /**
  9. * 字符串工具类
  10. */
  11. class StringUtils {
  12. public:
  13. static std::vector<std::string> split();
  14. };
  15. #endif //FLOW_SERVER_STRINGUTILS_H

StringUtils.cpp

  1. #include "StringUtils.h"
  2. std::vector<std::string> StringUtils::split() {
  3. std::vector<std::string> list;
  4. return list;
  5. }

main.cpp

  1. #include "StringUtils.h"
  2. int main() {
  3. StringUtils::split();
  4. }

解决方案

我做了以下操作解决了我的问题:

  • StringUtils.h 改为 StringUtil.h,
  • StringUtils.cpp 改为 StringUtil.cpp;

之后在运行,一切又正常了

关键字c++