0

Do you guys have any hint what's wrong with my code? I've made it as simple as possible and tried to search all over google but still have no idea.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Animal {
public:
    Animal();
    Animal(string _sound) :
        sound(_sound) {}
    virtual ~Animal();
    void give_sound() {
        cout << sound << " ";
    }
protected:
    string sound;
};

class Dog : protected Animal {
public:
    Dog(): Animal("woof") {}
};

int main() {

    Dog doggy();
    doggy.give_sound(); // expression must have class type

    return 0;
}    
IFeel3
  • 167
  • 1
  • 13

1 Answers1

6

Believe it or not Dog doggy(); declares a function names doggy. It returns a Dog by value, and accepts no parameters.

To amend it, don't use parenthesis (if you don't have any parameters to pas) when defining objects with auto storage. Just do Dog doggy;. Alternatively, you can do it in c++11 and onward as Dog doggy{};

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458