C++ 为什么不能从其他类的对象调用函数?
我有下面这些作文课。 问题是,在Elephant.cpp中,行:C++ 为什么不能从其他类的对象调用函数?,c++,ios,c++11,C++,Ios,C++11,我有下面这些作文课。 问题是,在Elephant.cpp中,行: cout在cout行中调用的函数的签名是 void eats(); 这意味着它不返回任何值,因此cout没有任何可显示的内容 解决此问题并将ProbleTic功能更改为: void Elephant::shout() { cout<<"Elephant do: \n"; animal.eats(); // has its own cout } void大象::shout() { coutBe
cout在
cout
行中调用的函数的签名是
void eats();
这意味着它不返回任何值,因此cout
没有任何可显示的内容
解决此问题并将ProbleTic功能更改为:
void Elephant::shout()
{
cout<<"Elephant do: \n";
animal.eats(); // has its own cout
}
void大象::shout()
{
coutBecausecout
无法打印void
。请返回一个char*
或std::string
。而且您确实无法将示例最小化一点?单个文件中的10行应该足够了。
#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
void eats();
void Elephant::shout()
{
cout<<"Elephant do: \n";
animal.eats(); // has its own cout
}