C++自定义类未被其他类识别
C++ Custom Class is not being recognized by other classes
我有Road和Car类,它们在自己的.cpp和.h文件中。我在Car标题中包含Road类的.h文件。我使用Road类作为Car类中函数的参数。我在汽车类中有静态变量,我需要在道路类中访问。
编译器无法识别Car类中的Road类型,我不知道为什么。
Road.h
#ifndef ROAD_H
#define ROAD_H
#include <iostream>
#include "car.h"
using namespace std;
class Road
{
public:
// class functions
private:
// member variables
};
#endif
Car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include "road.h"
using namespace std;
class Car
{
public:
// class functions
void enter_a_road(Road& r1, const short left_pos);
private:
// member variables
};
#endif
错误消息:
In file included from Road.h:11:0,
from Road.cpp:6:
Car.h:68:23: error: 'Road' has not been declared
void enter_a_road(Road& r1, const short left_pos);
^
您有一个循环依赖项。解决此问题的最快方法是使用正向声明。
#include <iostream>
using namespace std;
class Road;
class Car
{
public:
// class functions
void enter_a_road(Road& r1, const short left_pos);
private:
// member variables
};
这是可以接受的,因为编译器不需要知道Road
的内部结构就可以编译函数声明。
在Road.h
中可能也可以这样做,尽管我不知道为什么一开始就有include。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 模板类无法识别友元运算符
- 如何使枚举被其他类识别
- QT 无法识别 UTF-8 编码,适用于其他所有位置
- 实现文件只能识别其他类的远期声明
- C++ 其他类无法识别的类属性(标头包括公共)
- 为什么主函数不识别其他功能是否返回float类型编号
- Qt::如何识别/检测系统是否被Linux中的其他系统远程访问
- 使用Windows生物识别框架或任何其他方法通过.Net访问指纹读取器
- 模板功能,用于从其他容器中识别smart_pointer
- 当我的分词器识别出令牌时,如何执行其他操作
- Qt不识别其他子项目
- 其他类无法识别包含C++的类
- C++自定义类未被其他类识别
- 检测/识别端口(HDMI,其他)显示器连接到Windows 7/8/10 win32c++
- cc1plus:无法识别的命令行选项警告任何其他警告