【问题标题】:C++ Class Object inside another class另一个类中的 C++ 类对象
【发布时间】:2022-01-18 00:06:06
【问题描述】:

这让我在Event.h 中收到此错误:

字段“组”的类型“组”不完整

对于上下文,我想要一个类Group,它有一个所有者(来自类Person),它由一个人向量组成(类Person):

组.h

class Person;
#include "Person.h"

Class Group
{
    private:
        std::string name;
        std::vector<Person> people;
        int size = 0;
        Person owner;
    public:
        Group(Person owner);
        ~Group();
}

Person 类中,我只想拥有一个列表向量(List 类,对于这个特定错误并不重要)。请注意,在Person 类中,我有一个构造函数Person(int id);

Event 类中,我想邀请一组可以保存为Group 类的人:

事件.h

class Group;
#include "Group.h"

class Event
{
    private:
        std::string tittle;
        std::string description;
        bool locked;
        bool checked;
        Group group;

    public:
        Event(std::string tittle);
        ~Event();
}

为什么我的群组中不能有 Person 所有者?

编辑: 我不知道为什么,但现在它可以工作了。我只用#pragma once 保护了一切,也许我在编译的方式上做了一些改变。感谢所有答案:)

【问题讨论】:

  • 是的,为什么?什么阻碍了你?失败的症状是什么?请提供minimal reproducible example(最好是单个文件),其中包含有关错误的所有信息。
  • Class 应该是 class(区分大小写)。此外,在定义类之后缺少分号。这让我相信你没有发布你的真实代码。您可以edit您的帖子以包含真实代码(确保您正在运行并发布相同的版本)。此外,您可以通过发布您的Person.h 来澄清您的问题。在发布之前,您可能必须从中删除不需要的内容。另见minimal reproducible example

标签: c++ class object


【解决方案1】:

你正在定义一些乱序的东西。也许是#ifdef 守卫。

这编译得很好:


class Person {};

class Group
{
    private:
        std::string name;
        std::vector<Person> people;
        int size = 0;
        Person owner;
    public:
        Group( Person owr );
        ~Group();
};

class Event
{
    private:
        std::string tittle;
        std::string description;
        bool locked;
        bool checked;
        Group group;

    public:
        Event(std::string tittle);
        ~Event();
};

天箭:https://godbolt.org/z/f785vK1dq

【讨论】:

  • 天哪,制作一个干净的单个文件 MRE 就可以解决问题。真是个惊喜.... ;-)
  • 这正是#include 所做的@Yunnosch
  • 我对此表示怀疑。 Class -> class 不是由任何包含执行的......但是请注意,我的第一条评论是庆祝你的回答,而不是攻击它。
  • C++ 预处理器将扩展内存中的所有包含,然后将其作为单个文件传递给实际的编译器。这是非常普遍的知识。 geeksforgeeks.org/preprocessor-output-of-cpp-file
  • 我不反对。我想知道你的观点是什么。您打算用它来解释预处理器如何将“类”变成“类”?
【解决方案2】:

以下对我来说很好用 (Online Demo):

人.h

#ifndef Person_H
#define Person_H

class Person
{
};

#endif

组.h

#ifndef Group_H
#define Group_H

#include "Person.h"
#include <string>
#include <vector>

class Group
{
    private:
        std::string name;
        std::vector<Person> people;
        int size = 0;
        Person owner;
    public:
        Group(Person owner) : owner(owner) {}
};

#endif

事件.h

#ifndef Event_H
#define Event_H

#include "Group.h"
#include <string>

class Event
{
    private:
        std::string tittle;
        std::string description;
        bool locked = false;
        bool checked = false;
        Group group;

    public:
        Event(std::string tittle) : tittle(tittle), group(Person{}) {}
};

#endif

main.cpp

#include <iostream>
#include "Event.h"

int main()
{
    Event evt("title");
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多