C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用可选的’struct’关键字时c – g警告大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我写这个程序:
#include <iostream>

namespace foo {
    struct bar {
        int x;
    };
}

int main (void) {
    struct foo::bar *a = new struct foo::bar;
    delete a;
    return 0;
}

并编译:

g++ main.cxx -Wall -Wextra

它给我这个警告:

main.cxx: In function ‘int main()’:
main.cxx:10:39: warning: declaration ‘struct foo::bar’ does not declare anything [enabled by default]

但是,如果我在new关键字后面取出了struct关键字:

#include <iostream>

namespace foo {
    struct bar {
        int x;
    };
}

int main (void) {
    struct foo::bar *a = new foo::bar;
    delete a;
    return 0;
}

并以相同的方式编译,g不输出警告.为什么g输出警告如果我使用struct关键字?

解决方法

在C中,struct关键字定义一个类型,新类型不再需要struct关键字.这是C和C之间的许多区别之一.

大佬总结

以上是大佬教程为你收集整理的使用可选的’struct’关键字时c – g警告全部内容,希望文章能够帮你解决使用可选的’struct’关键字时c – g警告所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: