如何解决 c++ 中不完整类型的无效使用错误

发布于 2025-01-10 15:19:09 字数 1960 浏览 3 评论 0原文

我试图了解运行 main.cpp 文件时出现错误的原因。 我在 main.cpp 文件中包含两个类(foo 和 gee)。

这是我得到的错误:

foo.cpp: In member function ‘void foo::save(gee&)’:
foo.cpp:13:10: error: invalid use of incomplete type ‘class gee’
   13 |         i->addfoo(this);
      |          ^~
foo.h:7:7: note: forward declaration of ‘class gee’
    7 | class gee;
      |    

这里是 foo 类的声明(foo.h):

#ifndef foo_H 
#define foo_H


#include <set>
#include <string>
class gee;
class foo
{
public:
    foo(std::string _str);
    void save(gee& geeobj);
private:
    std::string str;
    std::set<gee*> geelist;
};

#endif

这里是 foo 类的定义(foo.cpp):

#include "foo.h"

foo::foo(std::string _str)
{
    str = _str;
}

void foo::save(gee& geeobj)
{
    geelist.insert(&geeobj);
    for(auto i:geelist)
    {
        i->addfoo(this);
       // std::cout << "how to run i->addfoo(this); \n"; // this line can be run.
    }

    return;
}

这里是 gee 类的声明(gee.h):

#ifndef gee_H
#define gee_H

#include <set>
#include <string>

class foo;
class gee
{
public:
    gee(std::string _name);
    void addfoo(foo& _foo_obj);

private:
    std::string name;
    std::set<foo*> foolist;
};

#endif

这里是定义gee 类(gee.cpp)

#include "gee.h"

gee::gee(std::string _name)
{
    name = _name;
}

void gee::addfoo(foo& _foo_obj)
{
    foolist.insert(&_foo_obj);
    return;
}

,这是主文件(main.cpp)

#include<iostream>
#include "gee.h"
#include "foo.h"

int main()
{
    gee gobj("geename");
    foo fobj("fooname");
    fobj.save(gobj); 
    return 0;
}

我也试图理解这些文件的依赖关系以及为什么我无法执行 main.cpp 就像链接器问题找不到一样addfoo 函数体。 如果您能给我补充解决方案,解释为什么我们不能使用它,我将非常感谢。

我使用 g++ 作为编译器并执行 main.cpp 我只需键入这两行。

g++ gee.h foo.h
g++ main.cpp gee.cpp foo.cpp -o a

I am trying to understand the reason of the error I get while running main.cpp file.
I have two class (foo and gee) included in main.cpp file.

here is the error I get:

foo.cpp: In member function ‘void foo::save(gee&)’:
foo.cpp:13:10: error: invalid use of incomplete type ‘class gee’
   13 |         i->addfoo(this);
      |          ^~
foo.h:7:7: note: forward declaration of ‘class gee’
    7 | class gee;
      |    

here is the declaration of foo class(foo.h):

#ifndef foo_H 
#define foo_H


#include <set>
#include <string>
class gee;
class foo
{
public:
    foo(std::string _str);
    void save(gee& geeobj);
private:
    std::string str;
    std::set<gee*> geelist;
};

#endif

here is the definition of foo class(foo.cpp):

#include "foo.h"

foo::foo(std::string _str)
{
    str = _str;
}

void foo::save(gee& geeobj)
{
    geelist.insert(&geeobj);
    for(auto i:geelist)
    {
        i->addfoo(this);
       // std::cout << "how to run i->addfoo(this); \n"; // this line can be run.
    }

    return;
}

here is the declaration of gee class(gee.h):

#ifndef gee_H
#define gee_H

#include <set>
#include <string>

class foo;
class gee
{
public:
    gee(std::string _name);
    void addfoo(foo& _foo_obj);

private:
    std::string name;
    std::set<foo*> foolist;
};

#endif

here is the definition of gee class(gee.cpp)

#include "gee.h"

gee::gee(std::string _name)
{
    name = _name;
}

void gee::addfoo(foo& _foo_obj)
{
    foolist.insert(&_foo_obj);
    return;
}

and here is the main file(main.cpp)

#include<iostream>
#include "gee.h"
#include "foo.h"

int main()
{
    gee gobj("geename");
    foo fobj("fooname");
    fobj.save(gobj); 
    return 0;
}

I am also trying to understand the dependency of these files and why I can't execute main.cpp like is it the linker problem can't find the addfoo function body.
I thank you if you can give me in addition of the solution, an explanation of why we can't use this.

I am using g++ as compiler and to execute the main.cpp I just type these two line.

g++ gee.h foo.h
g++ main.cpp gee.cpp foo.cpp -o a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文