4
포함 #include <functional>
using namespace std;
int main() {
binary_function<double, double, double> operations[] = {
plus<double>(), minus<double>(), multiplies<double>(), divides<double>()
};
double a, b;
int choice;
cout << "Enter two numbers" << endl;
cin >> a >> b;
cout << "Enter opcode: 0-Add 1-Subtract 2-Multiply 3-Divide" << endl;
cin >> choice;
cout << operations[choice](a, b) << endl;
}
#include <functional>
using namespace std;
int main() {
binary_function<double, double, double> operations[] = {
plus<double>(), minus<double>(), multiplies<double>(), divides<double>()
};
double a, b;
int choice;
cout << "Enter two numbers" << endl;
cin >> a >> b;
cout << "Enter opcode: 0-Add 1-Subtract 2-Multiply 3-Divide" << endl;
cin >> choice;
cout << operations[choice](a, b) << endl;
}
std :: binary_function - 통화와 일치하지 않습니까?
나는 점점 오전 오류는 다음과 같습니다Calcy.cpp: In function ‘int main()’:
Calcy.cpp:17: error: no match for call to ‘(std::binary_function<double, double, double>) (double&, double&)’
사람이 내가이 오류가 어떻게 그것을 제거하는 방법입니다 이유를 설명 할 수 있습니까?
이유는 무엇입니까') ('주의 첫 번째 줄에하지 컴파일러 플래그 오류? 해당 배열 정의가 허용되면 배열에 저장된 함수에 대한 호출도 마찬가지입니다. 그런데 왜 그 오류가 발생 했습니까? –
그것은 단지 당신이 필요로하는 일을합니다. 'binary_function' (또는 표준 라이브러리의 다른 어떤 타입에 대해서) 컴파일러로부터 특별한 대우를받을 자격이 없습니다. 'plus'는'binary_function'을 상속 받았기 때문에 할당이 정확하고 슬라이스가 발생합니다. 컴파일러는'binary (double, double)'가'binary_function'에 정의되어 있지 않기 때문에 최종 행을 컴파일하지 못합니다. 기본을 통해 파생 클래스에 추가 된 모든 메소드를 호출 할 수 없으므로 ... –
@David : 좋아, 지금은 이해했다. 감사. 답으로 게시하고 받아 들일 것입니다. –