错误“没有用于调用的匹配函数”当试图显示 [英] Error "no matching function for call to" when trying to display

查看:149
本文介绍了错误“没有用于调用的匹配函数”当试图显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下类来描述Platypus日期类型,并且在尝试显示结果时遇到问题。这是显示输出时出现问题的主要功能。



I have the following class written to describe a Platypus date type and am having problems when trying to display results. This is the main function where I am having a problem displaying the output.

#include "Platypus.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    Platypus p1('F', 6.5, 2, 'B', true, true);
    Platypus p2('M', 10.0, 3, 'C', true, true);
    p1.display();
    p1.age_me(2);
    p1.display();
    p1.eat();
    p1.display();
    p2.display();
    p2.age_me(3);
    p1.fight(p2);
    p1.display();
    Platypus p3.hatch;
}





我收到的错误是没有匹配函数来调用Platypus :: display()和Platypus类中没有任何函数存在。我真的不知道从哪里开始解决这个问题。我确实有一个头文件,但由于这已经很长了所以我把它留了下来。我对C ++和课程都很陌生,所以任何帮助都会非常感激。





I am getting an error that there is no matching function for call to Platypus::display() and that none of my functions exist in the class Platypus. I don't really know where to start in fixing this. I do have a header file but left it out since this is already decently long. I am fairly new to C++ and classes so any help would be greatly appreciated.

#include <iostream>
#include <cstdlib>
#include <ctime>

#include "Platypus.h"

using namespace std;

#ifndef PLATYPUS_H_INCLUDED
#define PLATYPUS_H_INCLUDED

class Platypus {

    public:

        Platypus::Platypus()
            : weight(0.0), age(0), name('A'), gender('M'), alive(false), mutant(false)        
        {
        }

        Platypus::Platypus(char gender, float weight, short age, char name, bool alive, bool mutant)   
        {
            bool alive = true;
            bool mutant = false;

            myWeight = weight;
            myAge = age;
            myName = name;
            myGender = gender;
            myAlive = alive;
            myMutant = mutant;
        }

        unsigned Platypus::getWeight() const; 
        {
            return myWeight;
        }
        unsigned Platypus::getAge() const;     
        {
            return myAge;
        }
        unsigned Platypus::getName() const;   
        {
            return myName;
        }
        unsigned Platypus::getGender() const;  
        {
            return myGender;
        }
        unsigned Platypus::getAlive() const;    
        {
            return myAlive;
        }
        unsigned Platypus::getMutant() const;  
        {
            return myMutant;
        }

        void Platypus::display(ostream & out) const
        {
            out << "Name is" << myName << endl
                << "Gender is" << myGender << endl
                << "Weight is" << myWeight << "lbs" << endl
                << "Age is" << myAge << "months" << endl;

            if (myAlive == true) {
                out << "Alive: "
                    << "Yes"
                    << endl;
            }
            else {
                out << "Alive: "
                    << "No"
                    << endl;
            }
            if (myMutant == true) {
                out << "Mutant : "
                    << "Yes"
                    << endl;
            }
            else {
                out << "Mutant: "
                    << "No"
                    << endl;
            }
        }

        void age_me(short inc)
        {
            myAge += inc;

            srand(time(NULL));
            int rand_mut = (rand() % 100) + 1;

            if(rand_mut <= 2)
            {
                myMutant = true;
            }

            if(rand_mut <= 10 * weight)
            {
                myAlive = false;
            }
            if(weight >= 10)
            {
                myAlive = false;
            }
        }

        void fight(Platypus & p)
        {
            srand(time(NULL));                                                      
            int fight_ratio = (getWeight() / p.getWeight()) * 50;       
            int random_ratio =  (rand() % 100) + 1;                     

            if(random_ratio < fight_ratio)                              
                p.setmyAlive = false;
            {
            }
            else                                                        
            {
                myAlive = false;
            }
        }

        void eat()
        {
            if(myAlive == true)
            {
                float rand_weight = 5.0 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (0.5 - 5)));  
                weight = weight + rand_weight;    
            }

        }

        void hatch()
        {
            srand(time(NULL));

            char newName = 97 + rand() % 26;  

            alive = true;    //alive defaulted to true
            mutant = false;
            age = 0;

            int rand_gender = rand() % 50 + 1;    
            if(rand_gender <= 25)    
            {
                gender = 'M';
            }
            else
            {
                gender = 'F';
            }

            float newrand_weight = 1.0 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (0.5 - 1)));   
            weight = newRand_weight;  

            char newName = 97 + rand() % 26;  
            name = newName;
        }
}
#endif





我尝试过:



在display



What I have tried:

Passing p1 in the parameters of display

推荐答案

的参数中传递p1我看到了一些错误。第一个是显示方法的原型是:

I see a couple of errors. The first is the prototype to the display method is :
void Platypus::display(ostream & out) const



你打电话给它:


and your call to it is this :

p1.display();



你需要传递一个ostream,比如cout。



另一个是一个更大的问题。头文件真的搞砸了。头文件顶部的东西(也就是我认为它是一个头文件 - 第二个列表)应该位于主模块的顶部。


You need to pass it an ostream, like cout.

The other is a bigger problem. The header file is really messed up. The stuff at the top of the header file (that is, I think it is a header file - the second listing) should be at the top of the main module.

#include <iostream>
#include <cstdlib>
#include <ctime>

#include "Platypus.h"

using namespace std;

int main()
{
// ...
}





头文件应该看起来像这样:



The header file should look like this:

#ifndef PLATYPUS_H_INCLUDED
#define PLATYPUS_H_INCLUDED

class Platypus
{
public:
    Platypus()
    : weight(0.0)
    , age(0)
    , name('A')
    , gender('M')
    , alive(false)
    , mutant(false)        
    {
    }

    unsigned getWeight() const; 
    {
       return myWeight;
    }
};

#endif



因为你在头文件中有实现,所以在类定义中,您不要将类名和冒号放在每个方法的前面。这只是在一个单独的实现源文件中完成的,通常在这个类中称为Platypus.cpp。



我展示了应该如何定义两个方法,但它们都应该遵循这种模式

在头文件中的每个方法名称之前省略Platypus ::。


Because you have the implementation in the header file, within the class definition, you don't put the class name and colons in front of every method. That is done only in a separate implementation source file, typically called Platypus.cpp for this class.

I showed how just two methods should be defined but they should all follow this pattern
which is to omit the "Platypus::" before each method name in the header file.


这篇关于错误“没有用于调用的匹配函数”当试图显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆