中未指定任何成员。为什么会发生这种情况,我该如何解决?
No member named '' in ''. Why did it happen and how do I fix it?
当我在主要打印我的PrintSount()时出现错误。编译器说:
在"动物"中没有名为"PrintSound"的成员)
为什么会发生这种情况,我该如何解决?
主要
#include <iostream>
#include "animal.h"
#include "pat.h"
#include "not_pat.h"
int main()
{
std::string lastName;
std::string sound;
std::string name;
std::string type;
double speed = 0;
animal* animals[2];
for (int i = 0; i < 2; i++)
{
std::string ani;
std::cout << "Enter animal: (dog,fish,lion,monkey ... ): " << std::endl;
std::cin >> ani;
if (ani == "dog" || ani == "cat" || ani == "fish")
{
animals[i] = new pat("test", "test", "test","test");
}
else
{
animals[i] = new not_pat(speed, lastName, "test2", "test2");
}
}
for (int i = 0; i < 2; i++)
{
std::cout << animals[i]->PrintName() << animals[i]->PrintLatName() << animals[i]- >PrintType() << animals[i]->PrintSound() << std::endl;
}
}
动物.H
#ifndef ANIMAL_H
#define ANIMAL_H
#include <iostream>
#include <stdio.h>
#include <string>
class animal{
public:
animal(std::string name, std::string type, std::string lastName);
std::string PrintType();
std::string PrintName();
std::string PrintLatName();
protected:
std::string _name;
std::string _lastName;
std::string _type;
private:
};
#endif
动物.cpp
#include "animal.h"
animal::animal(std::string name , std::string type, std::string lastName)
{
_name = name;
_type = type;
_lastName = lastName;
}
std::string animal::PrintType()
{
return this->_type;
}
std::string animal::PrintName()
{
return this->_name;
}
std::string animal::PrintLatName()
{
if(_lastName == "0")
{
return NULL;
}
else
{
return this->_lastName;
}
}
帕特·
#ifndef PAT_H
#define PAT_H
#include "animal.h"
#include <iostream>
#include <stdio.h>
#include <string>
class pat : public animal
{
public:
pat(std::string lastName, std::string sound, std::string name, std::string type);
std::string PrintSoud(animal *p);
protected:
std::string _sound;
private:
};
#endif
帕特.cpp
#include "pat.h"
#include "animal.h"
pat::pat(std::string lastName, std::string sound, std::string name, std::string type) : animal(name,type,lastName)
{
_sound = sound;
}
std::string pat::PrintSoud(animal *p)
{
return this->_sound;
}
not_pat.h
#ifndef NOT_PAT_H
#define NOT_PAT_H
#include <iostream>
#include <stdio.h>
#include <string>
class not_pat : public animal
{
public:
not_pat(double speed,std::string lastName, std::string name, std::string type);
double PrintSpeed();
protected:
double _speed;
private:
};
#endif
not_pat.cpp
#include "animal.h"
#include "not_pat.h"
not_pat::not_pat(double speed, std::string lastName, std::string name, std::string type) : animal(name, type,lastName)
{
if(speed == 0)
{
_speed = NULL;
}
else
{
_speed = speed;
}
}
double not_pat::PrintSpeed()
{
return this->_speed;
}
C++是一种静态类型的语言。使用pat
使用指向animal
的指针。因此,编译器会检查animal
是否具有成员函数PrintSound()
。它没有它,因此引发了编译错误。您需要将PrintSound
声明添加到animal
(可能是一个纯虚函数)来解决此问题。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- visual c++,如何获取解决方案目录中的代码
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 为什么这段代码不起作用,我该如何解决?