我现在正在单独的头文件和 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/