【问题标题】:Type 'string' could not be resolved类型“字符串”无法解析
【发布时间】:2021-04-24 00:29:11
【问题描述】:

我是 C++ 编码的新手,我正在尝试从另一个文件调用一个函数来检查包含文本文件的字符串是否由字母字符组成,但由于某种原因它不起作用。

我的 ap.cpp 文件中出现错误

无效参数' 候选人是: 布尔 is_alpha(?) ' 和

‘is_alpha’不能用作函数

还有我的头文件中的错误

无法解析类型“字符串”

我的代码:

AP.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "functions.h"
using namespace std;

int main () {
  string line;
  string textFile;
  ifstream myfile ("encrypted_text");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
        textFile += line;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  bool check = is_alpha(textFile);
  if (check){
      cout << "true";
  } else cout << "false";

  return 0;
}

checkFunctions.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include "functions.h"
using namespace std;

bool is_alpha (string str) {
    for(int i=0; i < str.size(); i++)
    {
        if( !isalpha(str[i]) || !isspace(str[i]))
        {
            return true;
        }
    }
    return false;
}

functions.h

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include <string>

bool is_alpha(string str);



#endif /* FUNCTIONS_H_ */

【问题讨论】:

标签: c++ string function header-files


【解决方案1】:

在您正确理解该语言及其含义之前,最好不要使用using namespace std;。如果您想知道 using namespace 的作用是什么,它基本上将内容设置在命名空间中并将其放入全局命名空间(您无需指定它的来源,在此 std 和方式称它为std::)。

错误是因为编译器不知道bool is_alpha(string str); 中的string 来自哪里。所以要解决这个问题,请考虑我的第一个建议,您可以像这样指定它的来源:bool is_alpha(std::string str);

另外,您无需在源文件中再次添加包含在头文件中的库。这意味着您可以从AP.cppcheckFunctions.cpp 中删除#include &lt;string&gt;

更多关于头文件:https://stackoverflow.com/a/9224641/11228029

【讨论】:

    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2019-02-15
    • 1970-01-01
    • 2017-06-05
    • 2020-05-01
    • 1970-01-01
    相关资源
    最近更新 更多