中未指定任何成员。为什么会发生这种情况,我该如何解决?

No member named '' in ''. Why did it happen and how do I fix it?

本文关键字:何解决 解决 情况 成员 任何 未指定 为什么      更新时间:2023-10-16

当我在主要打印我的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(可能是一个纯虚函数)来解决此问题。