免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4831 | 回复: 4
打印 上一主题 下一主题

[C++] 急问一个双冒号::的用法问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-26 18:28 |只看该作者 |倒序浏览
10可用积分
我有一个.h文件,声明了嵌套类

class A{
    class B{
      B();
      int f();
   };
};

然后在对应的.cpp文件里面如何去实现B的函数呢?

A::B::B(){
}这样实现构造函数,编译报错

int A::B::f(){
}同样报错。

plus: 我不能修改.h的声明。
如何解决这个问题?

最佳答案

查看完整内容

需要加public, 默认是private#include #include class A{public: class B{public: B() { printf("B()\n"); } int f() { printf("f()\n"); return 0; } };};int main(){ A::B b; b.f();}[root@localhst test]# g++ class1.cpp -o class1[root@localhost test]# ./class1B()f()

论坛徽章:
0
2 [报告]
发表于 2010-01-26 18:28 |只看该作者
需要加public, 默认是private

#include <stdlib.h>
#include <stdio.h>

class A{
public:
    class B{
public:
      B()
        {
                printf("B()\n");
        }
      int f()
        {
                printf("f()\n");
                return 0;
        }
   };
};

int main()
{
        A::B b;
        b.f();
}

[root@localhst test]# g++ class1.cpp -o class1
[root@localhost test]# ./class1
B()
f()

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
3 [报告]
发表于 2010-01-26 18:36 |只看该作者

回复 #1 jeanlove 的帖子

报啥错?  我这里什么错都没报……
只报了一个warning, int A::B::f() 没有返回一个值

论坛徽章:
0
4 [报告]
发表于 2010-01-28 01:05 |只看该作者
::
分儿呢?

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
5 [报告]
发表于 2010-01-28 08:50 |只看该作者
楼上说的有道理的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP