【发布时间】:2018-03-07 08:18:02
【问题描述】:
我正在从 repo 中读取文件,但无法编译代码。我的 github 合作伙伴(使用 mac)对代码没有问题,但是当我克隆他的 repo 时,我遇到了这个问题。
背景信息: 我最近进入 Linux 世界并正在运行 Elementary。由于我的其他编码项目有效,因此不确定这里是否存在问题,但这是背景信息。
error: no matching function for call to ‘std::basic_ifstream<char>::open(std::__cxx11::string&)’
infile.open(fullPath); // Open it up!
In file included from AVL.cpp:6:0:
/usr/include/c++/5/fstream:595:7: note: candidate: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s, ios_base::openmode __mode = ios_base::in)
^
/usr/include/c++/5/fstream:595:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’
Makefile:4: recipe for target 'main' failed
make: *** [main] Error 1
这是我的功能:
void AVL::parseFileInsert(string fullPath) {
ifstream infile;
infile.open(fullPath); // Open it up!
std::string line;
char c;
string word = "";
//int jerry = 0;
while (getline(infile, line))
{
// Iterate through the string one letter at a time.
for (int i = 0; i < line.length(); i++) {
c = line.at(i); // Get a char from string
tolower(c);
// if it's NOT within these bounds, then it's not a character
if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) {
//if word is NOT an empty string, insert word into bst
if ( word != "" ) {
insert(word);
//jerry += 1;
//cout << jerry << endl;
//reset word string
word = "";
}
}
else {
word += string(1, c);
}
}
}
};
非常感谢!
【问题讨论】:
-
显而易见的尝试是
infile.open(fullPath.c_str()); // Open it up!,但您似乎使用的是 c++11,并且从 c++11 开始可以使用 std::string 作为文件名打开文件,所以我不确定。 -
infile.open(fullPath);返回什么? -
@user:未编译的代码将无法运行。不运行的代码不会返回任何东西。
-
@john 是的,那是我尝试过的原始方法之一,但无济于事。还有其他想法吗?
标签: c++ visual-studio fstream