为什么要在从其他类的对象调用函数时抱怨
Why cout complaining when calling a function from an object of other class?
我在下面有这些作文课。 问题是,在大象.cpp中,有这样一句话: cout<<"大象做:"<<p>练习62.cpp这是主课
#include < iostream >
#include "Animal.h"
#include "Elephant.h"
int main()
{
Animal an;
Elephant el(an);
el.shout();
}
动物类.cpp:动物类
#include "Animal.h"
#include <iostream>
using namespace std;
Animal::Animal()
{
cout<<"Animal constructor"<<endl;
}
void Animal::eats()
{
cout<<"eats"<<endl;
}
void Animal::sleeps()
{
cout<<"sleeps"<<endl;
}
//Animal.h Animal header
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
Animal();
void eats();
void sleeps();
};
//Elephant.cpp Elephant class definition
#include <iostream>
#include "Elephant.h"
#include "Animal.h"
using namespace std;
Elephant::Elephant(Animal an)
: animal(an)
{
}
void Elephant::shout()
{
cout<<"Elephant do: n"<<animal.eats();
}
大象.h:大象头
#ifndef ELEPHANT_H
#define ELEPHANT_H
#include <iostream>
#include "Animal.h"
using namespace std;
class Elephant
{
public:
Elephant(Animal an);
void shout();
private:
Animal animal;
};
#endif
有人可以解释为什么cout不接受<中的这种函数调用吗>中的这种函数调用吗>
您在cout
行中调用的函数的签名为
void eats();
这意味着它不返回任何值,因此cout
没有要显示的内容。
解决此问题并将概率函数更改为:
void Elephant::shout()
{
cout<<"Elephant do: n";
animal.eats(); // has its own cout
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销