You are here:

C++/deitel c++


im writing a program trying learn c++ and i keep getting the error message, no appropriate constructor available.  can you help me understand what im doing wrong?  the purpose of this project is to:

For this assignment, you will implement a class, CPet, which will represent a pet. in so many words i need to prompt the user to give the pet name, the pet sound and make it sound 3 times.
this is what i have so far:

// this is the CPet.h file
#include <string>
using namespace std;

// Pet class definition
class Pet
{ // start Pet class
  Pet( string );
  void setPetType(string petTypeName);
  string getPetType();
  void displayMessage();

  string PetType; // Name for this pet
};// end Pet class


// this is my pet.cpp file
#include <iostream>
#include "Pet.h"  // include definition of class Pet
using namespace std;

Pet::Pet( string petTypeName )
  setPetType( petTypeName);
} // end Pet constructor

// function to set petTypeName
void Pet::setPetType(std::string petTypeName)
  PetType = petTypeName;

string Pet::getPetType()
  return PetType;
void Pet::displayMessage()
  cout << "Your pet is a\n" << getPetType()
     << "!" << endl;
} // end function displayMessage()

//this is my main.cpp file
#include <iostream>
#include "Pet.h"
using namespace std;

// functionmain begins program execution
int main()
  string nameOfPet;  
  Pet myPet;

  cout << "Your current pet type is: " << myPet.getPetType()
     << endl;

  cout << "\nPlease enter the type of pet: " << endl;
  getline( cin, nameOfPet );
  myPet.setPetType( nameOfPet );

  cout << endl;

Hi jwilson

In your main program, you have the line
Pet myPet;

That line is trying to make a Pet but it is not providing any parameters. The only constructor available to Pet is
Pet::Pet( string petTypeName )
which takes a string.

You should create another constructor in Pet which takes no arguments, and still initializes the Pet to something. For example

The call to setPetType("") above is optional because the string class has its own default constructor which sets string objects to be empty.

If you don't provide any constructor, C++ will provide a default no-argument constructor which does nothing. As soon as you provide any constructor, C++ stops giving you the default. Your program may have compiled in the past, then stopped compiling when you added a constructor and C++ stopped giving you the default.

I hope that explains things.
Best regards


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2017 All rights reserved.

[an error occurred while processing this directive]