类指针作为另一个对象的数据成员
Class Pointer as a Data Member of Another Object
只是得到一个奇怪的错误,我不完全确定为什么。
我有4个文件(两个标题和两个实现)。问题在标题中:
主文件只包含Station.h,这就是Station.h包含在其中的原因。
车站.h
#ifndef STATION_H
#define STATION_H
#include "Stations.h"
#include <string>
enum PassType{student, adult};
class Station{
std::string station_name;
unsigned int student_passes;
unsigned int adult_passes;
public:
Station();
void set(const std::string&, unsigned, unsigned);
void update(PassType, int);
unsigned inStock(PassType) const;
const std::string& getName() const;
};
#endif
车站.h
#ifndef STATIONS_H
#define STATIONS_H
#include "Station.h"
namespace w2{
class Stations{
Station *station;
public:
Stations(char *);
void update() const;
void restock() const;
void report() const;
~Stations();
};
}
#endif
它不知道什么是车站。我得到以下错误:
./Stations.h:9:2: error: unknown type name 'Station'; did you mean 'Stations'?
Station *station;
我到底错过了什么?
您是Station.h中Station.h的#include
。因此,编译器在class Station
之前看到class Stations
。在这种情况下,Station
似乎不需要Stations
,所以您可以简单地删除include。
如果Station
确实需要了解Stations
,那么您必须在其中一个标头或另一个标头中使用正向声明(注意不要以需要完整定义的方式使用正向声明的类)。
不要忘记在声明Stations类之后加一个分号:
class Stations {
Station *station;
};
U需要进行正向声明。从Station.h 中删除#include"Station.h"
#ifndef STATIONS_H
#define STATIONS_H
#include "Station.h"
namespace w2{
class Station;
class Stations{
Station *station;
public:
Stations(char *);
void update() const;
void restock() const;
void report() const;
~Stations();
};
}
#endif
相关文章:
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 从R中访问C 对象数据成员
- 通过指向对象数据成员的指针更改该对象的值
- 只有当声明一个对象时,内存空间才会分配给类的数据成员,那么为什么 Sizeof(class_name) 给我结果
- 如何在C 中的另一个对象的同一数据成员中输入另一个字符串
- C .更改对象数据成员的数据成员
- 在C++中,"static initialization fiasco"是否仅影响对另一个模块中定义的对象的数据成员的引用?
- 从另一个对象调用成员函数时出现问题
- 排序类对象数据成员从大到小
- 为具有一个数字数据成员的类定义所有比较运算符的方便方法
- 如何设置对象数据成员与这个c++与这个
- 如何在另一个类数据成员上定义“std::set”排序
- 另一个对象所包含的一个对象如何可以更改容器对象的私有数据成员
- 将一个对象的成员函数作为回调函数存储在另一个对象中
- 将一个对象数据复制到另一个相同类型 C++ 的对象
- 从Cocos2d::Layer派生的类有一个指针数据成员.这个元素也有一个向量容器.调用vector::push_bac
- 如何声明一个常量数据成员,但直到以后才初始化它
- C++:将指向一个对象的成员函数的指针存储在另一个对象中
- 按对象数据成员属性搜索对象的向量