一个异常奇怪的问题,望指点???

yuzhenting 2005-06-04 01:29:43
我在vc++中首先建了一个工程,并分别编辑了两的文件:1.cpp与2.cpp程序如下:
1.cpp:
#include<iostream.h>
class point
{private:
int x,y;
public:
void setpoint(int a,int b)
{x=a;y=b;}
int getx()
{return x;}
int gety()
{return y;}

};
void afs()
{
point a;
a.setpoint(1,2);
cout<<a.getx()<<endl;
}

2.cpp:
#include<iostream.h>
class point
{private:
int x,y;
public:
void setpoint(int a,int b)
{x=a;y=b;}
int getx()
{return x;}
int gety()
{return y;}

};
void afs()
{
cout<<"asd"<<endl;
}
void main()
{
afs();
point b;
b.setpoint(3,4);
cout<<b.getx()<<endl;
}
在程序运行时两个文件中定义的void afs()不但不会产生冲突,反而main()中的afs();语句执行的是1.cpp文件中的void afs(),不知道这是为什么?望高手指点
...全文
68 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tian_su 2005-06-04
  • 打赏
  • 举报
回复
奇了,
我用VC6.0试了一下
会出错啊
提示afx()已经定义过了

2.obj : error LNK2005: "void __cdecl afs(void)" (?afs@@YAXXZ) already defined in 1.obj
naturemickey 2005-06-04
  • 打赏
  • 举报
回复
我用的是VC6.0,没有问题,
main中的afs是2.cpp中的。
MagicCarmack 2005-06-04
  • 打赏
  • 举报
回复
程序应该没有问题吧
tongyongmc 2005-06-04
  • 打赏
  • 举报
回复
不懂,
2.cpp中根本没有用到
#include 1.cpp
又怎么会执行1.cpp中的void afs() ??

是不是哪里弄错了?
nicknide 2005-06-04
  • 打赏
  • 举报
回复
记得
void main(...)是错误的,必须是int main(...)


还有你说的那个问题
因为没有#include ,自然你1.cpp中的afs在第二个cpp中不可见,所以当然没有问题。
eric8231 2005-06-04
  • 打赏
  • 举报
回复
可能是BUG吧,用的VC6?

64,633

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧