此错误的原因是什么
what is the reason of this error?
在我的代码中,我想从Album
类的方法中使用get_title()
但是如果我在客户.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
如何访问Album
类的方法?
客户.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 PURCH_H
#define PURCH_H
class Song;
class Album;
class Purchase{
public:
vector <Song*> get_songs() { return songs; }
vector <Album*> get_albums() { return albums; }
private:
vector <Song*> songs;
vector <Album*> albums;
};
#endif
相册 :
#ifndef ALBUM_H
#define ALBUM_H
class Song;
class Album{
public:
string get_title() { return title; }
private:
string title;
vector <Song> songs;
};
#endif
客户.h :
#ifndef CUST_H
#define CUST_H
class Purchase;
class Customer{
public:
void print_tmpPurchase();
private:
Purchase tmpPurchase;
};
#endif
歌曲.h :
#ifndef SONG_H
#define SONG_H
class Album;
class Song{
// . . .
private:
Album* album;
};
#endif
问题是,当您尝试访问实例的成员时,类定义对编译器不可见。尽管您已经为类提供了前向声明,但这还不够。在访问这些类的成员或需要其大小的 .cpp 和 .h 文件中,您需要包含所有适当的标头,以便其定义在使用时可见。但是,在这种情况下,您的Album
类似乎需要Song
的定义,而不仅仅是前向声明。
在Album.h
添加
#include "Song.h"
在customer.cpp
添加
#include "Album.h'
等等...
相关文章:
- 是什么导致了Unity 3D中的"错误线程异常"?
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在C++中创建队列 - 什么是 malloc 错误?
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 没有可行的过载'='错误,可能导致这种情况的原因是什么?
- Clang 8 带有静态 constexpr 和数组的链接器错误 - 错误是什么以及如何解决它?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 导致hash_map错误的原因是什么,如何修复它?
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- C++中的"expected unqualified-id"错误是什么?
- 谁能告诉我,程序中的错误是什么?该程序仅用于获取文件扩展名
- 是什么导致 glTexSubImage1D() - 加载纹理时在此示例中出现GL_INVALID_VALUE错误?
- 我的代码中针对 NQueens 问题的错误是什么?
- 这在C++是什么类型的错误?
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 这种错误的原因是什么:将"功能"重新定义为不同类型的符号
- CUDA返回值错误35的含义是什么
- 这个错误可能是什么?ANSI C文件
- 是什么导致macOS Mojave上的GoogleTest链接错误
- 这个代码中的错误是什么,每次都会给我分段错误