0

Apologies if this is below the community's paygrade. I only started learning about OOP and classes last night and English is not my native language.

I am trying to create a class called "Mammal" with its name, type of fur, number of legs and the presence of a tail as properties. This is what my mammal.h looks like.

//mammal.h
#include <string>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


class Mammal
{
    public:
    //Properties
    enum Fur {bald, curly, frizzy, matted, shorthair, longhair, fluffy};
    Fur flooftype;
    int Legs;
    bool Tail;
    string Name;

    //Methods
    Mammal(string Name, Fur flooftype, int Legs, bool Tail);

    
    void identifyMammal(string Name, Fur flooftype, int Legs, bool Tail)
    {
        cout<<"Name: " << Name << endl;
        cout<<"Number of legs: "<< Legs << endl;
        cout<<"Fur type: "<< flooftype << endl;
        if(Tail==true){
            cout<<"This mammal has a tail.";            
        }else{
            cout<<"This mammal has no tail.";
        }
    }
};

Mammal::Mammal(string Name, Fur flooftype, int Legs, bool Tail)
{
    Name=Name;
    flooftype=flooftype;
    Legs=Legs;
    Tail=Tail;
}

And this is my main.cpp:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "mammal.h"
using namespace std;

int main()
{
    Mammal cat1;
    cout << Mammal.identifyMammal() << "." <<endl;
    cout << endl;
    system ("pause");
    return EXIT_SUCCESS;
}

I am unable to get this code to compile, it gives me the following errors:

no default constructor exists for class "Mammal"
type name is not allowed

I'm not sure why it's not recognizing my constructor. Can anyone point me in the right direction?

  • `Mammal cat1;` invokes default constructor, that is constructor taking no arguments. While your constructor requires 4 arguments. – user7860670 Oct 11 '20 at 07:02

4 Answers4

4

You've written a constructor, but your constructor isn't a default constructor. It takes the arguments name, flooftype, Legs, and Tail. To initialize a Mammal using that constructor, you'd write a call like

Mammal cat1("Captain", shorthair, 4, true);

C++ will automatically provide your class with a default constructor (see here, the implicitly-declared and -defined default constructor sections), but only if you don't also write your own constructors. Because you wrote a non-default constructor, you no longer get the implicitly-declared default constructor, and so your class has no default constructor at all. Which means that a call that really needs to go to the default constructor

Mammal cat1;

Is a compile-time error.

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
0

The line Mammal cat1; requires a default constructor because you are passing no arguments to it. Either write a default constructor or pass values for the four-parameter constructor you have written.

jkb
  • 2,376
  • 1
  • 9
  • 12
0
 Mammal cat1;

This line calls default constructor, however you don`t have it because of:

Mammal::Mammal(string Name, Fur flooftype, int Legs, bool Tail)

Change to Mammal cat1("name", matted, 2, true) or add Mammal() = default

Alex
  • 1,047
  • 8
  • 21
0

You're constructor is Mammal(string Name, Fur flooftype, int Legs, bool Tail);
But you're trying to call the Mammal() constructor.

There's a good explanation here.

Obsidian
  • 3,719
  • 8
  • 17
  • 30