°希作先生丶 发表于 2018-10-7 09:44:38

新人C++问题求助

如题,在定义结构体时遇到了一些问题,错误提示时error: 'void init(<anonymous struct>*)', declared using anonymous type, is used but never defined [-fpermissive]|

claws0n 发表于 2018-10-7 12:18:09

typedef struct xxx{   }结构体变量;

°希作先生丶 发表于 2018-10-7 17:52:57

claws0n 发表于 2018-10-7 12:18
typedef struct xxx{   }结构体变量;

我知道怎么声明普通的结构体,就是想问一下这样声明为什么无法通过

claws0n 发表于 2018-10-7 18:06:12

°希作先生丶 发表于 2018-10-7 17:52
我知道怎么声明普通的结构体,就是想问一下这样声明为什么无法通过

#include <iostream>

typedef struct data
{
        int id;
        char *name;
}Data;

typedef struct staticlinkedlist
{
        Data dt;
        int Cnext;
}StaticLinkedList;

void init(StaticLinkedList tar)
{
        for(int i = 0; i < 0; i++)
                tar.Cnext = i+1;
        tar.Cnext = 0;
        tar.Cnext = 0;
}

void Show(StaticLinkedList tar)
{
        for(int i = 0; i < 10; i++)
                std::cout << tar.Cnext << std::endl;
}

int main()
{
        StaticLinkedList s1;
        init(s1);
        return 0;
}都说了没有结构体名~~~ 然后 struct xxx -> typedef struct xxx XXX 不要用相同的名字

°希作先生丶 发表于 2018-10-7 18:59:20

claws0n 发表于 2018-10-7 18:06
都说了没有结构体名~~~ 然后 struct xxx -> typedef struct xxx XXX 不要用相同的名字

哦天哪可以了,谢谢老哥,还有点小问题就是data结构声明的时候是两个相同的名字为啥没报错- =

claws0n 发表于 2018-10-7 19:10:30

°希作先生丶 发表于 2018-10-7 18:59
哦天哪可以了,谢谢老哥,还有点小问题就是data结构声明的时候是两个相同的名字为啥没报错- =

那个还好,主要的是 StaticLinkedList,会与数组元素冲突,所以一般上都避免

°希作先生丶 发表于 2018-10-7 19:27:46

claws0n 发表于 2018-10-7 19:10
那个还好,主要的是 StaticLinkedList,会与数组元素冲突,所以一般上都避免

可以详细说一下么,初学者比较笨- =

claws0n 发表于 2018-10-7 19:36:20

°希作先生丶 发表于 2018-10-7 19:27
可以详细说一下么,初学者比较笨- =

因为你定义了一个结构体变量 StaticLinkedList,如果你写 StaticLinkedList,会是这个结构体的首地址,冲突了。
typedef struct xxx {}XXX; 等同
struct xxx{};
typedef struct xxx XXX; //这个还是声明的部分不是变量

°希作先生丶 发表于 2018-10-7 19:40:25

claws0n 发表于 2018-10-7 19:36
因为你定义了一个结构体变量 StaticLinkedList,如果你写 StaticLinkedList,会是这个结构体的首地址 ...

也就是说下面的XXX时声明而上面的XXX时定义这个意思么

claws0n 发表于 2018-10-7 19:44:03

°希作先生丶 发表于 2018-10-7 19:40
也就是说下面的XXX时声明而上面的XXX时定义这个意思么

呃,声明是没有内容的
比如说函数的声明
void fun(int);
函数的定义
void fun(int x)
{......}
主要是你声明为结构体数组,那么会同时定义一个结构体数组。尽量避免就对了

°希作先生丶 发表于 2018-10-7 19:51:13

claws0n 发表于 2018-10-7 19:44
呃,声明是没有内容的
比如说函数的声明
void fun(int);


嗯, 大概明白了一点- = 谢谢~
页: [1]
查看完整版本: 新人C++问题求助