c++ - 我的静态库 header 中的 void 函数问题

标签 c++ header static-libraries void

我正在为移动机器人编写 .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/

相关文章:

c++ - 没有匹配的函数来调用 basic_string::copy

python - 属性错误 : 'module' object has no attribute 'func' Only With C++ Invocation

c++ - 使用函数返回的值初始化 C++ header 中的 const 值

php - PHP 中 Bing 搜索的 Windows Azure 身份验证

c++ - 头文件和c++文件的问题

python - 我无法使用 Visual Studio 将 python.dll 构建为静态库 (/MTd)

c# - c#有没有类似c++的复制控制概念

c++ - std::map 中的用户定义类:二进制表达式的无效操作数

c++ - 在 Xcode 4 中包含 C/C++ header

iOS 静态库不链接 XCODE 5 iOS 7