나는 C++ 초보자이며 왜 내 간단한 코드가 컴파일되지 않는지 이해하지 못합니다. 첫 번째 인수에서 문자열을 응용 프로그램에 전달하여 클래스의 ifstream 객체를 인스턴스화하려고합니다.
건물은 제공하지 않습니다 : 오류 : 일치를 호출에 '(표준 : : ifstream) (CONST의 char *)'C++, ifstream 객체를 위해 string 함수를 사용하여 클래스 함수에 파일 이름 전달
헤더 :
#ifndef _SIMPLE_TEST
#define _SIMPLE_TEST
#include <iostream>
#include <fstream>
#include <string.h>
class mytest {
public:
mytest(int number);
bool validate_config(const std::string &s);
private:
std::ifstream configFileStream;
int magic;
};
#endif
코드 :
#include <iostream>
#include <fstream>
#include <string.h>
#include "simple.hpp"
mytest::mytest(int number) {
magic = number;
}
bool mytest::validate_config(const std::string &s) {
configFileStream(s.c_str());
return true;
}
int main(int argc, char **argv) {
bool check;
std::string configFile(argv[1]);
mytest DBG(17);
check = DBG.validate_config(configFile.c_str());
if (check) {
return 0;
}
return -1;
}
빌드 오류 (콘솔에서) :
Compile src/simple.cpp
src/simple.cpp: In member function 'bool mytest::validate_config(std::string)':
src/simple.cpp:12:28: error: no match for call to '(std::ifstream) (const char*)'
Makefile:23: recipe for target `_out/simple.o' failed
make: *** [_out/simple.o] Error 1
나는 검색하고 결과는 항상 내가
['std :: ifstream'] (http://en.cppreference.com/w/cpp/io/basic_ifstream) 클래스는 당신이 호출하려고하는'operator()'함수를 가지고 있지 않습니다. 아마도''open' (http://en.cppreference.com/w/cpp/io/basic_ifstream/open) 함수를 찾고 있습니까? –