我正在为移动机器人编写 .lib 文件。 在关于机器人移动的其他命令中,我还有扫描蓝牙设备的命令,如下所示:
void ScanForDevices(vector<Device> &Robot)
{
/* code for searching Bluetooth devices and saving their names and addresses into vector of Device struct*/
}
我的问题与编写 .lib 文件的 header 有关。
我的命令之一是:
string RobotMove(int Translation, int Rotation)
{
/* create Command
return string(Command);
}
在标题中,对于该命令我有:
// Returns MOVE command
std::string RobotMove(int Translation, int Rotation);
我遇到的问题是在 header 中写什么:
void ScanForDevices(vector<Device> &Robot)
我一直收到“不允许不完整的类型”是我尝试以与 RobotMove 命令相同的方式进行操作。我是否必须以某种方式在 header 结构 Device 中声明?
最佳答案
Do I have to declare in some way in the header struct Device?
如果要为 Device
创建 vector s,你需要通过定义让编译器知道类的大小。正如 BoBTFish 所指出的,通常的方法是只包含 Device.h
标题(或类似标题)。
如果您使用指针(vector<Device*>
或什至更好,适当类型的智能指针:vector<shared_ptr<Device>>
)前向声明(仅声明class Device;
)就足够了,因为编译器知道指针在您的架构上的大小。 (请注意,这是完全不同的方法,具有与您的问题不同的语义,这只是旁注)。
关于c++ - 我的静态库 header 中的 void 函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188508/