c++ - No member named '' in ''. Why did it happen and how do I fix it? -
i have error when print printsount() in main. compiler said:
no member named 'printsound' in 'animal')
why happening , how can fix it?
main
#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]; (int = 0; < 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"); } } (int = 0; < 2; i++) { std::cout << animals[i]->printname() << animals[i]->printlatname() << animals[i]- >printtype() << animals[i]->printsound() << std::endl; } }
animal.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
animal.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; } }
pat.h
#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
pat.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++ statically-typed language. use pat
using pointer animal
. compiler checks if animal
has member function printsound()
. not have it, there compilation error raised. need add declaration of printsound
animal
(probably pure virtual function) fix this.
Comments
Post a Comment