Fixed: modified as such:
3rd code piece, 1st line:
static void writeFile(User &user)
User class:
User class:
void print() made public
Now it works as intended. Thanks to everyone that helped.
This is the original post:
1. class User {
2. protected:
3. string name;
4. string surname;
5. .
6. .
7. .
8. void print() {
9. cout << name << " " <<surname; //etc
10. }
11. }
Inherited class:
1. class DiscountUser : public User {
2. public:
3. void print() {
4. cout << "Discount ";
5. User::print();
6. }
7. }
And later in another class I have:
1. void writeFile(User user) {
2. user.print();
3. }
And in another one:
1. User *user = NULL;
2.
3. if (userType == "Discount") {
4. user = new DiscountUser(name, surname, code);
5. }
6. else {
7. user = new BonusUser(name, surname, code);
8. }
9. writeFile(*user);
I get an error C2248: 'User::print' : cannot access protected member declared in class 'User'.
I know I get it because it's trying to access print() in the parent class. Is it because of the first line of the third code piece or did I really mess up with the pointers in the fourth one? Or because I'm a total loser and I'm missing something obvious?
User.printis protected, I would expect theUser.printto be accessible from theDiscountUserclass but not from "another class", which is one of the places where you're calling it fromvoid writeFile(User user) {tovoid writeFile(DiscountUser user) {