[英]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. 我现在正在单独的header和cpp文件中创建一个名为person的类。 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:...")
声明与“Person :: stat Person :: getStat()”(在“C:...”的第26行声明)不兼容
(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: 这是cpp文件中的代码:
#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. 我收到了getStat()函数的错误。 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.
我已经尝试在两个文件中包含字符串和iostream标头,也只在头文件中包含,因为类似的帖子建议它。 Both didn't solve my problem however.
然而,两者都没有解决我的问题。
Should be 应该
Person::stat Person::getStat() {
}
Your version declares a new struct stat
which isn't the same as Person::stat
. 您的版本声明了一个新的
struct stat
,它与Person::stat
。
struct stat Person::getStat()
is a method that returns a stat
that belongs to the global namespace, not to Person
: struct stat Person::getStat()
是一个返回属于全局命名空间的stat
的方法,而不是Person
:
Person::stat Person::getStat()
Note that there is no struct
here (to avoid declaring one). 请注意,这里没有
struct
(以避免声明一个)。 In C++, we don't use struct
after the type has been declared. 在C ++中,我们在声明类型后不使用
struct
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.