error: request for member 'GetAge' in 'c', which is of non-class type 'Cat()'

Hi,

I receive this error message: main.cpp:64:15: error: request for member 'GetAge' in 'c', which is of non-class type 'Cat()'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <string> 
#include <iostream> // для объекта cout
using namespace std;

class Cat {
public:
    Cat(int initialAge = 5);
    ~Cat();
    int GetAge() const; // метод доступа const функция доступа к данным-членам класса без права их изменения
    void SetAge(int age);
    //void Meow();

    void Wydam() {
        Bam();
    }; //статический метод
    int Bgsamm();

    Cat() {
        itsAge = 0;
        B = 8;
    };

private:
    int itsAge; // статические переменные класса (Статическое поле)
    int B;

    void Bam() {
        cout << "Wydam\n";
        int BG = itsAge + B;
        cout << BG;
    }

};

int Cat::GetAge() const {
    return itsAge;
}

// конструктор класса Cat
Cat::Cat(int initialAge) {
    itsAge = initialAge;
    cout << "Cat constructor\n";
}

Cat::~Cat() // деструктор, который не выполняет никаких действий
{
    cout << "Cat destructor\n";
}

int Cat::Bgsamm() {
    return (itsAge++);
}

int main(int argc, char** argv) {
    
    Cat c();
    cout << c.GetAge();
    
    return 0;
}


Thank you!
Next time, you should point out the line the error is for.

The problem, on line 56, is because of the parentheses. You do not call the default constructor like that. The compiler will interpret it as a function prototype, where the function's name is c, takes no arguments, and returns a Cat object.
 
Cat c;
Thank you very much

I want to use two constructor (see my code below):
Cat(int initialAge = 5);

and

1
2
3
4
    Cat() {
        itsAge = 0;
        B = 8;
    };


But I cannot. Why?

Output:
main.cpp:63:9: error: call of overloaded 'Cat()' is ambiguous


Cat c;
Because your Cat(int) constructor has a default argument, which lets you call it without arguments.
This allows you to call it like so:
 
Cat c = Cat();

But here comes the problem: which constructor should be called? Do you want the constructor that takes no arguments Cat()? Or do you want to call the constructor that takes an int Cat(int), for which 5 has been provided?

You can look at these functions as an example:
1
2
3
4
5
6
void funky(char c, int I = 5){
   cout << c << I < '\n';
}
void funky(char c){
   cout << c << '\n';
}


If you write funky('a'), do you want just 'a' to be printed, or both 'a' and 5?

You don't really need Cat(). Cat(int = 5) would serve fine as a default constructor.
Thanks! You solved my problem again :)
Topic archived. No new replies allowed.