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

运行出现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日