我已经实现了查找max和min的函数。但我得到以下错误代码: [错误]来自'std :: ios_base&的无效转换(*)(std :: ios_base&)'到'int'[-fpermissive]
#include<iostream>
using namespace std;
int a[1000], n, maxDiff, maxValue, minValue;
void nhap() {
int i, n;
cout << "Nhap n: ";
cin >> n;
for ( int i =1; i <= n; i ++) {
cout << "Nhap vao mang thu i: " << i << endl;
cin >> a[i];
}
}
void findmaxdiff(int left, int right, int maxDiff, int& maxValue, int& minValue) {
int mid;
int maxD1, maxV1, minV1;
int maxD2, maxV2, minV2;
if(left==right) {
maxDiff = 0;
maxValue = a[right];
minValue = a[right];
}
else {
mid = (left +right) / 2;
findmaxdiff(left, mid, maxD1, maxV1, minV1);
findmaxdiff(mid+1, right, maxD2, maxV2, minV2);
maxDiff = maxV2 - minV1;
if(maxDiff< maxD1) maxDiff = maxD1;
if(maxDiff< maxD2) maxDiff = maxD2;
if(maxV1> maxV2){
maxValue= maxV1;
}
else {
maxValue = maxV2;
}
if(minV1< minV2) {
minValue = minV1;
}
else {
minValue= minV2;
}
}
}
int main() {
nhap();
findmaxdiff(left, n, maxDiff, maxValue, minValue);
cout << maxDiff;
}
答案 0 :(得分:2)
缺少变量#include "iostream"
#include <string>
using namespace std;
class Base {
public:
int value;
string name;
Base(int _value, string _name) : value(_value),name(_name) {
}
virtual void print() {
cout << "name is " << name << " value is " << value << endl;
}
};
class Child : public Base{
public:
int value2;
Child(int _value, string _name, int _value2): Base(_value,_name), value2(_value2) {
}
virtual void print() {
cout << "name is " << name << " value is " << value << " value2 is " << value2 << endl;
}
};
int main()
{
Base base = Base(10,"base");
Child child = Child(11,"child",22);
Base* base_array[2];
base_array[0] = &base;
base_array[1] = &child;
base_array[0]->print();
base_array[1]->print();
return 0;
}
定义。
不要left
将std
命名空间扩展到全局命名空间中,您将避免此类错误。您忘记声明变量using namespace std;
,并且已应用名称空间left
中的std::ios_base& left( std::ios_base& str );
函数。