代码什么的要哭了 2022-04-26 19:49 采纳率: 67.9%
浏览 106
已结题

运行出现error C2872: 'cout' : ambiguous symbol该怎么解决

源代码如图


/*定义一个Animal类,
属性: weight,count
访问控制权限为保护类型
其中count为静态数据成员,用来记录animal的个数。

成员函数:
getCount() 用于获得属性count的值
void spark() 为纯虚函数

定义一个Dog类,以public方式继承Animal类
重写spark()函数,用于输出the dog  is wa wa wa!

定义一个Cat类,以public方式继承Animal类
重写spark()函数,输出the cat  is miao miao!

定义一个全局函数totalWeight(),用于计算一个Dog对象和一个Cat对象的重量和

要求程序运行结果为
the dog is wa wa wa
the cat is miao miao
共有 2 个动物
动物共 5 kg*/

//StudybarCommentBegin
//#include "iostream.h"
//补充待添加的代码,即4个类的定义,1个全局函数
//StudybarCommentEnd

#include <iostream>
using namespace std;
class Animal
{
protected:
    int weight;
    static int count;
public:
    Animal(){count ++;}
    static int getCount()
    {
        return count; 
    }
    virtual void spark()=0;
};
class Dog:public Animal
{
public:
    Dog(int w2){weight = w2;}
    int getWeight()
    {
        return weight;
    }
    virtual void spark(){cout << "the dog is wa wa wa" << endl;} 
};
class Cat:public Animal
{
public:
    Cat(int w1){weight = w1;}
    int getWeight()
    {
        return weight;
    }
    virtual void spark(){cout << "the cat is miao miao" << endl;}
};

int totalWeight(Dog &t, Cat &p )
{
    return t.getWeight()+p.getWeight();
}
//StudybarCommentBegin
int main()
{
    Dog d(2);
    d.spark();
    Cat c(3);
    c.spark();
    cout<<"共有 "<<c.getCount()<<" 个动物"<<endl;
    cout<<"动物共 "<<totalWeight(d,c)<<" kg"<<endl;
}

//StudybarCommentEnd
  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月26日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分