C++ 这个错误的原因是什么?
在我的代码中,我想使用我的C++ 这个错误的原因是什么?,c++,class,pointers,C++,Class,Pointers,在我的代码中,我想使用我的Album类的fromget_title()方法,但如果我在customer.cpp中包含“Album.h”,则会出现以下错误: error C2036: 'Song *' : unknown size 但现在我有一个错误: error C2227: left of '->get_title' must point to class/struct/union/generic type error C2027: use of undefined type 'Alb
Album
类的fromget_title()
方法,但如果我在customer.cpp中包含“Album.h”,则会出现以下错误:
error C2036: 'Song *' : unknown size
但现在我有一个错误:
error C2227: left of '->get_title' must point to class/struct/union/generic type
error C2027: use of undefined type 'Album'
IntelliSense: pointer to incomplete class type is not allowed
如何访问我的相册
类的方法
customer.cpp:
#include "customer.h"
void Customer::print_tmpPurchase()
{
if (tmpPurchase.get_albums().size() != 0)
{
cout << "Albums Of Your Basket: " << endl;
for (int i = 0; i < tmpPurchase.get_albums().size(); i++)
{
cout << "\t" << i + 1 << "." << tmpPurchase.get_albums()[i]->get_title() << endl;
}
}
}
宋h:
#ifndef SONG_H
#define SONG_H
class Album;
class Song{
// . . .
private:
Album* album;
};
#endif
问题是,当您尝试访问实例的成员时,类定义对编译器不可见。尽管您已经为类提供了转发声明,但这还不够。在访问这些类的成员或需要其大小的.cpp和.h文件中,您需要包含所有适当的头文件,以便在使用时可以看到它们的定义。然而,在这种情况下,您的
Album
类似乎需要定义Song
,而不仅仅是一个转发声明
在相册.h中添加
#include "Song.h"
#include "Album.h'
在customer.cpp中
add
#include "Song.h"
#include "Album.h'
等等…请分享客户。hcode@GaneshKamath请不要。共享更多的代码并不能使这个问题变得更好(我怀疑更糟),但我至少可以编译并检查是否可以对此进行改进chap@GaneshKamath“但我至少可以编译和检查…”这通常不值得这么做。OP首先负责提供一份报告,我们可以用它来重现这个错误,并解释他们不清楚的地方。谢谢。我会记住的。非常感谢你。我只在customer.cpp中包含了album.h,它是有效的。
#include "Album.h'