为什么连编译都通不过????

LuoQS1 2002-01-14 03:31:35
#include "stdafx.h"
#include<iostream.h>
class AAA
{
public:
void MyCout()
{
cout<<"AAA::none\n";
}
void MyCout(int i)
{
cout<<"AAA::int"<<i<<endl;
}
};
class BBB
{
public:
void MyCout()
{
cout<<"BBB::none\n";
}
};
int main(int argc, char* argv[])
{
int j=10;
BBB test;
test.MyCout(j);
return 0;
}

...全文
1336 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
dxml 2002-01-18
  • 打赏
  • 举报
回复
将main()改为

int main(int argc, char* argv[])
{
int j=10;
BBB test;
AAA* ptest = &test;
ptest->MyCout(j);
return 0;
}



int main(int argc, char* argv[])
{
int j=10;
BBB test;
static_cast<AAA>(test).MyCout(j);
return 0;
}

均可编译

由那位大哥能说明一下为什么???

cBeginner 2002-01-17
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include<iostream.h>
class AAA
{
public:
void MyCout()
{
cout<<"AAA::none\n";
}
void MyCout(int i)
{
cout<<"AAA::int"<<i<<endl;
}
};
class BBB
{
public:
void MyCout()
{
cout<<"BBB::none\n";
}
};
int main(int argc, char* argv[])
{
int j=10;
BBB test; // 应该改成 AAA test;
test.MyCout(j);
return 0;
}

因为BBB类里没有宣告void MyCout(int i),而是在AAA里宣告的void MyCout(int i)
yininvar 2002-01-17
  • 打赏
  • 举报
回复
程序没有入口嘛!
feelinn 2002-01-17
  • 打赏
  • 举报
回复
用Virtual行不?
llmcw 2002-01-17
  • 打赏
  • 举报
回复
同意楼上的。
tang1975 2002-01-16
  • 打赏
  • 举报
回复
#include<iostream.h>
不应该用了吧?
foxmike 2002-01-15
  • 打赏
  • 举报
回复
我觉得scklotz(晓春)等说的是正确的。
btw,你们是不是也在看《Inside the c++》?

其实这样一种情况与java有点不一样,在java中
子类只会覆盖父类中完全同名,同参数的方法,其他同名,不同参数的方法,
一样保留,可以调用,与这里的情况有点不同。(我个人觉得c++的不好,起码是一致性不好。)
mahongxi 2002-01-15
  • 打赏
  • 举报
回复
在CWND派生类中使用:
MessageBox(NULL,"Hello","",MB_OK);
也会出错,它并没有调用全局函数MessageBox,应改为:
::MessageBox(NULL,"Hello","",MB_OK);
与你的情况一个道理。

我觉得似乎有一种把函数决议为最内层次的意思。
mistsoft 2002-01-15
  • 打赏
  • 举报
回复
进行如下改动后,方可编译通过.不过如果在开发中要这么做的话,最好将MyCout声明为Virtual.
另外,我觉得这是一个正常的现象,函数的重载应该有一定的范围限制,它不应超出自身类的范围.
还有,如果你把BBB中的MyCout去掉,则也可编译通过.
#include "stdafx.h"
#include<iostream.h>
class AAA
{
public:
void MyCout()
{
cout<<"AAA::none\n";
}
void MyCout(int i)
{
cout<<"AAA::int"<<i<<endl;
}
};
class BBB:public AAA //必需从AAA继承
{
public:
void MyCout()
{
cout<<"BBB::none\n";
}
void MyCout(int i) //必需显性定义明
{
AAA::MyCout(i);
}
};
int main(int argc, char* argv[])
{
int j=10;
BBB test;
test.MyCout(j);
return 0;
}
oldcat0076 2002-01-15
  • 打赏
  • 举报
回复
你的程序写得有问题,如果BBB是从AAA继承就应该这样定义BBB:class BBB:public AAA;如果BBB不是继承自AAA,则BBB的MyCout()方法就应该带一个参数,或者调用的时候不带参数。
LuoQS1 2002-01-14
  • 打赏
  • 举报
回复
给分
LuoQS1 2002-01-14
  • 打赏
  • 举报
回复
谢谢cccc2002(cccc);
cccc2002 2002-01-14
  • 打赏
  • 举报
回复
每当派生类有某个member与其基类的member同名时,便会遮蔽住基类的那份member,也就是说,派生类对该名称的任何运用,都会被决议为该派生类自身的那份member,而非继承而来的那份member。这种情况下,如果要在派生类中使用继承而来的那份member ,必需利用class scope运算符加以修饰

---Stanley B.Lippan《Essential C++》(侯译本 P152)
chen_jun_fen 2002-01-14
  • 打赏
  • 举报
回复
#include <iostream.h>

class AAA
{
public:
void MyCout()
{
cout<<"AAA::none\n";
}
void MyCout(int i)
{
cout<<"AAA::int"<<i<<endl;
}
};
class BBB
{
public:
void MyCout()
{
cout<<"BBB::none\n";
}
};
int main(int argc, char* argv[])
{
int j=10;
BBB test;
test.MyCout();
return 0;
}


chen_jun_fen 2002-01-14
  • 打赏
  • 举报
回复
#include <iostream.h>

class AAA
{
public:
void MyCout()
{
cout<<"AAA::none\n";
}
void MyCout(int i)
{
cout<<"AAA::int"<<i<<endl;
}
};
class BBB
{
public:
void MyCout()
{
cout<<"BBB::none\n";
}
};
int main(int argc, char* argv[])
{
int j=10;
BBB test;
test.MyCout();
return 0;
}


LuoQS1 2002-01-14
  • 打赏
  • 举报
回复
关注关注
LuoQS1 2002-01-14
  • 打赏
  • 举报
回复
to scklotz(晓春):
BBB 把名字 MyCount 覆盖了基础类(AAA)的 MyCount
这个时候,AAA::MyCount 就无效了。这是 C++ 的规则。
你在那个地方看见类似这句话的,能不能介绍给我???
scklotz 2002-01-14
  • 打赏
  • 举报
回复
问的有深度。

BBB 把名字 MyCount 覆盖了基础类(AAA)的 MyCount
这个时候,AAA::MyCount 就无效了。这是 C++ 的规则。

如果你一定要用,你可以这么做
class BBB ......
{
...

void MyCount( int i )
{
AAA::MyCount( i );
}

...
};

根据内联原则,实际上没有带来额外的编码。
LuoQS1 2002-01-14
  • 打赏
  • 举报
回复
to bskay(bskay):
怎样理解这样的现象,算不算编译器的一个BUG
bskay 2002-01-14
  • 打赏
  • 举报
回复
或者去了BBB中的

class BBB:public AAA
{
public:
//void MyCout()
//{
//cout<<"BBB::none\n";
//}
};
加载更多回复(9)

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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