0

I have implemented functions to find the max and min. But I get the error code below: [Error] invalid conversion from 'std::ios_base& (*)(std::ios_base&)' to '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;
    }

enter image description here

Jodocus
  • 7,493
  • 1
  • 29
  • 45

1 Answers1

3

The variable left definition is missing.

Do not expand std namespace into the global namespace by using namespace std; and you will avoid such errors. You forgot to declare variable left and function std::ios_base& left( std::ios_base& str ); from namespace std is applied.

Why is using namespace std considered bad practice?

273K
  • 29,503
  • 10
  • 41
  • 64
  • The compiler note says it was an issue with the first parametre of the function. I think it is just a matter of getting familiar with the compiler messages. – Gamenotcore May 26 '18 at 06:32
  • 1
    @Gamenotcore It says that `left` comes from `std`, that is enough to understand the reason. – 273K May 26 '18 at 06:34
  • Well, that is true but the other half of issue lies with understanding the compiler messages. But, yes, proper code practices minimize the potential for the issue to come up. The same issue, however, could still show up without importing std if you made a mistake with your own scope's variables. – Gamenotcore May 26 '18 at 06:44