为什么这个C ++代码提供编译错误? [英] Why doesn't this C++ code give compilation error?

查看:130
本文介绍了为什么这个C ++代码提供编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include< iostream> 
using namespace std;

struct a {
int e;
struct abc * d;
};

struct abc {
int c;
};

int main()
{
return 0;
}

我已经定义了 struct abc 定义 struct a 后,我已经声明了 abc 的结构指针。这应该抛出编译错误,因为在它的声明之前使用 abc 。但是,它不,为什么?
当我替换为 struct abc d 而不是 struct abc * d

pre





解决方案

struct abc * d;

一方面声明 struct abc 另一方面声明 struct abc * 类型的指针 d



在这个声明中,没有必要使用struct abc的精确定义,因为没有使用结构的数据成员。



此说明符 struct abc 被称为精心设计的类型说明符。



它在给定范围中引入了一个新类型,或引用了一个已声明的类型。


#include<iostream>
using namespace std;

struct a{
    int e;
    struct abc *d;
};

struct abc{
    int c;
};

int main()
{
 return 0;  
}

I have defined the struct abc after definition of struct a in which i have declared a structure pointer for abc. This is supposed to throw compilation error because abc is used before its declaration. But, it doesn't, why? Whereas when i replace it with just struct abc d instead of struct abc *d, it is giving compilation error as expected.

解决方案

This declaration

struct abc *d;

on the one hand declares struct abc and on the other hand declares pointer d of type struct abc *.

In this declaration there is no need to have the exact definition of struct abc because no data member of the structure is used.

This specifier struct abc is called elaborated type specifier.

It introduces a new type in the given scope or refers to an already declared type.

这篇关于为什么这个C ++代码提供编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆