2

I don't understand how fix this problem, tried many things but no solution. Help on it would be much appreciated. Thanks.

Error 1 error C2664: 'void showAllBuses(const Bus *[],int)' : cannot convert argument 1 from 'Bus **' to 'const Bus *[]'

void showAllBuses(const Bus* pBuses[], int numBus) {
    for (int i = 0; i < numBus; i++) {
        cout << "Bus no ." << numBus << " details: " << endl;
        cout << "Number: " << pBuses[i]->getNumber() << endl;
        cout << "Driver name: " << pBuses[i]->getDriver().getName() << endl;
        cout << "Driver experience(years): " << pBuses[i]->getDriver().getYearsDriving() << endl;
    }
}

void listBusesWithYearsDriving(const Bus* pBuses[], int numBus, int drivingYears) {
    for (int i = 0; i < numBus; i++) {
        if (pBuses[i]->getDriver().getYearsDriving() >= drivingYears) {
            cout << "Bus number: " << pBuses[i]->getNumber() << endl;
            cout << "Driver name: " << pBuses[i]->getDriver().getName() << endl;
            cout << "Driver experience: " << pBuses[i]->getDriver().getYearsDriving() << endl;
        }
    }
}

void removeDriver(Bus* pBuses[], int busPos) {
    pBuses[busPos]->removeDriver();
}

void main() {
    const int ASIZE = 4;
    int drivingYears = 0;
    Bus* buses = new Bus[ASIZE];
    for (int i = 0; i < ASIZE; i++) {
        addNewBus(&buses[i]);
        cout << "Bus " << i << ": " << &buses[i] << endl;
    }
    showAllBuses(&buses, ASIZE);
    cout << "Please enter a minimum years of experience to look for: " << endl;
    cin >> drivingYears;
    listBusesWithYearsDriving(&buses, ASIZE, drivingYears);
    removeDriver(&buses, 0);
    showAllBuses(&buses, ASIZE);
    delete[] buses;
    buses = nullptr;
    cout << "\n\n";
    system("pause");
}

1 Answer 1

2

For any type T, T* can be implicitly converted to const T*, but T** cannot be implicitly converted to const T**. Allowing this would make it possible to violate constness(1).

A T ** can be converted to const T* const *, though. Since your function does not modify the array in any way, you can simply change its parameter like that:

void showAllBuses(const Bus* const * pBuses, int numBus) {

Bear in mind that in a function parameter declaration, * and the outermost [] are synonyms.


(1) Here's the code:

const int C = 42;
int I = -42;
int *p = &I;
int *pp = &p;
const int **cp = pp;  // error here, but if it was allowed:
*cp = &C;  // no problem, *cp is `const int *`, but it's also `p`!
*p = 0;  // p is &C!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.