Reputation: 62
I have declared a variable name
within my people
class which is producing an ambiguity error. I tried renaming the name
variable, avoided using using namespace std
, imported the required libraries, replaced character array with string type but it throws same error all the time. I referred to other posts on Stack Overflow but I could not find the solution.
#include <iostream>
#include <cstdlib>
#include <cstring>
class people{
public:
char name[20];
};
class staff : public people {
public:
int i = 3;
};
class manager : public people {
public:
int j = 8;
};
class lecturer : public manager, public staff {
public:
void set(char* a) {
strcpy(name, a);
}
void get(){
std::cout << "Lecturer name: " << name << std::endl << "Number of managers: " << j << std::endl << "Number of staff: " << i << std::endl;
}
};
int main() {
lecturer ob;
ob.i = 1;
ob.j = 6;
ob.set("aaa bbb");
ob.get();
return 0;
}
and the error message is
main.cpp:24:16: error: reference to ‘name’ is ambiguous
strcpy(name, a);
Upvotes: 0
Views: 479
Reputation: 46
The compiler doesn't know which member 'name' should use in the strcpy function since lecturer inherits from 2 classes which are manager and staff (both have member 'name'), so to tell the compiler who's name should use, to assign manager's name, you can use:
strcpy(manager::name, a);
and for staff's name:
strcpy(staff::name, a);
Upvotes: 2