📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2864| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[学C/C++] 关于extern的问题 c++

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
// simple.h
#ifndef Simple
#define Simple
#include<iostream>

using namespace std;

class simple
{
    int num;
    static int i;
public:
    simple(){ num=1; i++;cout<<"this is inside the constructor of "<< i <<"
simple"<<endl;}

};

int simple::i=0;

class simple1  //和simple类几乎相同,constructor函数多了个形参 int k,其它一
样
{
    int num;
    static int i;
public:
    simple1(int k):num(k){ i++;cout<<"this is inside the constructor of "
<<
i <<" simple1"<<endl;}
};

int simple1::i=0;
#endif

// another.cpp
#include "exe11.cpp"
#include "simple.h"
using namespace std;
extern simple k;
//extern simple1 s;
extern int a;

void main()
{
    cout<<"a= "<<a<<endl;

}

//exe11.cpp
#include "simple.h"

simple k;
simple1 s(3);
int a=9;

先不考虑绿色字体代码,运行结果是
this is inside the constructor of 1 simple
this is inside the constructor of 1 simple1
this is inside the constructor of 2 simple
a=9

红色代码extern simple k;本意只是想做声明,为了使用在exe.11中已经定义的k,
却
又定义一个simple类k,    并且不与 exe11.cpp中的k发生冲突, 让我很奇怪。 求
解
答。

若考虑绿色部分代码,会有这么一个错误,
error C2512: 'simple1' : no appropriate default constructor available
可我明明也只想声明,不想定义,难道声明的语法不对? 求解答

谢谢!

上一篇:Google: 最大无障碍点子矩阵
下一篇:Google : find the number of ways the students could be ranked
🔗
wwwyhx 2011-7-6 13:56:24 | 只看该作者
全局:
#include "exe11.cpp"??
没见到这么include源文件的,去掉这么一行试试。
extern会在其他的obj文件中找simple k
可是exe11.cpp和another.cpp编译到了一个obj文件
还是,从没有见到过include 源文件的用法
回复

使用道具 举报

🔗
stephenK 2011-7-9 15:01:17 | 只看该作者
全局:
回复 2# wwwyhx

见过一次 include cpp的。。
回复

使用道具 举报

🔗
wwwyhx 2011-7-10 17:24:10 | 只看该作者
全局:
回复  wwwyhx

见过一次 include cpp的。。
stephenK 发表于 2011-7-9 15:01



    可以这么做是没错了,编译器不管后缀名是什么只管贴过来哈
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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