c++ - 为什么我得到 "declaration is incompatible with (x)"?

标签 c++ c++14

我现在正在单独的头文件和 cpp 文件中创建一个名为 person 的类。 对于其中一个函数,我遇到了这个错误:

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

(不是确切的目录,但你明白了)

这是头文件中的代码:

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

这是cpp文件中的代码:

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

#include <string>
#include <iostream>

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


Person::~Person()
{
}

struct stat Person::getStat() {

}

我在 getStat() 函数中遇到错误。我已经尝试在两个文件中包含字符串和 iostream header ,并且也只在 header 文件中包含,因为类似的帖子建议这样做。然而,两者都没有解决我的问题。

最佳答案

应该是

Person::stat Person::getStat() {

}

您的版本声明了一个新的 struct stat,它与 Person::stat 不同。

关于c++ - 为什么我得到 "declaration is incompatible with (x)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54224461/

相关文章:

c++ - 可变参数模板扩展、继承和 std::unique_ptr

c++ - 我如何逐字阅读文本文件(分隔空格和换行符)

c++ - SDL2 2D纹理分配/池化

c++ - 在 "if" block 中分配字 rune 字

c++ - 有条件地构造参数包的默认可构造类

c++ - 在 map 中使用 unique_ptr 时删除 std::pair 中的函数

c++ - 许多类变量的 pthread

c++ - 用于查找元素第一次出现的二分搜索的不变量

c++ - 结构如何继承自身?

c++ - 将 'new[]' 智能替换为 make_unique