Pretty new to C++, I was given an assignment that is basically about utilizing two different classes, however when creating my header files and c++ files and attempting to compile I get an error that reads no matching function for call to 'Owner::Owner()'. Since I am not super familiar with C++ yet I am assuming that this issue has something to do with my constructors and the way I am trying to call them, my assignment details what I think my issue is about but I am having trouble understanding exactly what needs to be done. I will provide the assignment details on the issue, as well as the code and compilation error below. Sorry for all the information I've just been stuck with this issue for a while and I can't seem to figure out a solution.
Transcribed Error
In constructor 'Dog::Dog(std::__cxx11::string, int)':
Dog.cpp:23:46: error: no matching function for call to 'Owner::Owner()' Dog::Dog(std::string unsetBreed, int unsetAge){
Assignment Details
Now you will write a program that consists of two classes, a Dog class and an Owner class. Their specification is shown in the UML diagram below. Notice that in our design, every Dog has an Owner class member. Class Owner is immutable, as mentioned above. An immutable class is just a class whose members cannot be changed(mutated) after an object was instantiated. Therefore, the Owner class does not have any setter methods. The Owner’s class attributes must be set at the time of creation(in the Owner’s constructor). You will call Owner’s constructor from inside Dog’s constructor. Do not forget to do it inside each constructor in class Dog.
Dog.h File
#ifndef DOG_H_INCLUDED
#define DOG_H_INCLUDED
#include <iostream>
#include "Owner.h"
class Dog {
//-----------------------//
private:
std::string breed;
int age;
Owner owner;
static int dogCount;
//-----------------------//
public:
Dog();
Dog(std::string, int);
std::string getBreed();
int getAge();
void setBreed(std::string);
void setAge(int);
void printDogInfo();
int getDogCount();
};
#endif // DOG_H_INCLUDED
Owner.h File
#ifndef OWNER_H_INCLUDED
#define OWNER_H_INCLUDED
#include <iostream>
class Owner {
//-----------------------//
private:
std::string name;
int age;
//-----------------------//
public:
Owner(std::string, int);
std::string getName();
int getAge();
//-----------------------//
};
#endif // OWNER_H_INCLUDED
Dog.cpp File
#include <iostream>
#include "Owner.cpp"
#include "Owner.h"
#include "Dog.h"
//---------------SETTERS------------------//
void Dog::setBreed(std::string dogBreed){dogBreed = breed;}
void Dog::setAge(int dogAge){dogAge = age;}
//--------------GETTERS------------------//
std::string Dog::getBreed(){return breed;}
int Dog::getAge(){return age;}
int Dog::getDogCount(){return dogCount;}
//--------------OTHERS-------------------//
Dog::Dog(std::string unsetBreed, int unsetAge){
Owner::Owner(std::string unsetName, int unsetOwnerAge);
Dog::setBreed(unsetBreed);
Dog::setAge(unsetAge);
}
void Dog::printDogInfo(){
Dog::getBreed();
Dog::getAge();
}
Owner.cpp File
#include <iostream>
#include "Owner.h"
#include "Dog.h"
//--------------GETTERS------------------//
int Owner::getAge(){return age;}
std::string Owner::getName(){return name;}
//--------------OTHERS-------------------//
Owner::Owner(std::string unsetName, int unsetOwnerAge){
Owner::getName();
Owner::getAge();
}
The problem here is that you don't have a constructor that receives 0 parameters.
You have 2 options:
1 - Define other constructor:
//header file
class Owner {
...
public:
Owner(std::string, int);
Owner();
...
};
//cpp file
...
Owner::Owner(){
name = "Jhon Doe";
age = 18;
}
...
2 - Define default params:
//header file
class Owner {
...
public:
Owner(std::string unsetName = "John Doe", int unsetAge = 18);
...
};
//cpp file
...
Owner::Owner(std::string unsetName = "John Doe", int unsetAge = 18);
Owner::getName();
Owner::getAge();
}
...
Note: I don't know why you are calling getter in the constructors.
Looks like your Owner class dosen't have a default constructor try including Owner::Owner() constructor.
Related
I'm trying to create an entity system.
Each entity has a list of components and each component has a pointer to the parent Entity.
========================Example Code:=========================
---------------Dog.h--------------------
#include <string>
class AnimalCare;
class Dog
{
public:
Dog(AnimalCare* parent);
std::string GetParentName();
void Feed(void*);
private:
AnimalCare* g_parent;
}
----------------------------------------
----------------Dog.cpp-----------------
#include "Dog.h"
Dog::Dog(AnimalCare* parent){
g_parent =parent;
}
void Dog::Feed(void* food){
//TODO: Feeding
}
std::string Dog::GetParentName(){
[1]return parent->GetName();
}
----------------------------------------
---------------AnimalCare.h-------------
#include "Dog.h"
class AnimalCare{
public:
AnimalCare(std::string name);
std::string GetName();
void InitDog();
private:
std:string g_name;
Dog* g_dog;
};
----------------------------------------
--------------AnimalCare.cpp------------
#include "AnimalCare.h"
AnimalCare::AnimalCare(std::string name){
g_name =name;}
std:string AnimalCare::GetName(){
return g_name;}
void AnimalCare::InitDog(){
g_dog = new Dog(this);}
============================================================
[1] -> I'm getting pointer to incomplete class type is not allowed.
I know, it is because AnimalCare class in Dog header is just declared but not defined. ->Is there a way to get around this?<-
You need to add this to Dog.cpp:
#include "AnimalCare.h"
Also, you are missing the ; after your class declaration for Dog, so it should be:
class Dog
{
public:
Dog(AnimalCare* parent);
std::string GetParentName();
void Feed(void*);
private:
AnimalCare* g_parent;
};
I am new to C++ and I'm trying to implement Reflection.
I have a Method class, Field class, and Object class.
Say, that I have an Object of Class Male called Daniel.
The class Male has a method called increaseAge() which increases Daniel's age by 1.
Daniel has an int field - age.
How do I invoke the Method increaseAge() on Daniel.
typedef void(*Func)(Object*);
class Method{
private:
std::string nameMethod;
Func f;
public:
Method(std::string name,Func fun){
nameMethod=name;
f=fun;
};
/*virtual void invoke(Object* const obj);
if Object has method, then invoke method on obj.
else > throw MethodNotFound.
*/
};
#endif /* METHOD_H_ */
and, my main.cpp looks like this:
#include <iostream>
#include <string>
#include "Field.h"
#include "Method.h"
using namespace std;
void increaseAge(Object* obj){
age++;
}
int main(){
Method* m2 = new Method("increaseAge",increaseAge);
Object Daniel = new Male;
m2.invoke(Daniel);
}
I have two clases Pet and Person
Here is the Person.h:
#ifndef PERSON_H
#define PERSON_H
#include <list>
class Pet;
class Person
{
public:
Person();
Person(const char* name);
Person(const Person& orig);
virtual ~Person();
bool adopt(Pet& newPet);
void feedPets();
private:
char* name_;
std::list<Pet> pets_;
};
#endif
And here is the Pet.h
#ifndef PET_H
#define PET_H
#include <list>
#include "Animal.h"
class Person;
class Pet : public Animal
{
public:
Pet();
Pet(const Pet& orig);
virtual ~Pet();
std::list<Pet> multiply(Pet& pet);
private:
std::string name_;
Person* owner_;
};
#endif
The problem that i have is this:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/list.tcc:129: error: invalid use of undefined type `struct Pet'
Person.h:13: error: forward declaration of `struct Pet'
I fixed trying to put this std::list<Pet>* pets_; but when i tried to call list functions always have a link problem. My question is how a have to include a list inside a class that contains objects from another class.
The standard requires that, except where explicitly stated, you use complete types with the library templates. This basically inhibits your design (where each object maintains by value a list of the other type).
You can work around this by using [smart] pointers (either a pointer to the container or container of pointers).
My parent class, Course, has the method addStudent(Student s). My child class, BetterCourse, inherits from Course. Every time I try to run BetterCourse.addStudent(s), I get the following error:
error: no matching function for call to
‘BetterCourse::addStudent(Student (&)())’ note: candidates are: void Course::addStudent(Student)
I understand it's telling me addStudent() hasn't been defined in BetterCourse and that it's recommending I use the one present in the parent class, Course. This has me confused as the whole idea around inheritance is not needing to redefine inherited functions and variables.
Course is as follows:
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
class Course
{
protected:
string id;
string name;
public:
Course();
Course(string id, string name);
void addStudent(Student s);
};
Course::Course()
{
//code
}
Course::Course(string id, string name)
{
//code
}
void Course::addStudent(Student s)
{
//code
}
BetterCourse:
#include <iostream>
#include <string>
#include "Course.h"
using namespace std;
class BetterCourse : public Course
{
public:
BetterCourse(string id, string name) : Course(id,name){};
};
From your error it seems that you for the first time get to the ugliest part of C++.
This:
Student s();
Is function declaration - not object definition. s type is Student (*)() so when you call:
BetterCourse bc;
bc.addStudent(s);
You get your error - you don't have method to add functions returning Student.
Define Student in the following ways:
Student s;
Student s {}; // new C++11 way
Student s = Student(); //
It sounds like you're actually calling the function 'addStudent' with an inappropriate argument. Could you show the code that is actually calling addStudent on the BetterCourse object? It sounds like you're using a reference to the student instead of the student object itself.
you can not call BetterCourse.addStudent(s) you should create an object
BetterCourse obj;
obj.addStudent(s);
should work
If you want to call BetterCourse::addStudent(s) than declare addStudent(s) as static method
When I try the two forms of constructor in classes of derivation hierarchy, the result turns out to be different. Could anybody tell me why? Below are the test code.
//Person.h
#ifndef PERSON_H_
#define PERSON_H_
#include<string>
using std::string;
class Person{
private:
string firstname;
string lastname;
public:
Person(const char *fn="NoName", const char *ln="NoName"); //A
Person(const string &fn, const string &ln);
virtual ~Person(){}
};
class Gunslinger:virtual public Person{
private:
int notchnum;
public:
Gunslinger(const char*f="unknown",const char*n="unknown",int not=0);//B
virtual ~Gunslinger(){}
};
class PokerPlayer:virtual public Person{
public:
PokerPlayer(const char*fn="unknown", const char*ln="unknown");//C;
virtual ~PokerPlayer(){}
};
class BadDude:public Gunslinger,public PokerPlayer{
public:
BadDude(const char*fn="unknown", const char*ln="unknown", int notc=0);//D
};
#endif
//PersonDefinition.cpp
#include"Person.h"
#include<iostream>
#include<cstdlib>
using std::cout;
using std::endl;
using std::cin;
Person::Person(const char*fn, const char*ln):firstname(fn),lastname(ln){
}
Person::Person(const string &fn,const string &ln):firstname(fn),lastname(ln){
}
Gunslinger::Gunslinger(const char*fn,const char*ln, int not):Person(fn,ln),notchnum(not){
}
PokerPlayer::PokerPlayer(const char*fn,const char*ln):Person(fn,ln){
}
BadDude::BadDude(const char*fn, const char*ln, int notc):Person(fn,ln),PokerPlayer(fn, ln),Gunslinger(fn,ln,notc){
}
//PersonTest.cpp
#include<iostream>
#include "Person.h"
int main(){
Person a("Jack","Husain");
PokerPlayer b("Johnson","William",8);
Gunslinger c("Mensly","Sim");
}
So, here is the problem. The program above fails to compile with the default constructor with default value for all argument and throws an error message saying that "expected ',' or '...' before '!' token", but if I replace the default constructor in Line A,B,C,D with the form without argument, the program compiles and run successfully.Could anyone tell me why? Below is the error message.
You did not implement all the constructors. For instance, you declared a constructor PokerPlayer::PokerPlayer(char*, char*) but you are trying to create a PokerPlayer with PokerPlayer b("Johnson","William",8); (i.e. you never declared a constructor which takes the third argument). The declaration you want for that previous line to work is PokerPlayer::PokerPlayer(char*, char*, int);
Furthermore, you have the exact opposite problem when trying to declare a GunSlinger. Your GunSlinger class requires the third parameter and you are trying to declare it without that argument.
Even though your base class supports several types of constructors, each derived class must also have every constructor you wish to use on it explicitly declared/implemented (with the exception of the default constructor).
EDIT
Here is some semi-functional code:
class PokerPlayer : public Person
{
...
PokerPlayer(char* fname, char* lname, int val);
...
};
Implementation
PokerPlayer::PokerPlayer(char* fname, char* lname, int val) : Person(fname, lname, val)
{
// Anything else we should do...
}