简体   繁体   English

为什么我得到“声明与(x)不兼容”?

[英]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.

相关问题 为什么我的向量迭代器不兼容? - Why am I getting vector iterators incompatible? 为什么我得到“矢量迭代器不兼容”? - Why am I getting “vector iterators incompatible”? 为什么在一个声明中得到多种类型? - Why am I getting Multiple Types in One Declaration? 为什么我得到没有类型错误的声明 - 链表? - Why am I getting declaration with no type error - linked list? 为什么我在声明本身中收到“未在范围内声明”错误? - Why am I getting a "not declared in scope" error in the declaration itself? 我收到重言式编译器错误。 我应该如何修复“类型“X”的参数与类型“X”的参数不兼容? - I'm getting tautological compiler errors. How am I supposed to fix “argument of type ”X“ is incompatible with parameter of type ”X"? 为什么我得到“char 类型的参数与 FILE 类型的参数不兼容”? - Why am I getting “argument of type char is incompatible with parameter of type FILE”? 为什么我在&#39;a = x&#39;中收到错误,表示操作符=不匹配? - Why am I getting the error no match for operator= in 'a = x'? 为什么我在“找不到‘X’标识符’”时出现错误 - Why am i getting an error on “ 'X' identifier not found' ” 为什么在标头中的类声明中声明变量时出现错误? - Why am I getting an error when declaring a variable in a class declaration within a header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM