如何解决 c++ 中不完整类型的无效使用错误
我试图了解运行 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论