鱼C论坛

 找回密码
 立即注册
查看: 2945|回复: 7

[已解决]C++写静态链表时报错“间接寻址级别不同”。

[复制链接]
发表于 2019-6-3 13:21:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 菜鸟小乔 于 2019-6-3 13:24 编辑

问题描述:静态链表初始化代码只写了初始化函数,就报错:

错误        C2040         “Slist”:“Slist [10]”与“Slist”的间接寻址级别不同.

请问这个错误具体原因是什么?如何改正?


-----------StaticLinkList.h文件---------------------
  1. #ifndef staticlinklist_h_include
  2. #define staticlinklist_h_include
  3. /*
  4. 静态链表 SLL
  5. */
  6. #include<iostream>
  7. using namespace std;

  8. #define MAX_SIZE_SSL 10

  9. typedef struct {
  10.         int id;
  11.         string name;
  12. }EleType;

  13. //静态链表,就是一个结构数组
  14. typedef struct Slist{
  15.         EleType data;
  16.         int next;                //游标cursor,若果为零表示无指向
  17. }Slist[MAX_SIZE_SSL];

  18. void InitSLL(Slist sList);//初始化

  19. void PrintSLL(Slist sList);//打印
  20. #endif
复制代码

-------------------.cpp文件---------------------
  1. #include<iostream>
  2. #include"StaticLinkList.h"
  3. using namespace std;


  4. void InitSLL(Slist sList)//初始化静态链表:每个元素的游标都指向下一个元素
  5. {
  6.         for (int i = 0; i < MAX_SIZE_SSL; i++)//数组第一个元素的游标存放第一个空闲节点的下标
  7.         {
  8.                 sList[i].next = i + 1;
  9.         }
  10.        
  11.         sList[MAX_SIZE_SSL - 1].next = 0;//将最后一个结点置空(最后一个元素的next用来保存第一个插入的元素的下标)
  12.        
  13.         sList[MAX_SIZE_SSL - 2].next = 0;//倒数第二个元素也置零(备用链表尾结点置空,去除首尾后剩余的部分是备用链表)
  14. }
复制代码

------------------main.cpp----------------------------------
  1. #include<iostream>
  2. #include"StaticLinkList.h"
  3. using namespace std;

  4. void testSLL();

  5. int main()
  6. {
  7.         testSLL();
  8.         return 0;
  9. }

  10. void testSLL()
  11. {
  12.         Slist sList;
  13.         InitSLL(sList);
  14. }
复制代码
最佳答案
2019-6-3 23:01:30
本帖最后由 人造人 于 2019-6-3 23:12 编辑

在C++中
  1. struct EleType
  2. {
  3.         int id;
  4.         char name[64];
  5. };

  6. EleType e1;
  7. struct EleType e2;
复制代码


EleType e1;
struct EleType e2;
加struct和不加完全一样,书上好像就是这么写的
struct后面的名字在全局空间

在C语言中
  1. struct EleType
  2. {
  3.         int id;
  4.         char name[64];
  5. };

  6. EleType e1;
  7. struct EleType e2;
复制代码


EleType e1;报错,说找不到这个名字

C语言中的struct后面这个名字不在全局空间,你用的时候每次都要加上struct,很麻烦,解决方法是用typedef把这个名字放到全局空间
例如这样就可以了
  1. typedef struct EleType
  2. {
  3.         int id;
  4.         char name[64];
  5. } Eletype;

  6. EleType e1;
  7. struct EleType e2;
复制代码


但是在C++中名字本来就在全局空间,你用typedef反而导致了名字冲突

也就是说,理论上改成这样就行了
  1. #ifndef staticlinklist_h_include
  2. #define staticlinklist_h_include
  3. /*
  4. 静态链表 SLL
  5. */
  6. #include<iostream>
  7. using namespace std;

  8. #define MAX_SIZE_SSL 10

  9. typedef struct {
  10.         int id;
  11.         string name;
  12. }EleType;

  13. //静态链表,就是一个结构数组
  14. //typedef struct Slist{
  15. typedef struct {
  16.         EleType data;
  17.         int next;                //游标cursor,若果为零表示无指向
  18. }Slist[MAX_SIZE_SSL];

  19. void InitSLL(Slist sList);//初始化

  20. void PrintSLL(Slist sList);//打印
  21. #endif
复制代码



但是,C/C++中的这个typedef真的就是一个别名,你这样改编译器会说
  1. ‘void InitSLL(<unnamed struct>*)’, declared using unnamed type, is used but never defined
复制代码


编译器会说Slist sList的类型是<unnamed struct>*


C/C++中的这个typedef真的就是一个别名
那么好吧,这样总行了吧,你别说这样一改还真行了^_^
  1. #ifndef staticlinklist_h_include
  2. #define staticlinklist_h_include
  3. /*
  4. 静态链表 SLL
  5. */
  6. #include<iostream>
  7. using namespace std;

  8. #define MAX_SIZE_SSL 10

  9. typedef struct {
  10.         int id;
  11.         string name;
  12. }EleType;

  13. //静态链表,就是一个结构数组
  14. //typedef struct Slist{
  15. typedef struct Slist_tag{
  16.         EleType data;
  17.         int next;                //游标cursor,若果为零表示无指向
  18. }Slist[MAX_SIZE_SSL];

  19. void InitSLL(Slist sList);//初始化

  20. void PrintSLL(Slist sList);//打印
  21. #endif
复制代码


也就是保证struct后面的名字和typedef生成的名字不一样,因为这两个名字都在全局空间,名字一样会导致冲突
  1. //静态链表,就是一个结构数组
  2. //typedef struct Slist{
  3. typedef struct Slist_tag{
  4.         EleType data;
  5.         int next;                //游标cursor,若果为零表示无指向
  6. }Slist[MAX_SIZE_SSL];
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-3 13:35:20 | 显示全部楼层
把.cpp改成.c看看
main.c和StaticLinkList.c
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-3 15:14:15 | 显示全部楼层
本帖最后由 菜鸟小乔 于 2019-6-3 15:17 编辑
人造人 发表于 2019-6-3 13:35
把.cpp改成.c看看
main.c和StaticLinkList.c


试了,改文件类型以及头文件,不行。。。

我新建了一个工程,写了一样的代码,就没有报错。。。

我又试了原来那个有问题的工程文件,只留了main.cpp文件,只写了一个return 0;的空的main函数,竟然也报错,这是工程文件哪里有问题吗??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-3 23:01:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 人造人 于 2019-6-3 23:12 编辑

在C++中
  1. struct EleType
  2. {
  3.         int id;
  4.         char name[64];
  5. };

  6. EleType e1;
  7. struct EleType e2;
复制代码


EleType e1;
struct EleType e2;
加struct和不加完全一样,书上好像就是这么写的
struct后面的名字在全局空间

在C语言中
  1. struct EleType
  2. {
  3.         int id;
  4.         char name[64];
  5. };

  6. EleType e1;
  7. struct EleType e2;
复制代码


EleType e1;报错,说找不到这个名字

C语言中的struct后面这个名字不在全局空间,你用的时候每次都要加上struct,很麻烦,解决方法是用typedef把这个名字放到全局空间
例如这样就可以了
  1. typedef struct EleType
  2. {
  3.         int id;
  4.         char name[64];
  5. } Eletype;

  6. EleType e1;
  7. struct EleType e2;
复制代码


但是在C++中名字本来就在全局空间,你用typedef反而导致了名字冲突

也就是说,理论上改成这样就行了
  1. #ifndef staticlinklist_h_include
  2. #define staticlinklist_h_include
  3. /*
  4. 静态链表 SLL
  5. */
  6. #include<iostream>
  7. using namespace std;

  8. #define MAX_SIZE_SSL 10

  9. typedef struct {
  10.         int id;
  11.         string name;
  12. }EleType;

  13. //静态链表,就是一个结构数组
  14. //typedef struct Slist{
  15. typedef struct {
  16.         EleType data;
  17.         int next;                //游标cursor,若果为零表示无指向
  18. }Slist[MAX_SIZE_SSL];

  19. void InitSLL(Slist sList);//初始化

  20. void PrintSLL(Slist sList);//打印
  21. #endif
复制代码



但是,C/C++中的这个typedef真的就是一个别名,你这样改编译器会说
  1. ‘void InitSLL(<unnamed struct>*)’, declared using unnamed type, is used but never defined
复制代码


编译器会说Slist sList的类型是<unnamed struct>*


C/C++中的这个typedef真的就是一个别名
那么好吧,这样总行了吧,你别说这样一改还真行了^_^
  1. #ifndef staticlinklist_h_include
  2. #define staticlinklist_h_include
  3. /*
  4. 静态链表 SLL
  5. */
  6. #include<iostream>
  7. using namespace std;

  8. #define MAX_SIZE_SSL 10

  9. typedef struct {
  10.         int id;
  11.         string name;
  12. }EleType;

  13. //静态链表,就是一个结构数组
  14. //typedef struct Slist{
  15. typedef struct Slist_tag{
  16.         EleType data;
  17.         int next;                //游标cursor,若果为零表示无指向
  18. }Slist[MAX_SIZE_SSL];

  19. void InitSLL(Slist sList);//初始化

  20. void PrintSLL(Slist sList);//打印
  21. #endif
复制代码


也就是保证struct后面的名字和typedef生成的名字不一样,因为这两个名字都在全局空间,名字一样会导致冲突
  1. //静态链表,就是一个结构数组
  2. //typedef struct Slist{
  3. typedef struct Slist_tag{
  4.         EleType data;
  5.         int next;                //游标cursor,若果为零表示无指向
  6. }Slist[MAX_SIZE_SSL];
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-3 23:07:11 | 显示全部楼层
本帖最后由 人造人 于 2019-6-3 23:17 编辑

这个位置没用了,可以删除,我好像无法删除
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-3 23:09:10 | 显示全部楼层
本帖最后由 人造人 于 2019-6-3 23:17 编辑

这个位置也没用了,可以删除,我好像无法删除
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-3 23:10:00 | 显示全部楼层
本帖最后由 人造人 于 2019-6-3 23:18 编辑

这个位置也没用了,可以删除,我好像无法删除
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-4 11:58:09 | 显示全部楼层
人造人 发表于 2019-6-3 23:10
这个位置也没用了,可以删除,我好像无法删除

我也无法删除,送你上最佳答案吧,谢谢帮忙看帖啦~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 20:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表