【发布时间】: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_ */
【问题讨论】:
-
std::string在你的头文件中(因为在那之前没有using namespace std;),而不是string。我建议稍微阅读一下命名空间。 -
每个“C++ 编码新手”如果完全忘记that
using namespace std;is part of C++ and make a promise to never use it in their code,至少在他们获得足够的经验来理解所有含义之前,他们都会帮自己一个很大的忙。
标签: c++ string function header-files