首页 新闻 会员 周边

C++ 新手问题 关于类和结构体

0
悬赏园豆:20 [已解决问题] 解决于 2018-06-03 11:47

直接上代码

 1 #include "Guards.h"
 2 
 3 //记录结构体
 4 struct rec
 5 {
 6     size_t cbsize;
 7     time_t front;
 8     time_t back;
 9     long len;
10     CGuards::GuardsInfo guardsinfo;
11 };

问题来了,编译器开始报错:

这里提示识别不出来CGuard是我自定义的类。(CGuard是定义在Guard.h中)

请指教!!!

C++
问题补充:

而且以下代码也无法编译通过!

1     //自定义ID类型
2     //using id = long;
3     typedef long id;
4     //别名标识记录类型
5     //using counter = id;
6     typedef id counter;

推测应该是vs2012不支持C11标准中使用using定义别名的用法。

TearsOfDawn的主页 TearsOfDawn | 初学一级 | 园豆:34
提问于:2018-05-30 21:21
< >
分享
最佳答案
0

类成员变量作为变量类型这个就是不对的,因为成员变量是变量,而不是变量类型

收获园豆:20
Arthurian | 小虾三级 |园豆:1123 | 2018-05-31 09:19

意思是自定义类不可以充当结构体的成员?

TearsOfDawn | 园豆:34 (初学一级) | 2018-05-31 19:28

@TearsOfDawn: 自定义类的对象可以作为结构体成员,但是类的成员变量不能作为结构体成员的变量类型;请分清楚 变量、变量类型、类、类的成员

Arthurian | 园豆:1123 (小虾三级) | 2018-05-31 19:47

@Amedeo: 我这里的GuardsInfo不是类的成员,而是内嵌在CGuard类中的结构体类型,具体代码在“Guards.h”中,如下:

 1 class CGuards
 2 {
 3 public:
 4     CGuards(void);
 5     CGuards(CDuration* prt):parent(prt){}
 6     ~CGuards(void);
 7 
 8     struct GuardsInfo
 9     {
10         int cbsize;
11         CGuard::personnel* guards;
12     };
13 
14     const GuardsInfo& enquireGuardsInfo();
15     void destroyGuardsInfo();
16 
17     CGuard& operator[](string id);
18 
19     int size();
20 
21     void append(string id,CGuard guard);
22     void remove(string id);
23     void clear();
24 
25 private:
26     map<string,CGuard> guards;
27     CDuration *parent;
28     GuardsInfo info;
29 };

这样用也不可以吗?

TearsOfDawn | 园豆:34 (初学一级) | 2018-05-31 20:00

@TearsOfDawn: 这样的话 应该是别的地方错误,编译器定位显示到这了而已

Arthurian | 园豆:1123 (小虾三级) | 2018-05-31 20:26

@Amedeo: 我快疯了,许多头文件都是类似定义的,都出现了雷同的错误,而且还找不到错误源。

TearsOfDawn | 园豆:34 (初学一级) | 2018-05-31 20:31
其他回答(2)
0

guardsinfo 在后面定义

blck | 园豆:202 (菜鸟二级) | 2018-05-30 23:00

没听懂,请详谈!

支持(0) 反对(0) TearsOfDawn | 园豆:34 (初学一级) | 2018-05-31 19:27
0

已经解决!

需要进行依赖项设置,主要通过头文件包含,以及类别前置声明。

TearsOfDawn | 园豆:34 (初学一级) | 2018-06-03 11:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册