简体   繁体   中英

Trying to pass a ** array but keep getting c2664 error

Okay, so I have a class called student and another called faculty that are both derived classes from a person class. I need to make two teams consisting of both student and faculty. I have an array of students and an array of faculty. I created two more arrays for the teams that are both person**. when I try to pass the array I get the compiler error c2664 with a message of cannot convert parameter3 from 'Person**[12] to Person*[]'

Student sArr[12];
Faculty fArr[12];

Person** t1Arr[12];
Person** t2Arr[12];
generateTeams(fArr,sArr,t1Arr,t2Arr);
}
void generateTeams(Faculty fArr[],Student sArr[],Person** t1Arr[], Person** t2Arr[]){....}

I was also getting erros while attempting to assign the student/faculty to the person array so I had to reinterpret_cast as follows:

    for(int i=0;i<12;i++){
        if(sArr[i].getTeam()==1){
            t1Arr[t1Count]=reinterpret_cast<Person**> (&sArr[i]);
            t1Count++;
        }
        else if(sArr[i].getTeam()==2){
            t2Arr[t2Count]=reinterpret_cast<Person**> (&sArr[i]);
            t2Count++;
        }
        else if(fArr[i].getTeam()==1){
            t1Arr[t1Count]=reinterpret_cast<Person**> (&fArr[i]);
            t1Count++;
        }
        else if(fArr[i].getTeam()==2){
            t2Arr[t2Count]=reinterpret_cast<Person**> (&fArr[i]);
            t2Count++;
        }
    }

I can provide mor of the code if needed, but I think what I have shown should suffice as that is where the problem lies.

Student sArr[12] is an array of Students.

If I understand your code, you're trying to assign your Students and Faculty to teams by keeping separate arrays of pointers to Person objects presumably Student and Faculty both inherit from Person).

If this is correct, then you want your teams to be made of of arrays of pointers.

Person* t1Arr[12]

What you have declared instead of are arrays of pointers-to-pointers, which has it's uses, but those are pretty rare.

Assuming that the error message quote

cannot convert parameter3 from 'Person**[12] to Person*[]

is correct, then it does not match the alleged function declaration

void generateTeams(Faculty fArr[],Student sArr[],Person** t1Arr[], Person** t2Arr[]){....}

ie this is not the real code .

Alternatively it's not the real error message.

But basically, assuming that the error message is correctly quoted, it tells you what's wrong: you have declared a function that expects a Person*[] as third argument, but you're passing a Person**[] .


At a guess , you have forward-declared the function, and missed a * .

Forward-declaration is a generally Bad Practice™: it's more work for no gain and does sometimes foul up things, as I suspect it has here.


A basic and probably the best solution is to ditch your current raw-array-and-pointers-and-casting code, and use std::vector .

Your problem appears to be in how you create an array of pointers to objects, and how you use those objects from the array. Here's a simplified example of how to do that:

#include <cstdio>
using namespace std;

class person {
    const char * _name;
public:
    person(const char * n) : _name(n) {}
    const char * getname() const { return _name; }
};

person * pa1[12];

int main( int argc, char ** argv )
{
    const char * names[12] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l" };

    // construct objects
    for( int i = 0; i < 12; ++i) {
        pa1[i] = new person(names[i]);
    }

    // use objects
    for( int i = 0; i < 12; ++i) {
        printf("person %d is %s\n", i, pa1[i]->getname());
    }

    // destroy objects
    for( int i = 0; i < 12; ++i) {
        delete pa1[i];
    }

    return 0;
}

Here I've created a simple class, person , and I've constructed an array of pointers to person like this:

person * pa1[12];

I then used a simple for loop to construct objects with the new operator:

for( int i = 0; i < 12; ++i) {
    pa1[i] = new person(names[i]);
}

The new operator allocates space for each object, and calls its constructor.

I can then use those objects by dereferencing the pointer with the -> operator:

for( int i = 0; i < 12; ++i) {
    printf("person %d is %s\n", i, pa1[i]->getname());
}

Because I used new to construct the objects, I need to destroy them with delete :

for( int i = 0; i < 12; ++i) {
    delete pa1[i];
}

Also note that it's always a good idea to reduce a problem to a short self-contained example in order to best understand it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM