【发布时间】:2016-01-13 23:01:28
【问题描述】:
我在使用在不同命名空间中声明的结构时遇到问题。在 File1.h 中,我声明了结构并将其放在命名空间“Foo”中。
//File1.h
namespace Foo{
struct DeviceAddress;
}
struct DeviceAddress {
uint8_t systemID;
uint8_t deviceID;
uint8_t componentID;
};
在 File2.c 中,我尝试创建该结构的实例:
//File2.c
#include "File1.h"
struct Foo::DeviceAddress bar;
但我在 File2.c 中尝试声明结构的那一行出现错误。错误信息是: 错误 C2079:'bar' 使用未定义的结构 'Foo::DeviceAddress'
我使用 MS C++ 编译器和 Visual Studio 作为开发环境。
我是在尝试声明“bar”时出现某种语法错误,还是我不了解命名空间或结构?
【问题讨论】:
-
你必须声明你的
struct驻留在这个命名空间中。前向声明不足以使其进入Foo。
标签: c++ struct namespaces