Kupperu
Kupperu

Reputation: 121

Why am I getting a "declaration is incompatible with (x)"?

I'm creating a class called person right now in separate header and cpp files. And for one of the functions I'm getting this error:

declaration is incompatible with "Person::stat Person::getStat()" (declared at line 26 of "C:...")

(Not the exact directory but you get the idea)

Here is the code in the header file:

#pragma once
#include <string>
#include <iostream>
class Person
{
public:
    struct stat {
        int str;
        int end;
        int dex;
        int intel;      
    };
    Person();
    ~Person();
    //properties
    stat getStat();
};

Here is the code in the cpp file:

#include "pch.h"
#include "Person.h"

#include <string>
#include <iostream>

Person::Person()
    :age(12), height(0)
{
}


Person::~Person()
{
}

struct stat Person::getStat() {

}

I'm getting the error with the getStat() function. I've tried including the string and iostream headers in both file and also only in the header file since a similar post suggested it. Both didn't solve my problem however.

Upvotes: 1

Views: 3350

Answers (2)

Matthieu Brucher
Matthieu Brucher

Reputation: 22043

struct stat Person::getStat() is a method that returns a stat that belongs to the global namespace, not to Person:

Person::stat Person::getStat()

Note that there is no struct here (to avoid declaring one). In C++, we don't use struct after the type has been declared.

Upvotes: 1

john
john

Reputation: 88027

Should be

Person::stat Person::getStat() {

}

Your version declares a new struct stat which isn't the same as Person::stat.

Upvotes: 3

Related Questions