代码什么的要哭了 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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)