C++ Inheritance - Running Parent Method in Child Class - c++

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

Related

trouble with accessing a class from a different class

just started to learn c++.I'm trying new things in c++ on thing i wanted to try is to access a class from another class and change its instances and print its instance on screen.
I would like to know 2 things 1)whats wrong with my code 2)where should i declare class declarations (in main file or class definition file?)
here is the error log -
'object::carrier' uses undefined class 'sub'
'cout': is not a member of 'std'
'cout': undeclared identifier
this is what i came up with-
source.h
#include <iostream>
#include <vector>
#include "stuff.h"
int main()
{
object spoon(3);
spoon.get();
}
stuff.cpp
#pragma once
#include <vector>
class object;
class sub;
class object
{
private:
std::vector <sub> thing;
public:
object(int n);
void get() const;
};
class sub
{
private:
int num;
public:
void set_num(int n);
};
stuff.cpp
#include <vector>
#include "stuff.h"
// methods for object
object::object(int n)
{
sub carrier;
carrier.set_num(n);
}
void object::get() const
{
std::cout << carrier.num;
}
// methods for sub
void sub::set_num(int temp_num)
{
num = temp_num;
}
thanks
In your object class, specifically object::get definitions, you use the variable carrier without it being in scope.
When you declare the variable sub carrier in your constructor, it is only accessible in the same scope, that is, inside the constructor. Once your program leaves the scope, the variable carrier is deallocated (deleted).
You must add the variable sub carrier as a member to your class like so:
class object
{
private:
sub carrier
// other stuff
}
Edit:
I so you edited your question.
You must either replace cout with std::cout because cout is part of the c++ standard library. Alternatively, a less verbose option would be to add using namespace std; at the top of every .cpp file. This basically tells the compiler that you can use the namespace std without explicitly saying it. But don't do it for .h files. It's not a good idea.

Why am I getting a "no matching function" error?

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.

Need help printing out a string

Im currently learning about classes in C++. Im coming from a java language background.
I have a class called animals. the animals constructor has a parameter in which a string is required. When i create an object of that class and try to print out the string that was passed to the constructor, it doesn't print out anything apart from a new line..
ANIMAL CLASS
#include "animal.h"
#include <string>
#include <iostream>
using namespace std;
animal::animal(string nameofanimal)
{
string name = nameofanimal;
}
void animal::getName(){
cout << name << endl;
}
ANIMAL HEADER:
#pragma once
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
#include <iostream>
using namespace std;
class animal
{
private:
string name;
public:
animal(string x);
void getName();
};
#endif ANIMAL_H
MAIN CLASS
#include "animal.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
animal firstAnimal("Bob");
firstAnimal.getName();
}
the output is nothing. this is a very basic code and honestly cant believe I don't know how to do such a simple task. What I have noticed is that whenever I highlight the name varible in the animals constructor, the varible name in the getName() function doesn't highlight so im guessing this has something to do with pointers... I may be wrong though...
Change your animal constructor definition as follows
animal::animal(string nameofanimal) : name(nameofanimal) {
}
In your original definiton you have a local variable name that shadows the class member variable:
animal::animal(string nameofanimal)
{
string name = nameofanimal; // <<< This sets only the local variable
}
The problem is you are storing value in local variable
string name = nameofanimal;
while the value is not stored in actual data member of the class. To store it in data member use the following:
name = nameofanimal;

C++: Error with 'this' keyword

I am attempting to create an poptart vending machine program within c++ and i am trying to use the this keyword, however i am always getting an error stating 'this' may only be used inside a nonstatic member function. Below is a part of the code that i am getting one such issue in
Code:
#include <iostream>
#include "HasCredit.h"
using namespace std;
void insertMoney(int money)
{
cout<<"You inserted: " << money;
money = money+this->currentContext->getStateParam(Credit);
this->currentContext->setStateParam(Credit,money);
cout<< "Total: "<<money<<endl;
this->currentContext->setState(Has_Credit);
}
Any suggestions onto why i am getting this error will be most appreciated. Cheers.
Edit: the insertMoney method is within a class called HasCredit.
Edit2: member declarations are now made outside of the constructor
Edit3: Added state class declaration
The Class Definition Code is Below:
#include <iostream>
#include "State.h"
#include "StateContext.h"
using namespace std;
class HasCredit: public State
{
HasCredit (StateContext* Context) : State(Context) {
}
void insertMoney(int);
void MakeSelectionCoating(int);
void MakeSelectionFilling(int);
void moneyRejected(void);
void addPopTarts(int);
void dispense(void);
};
The state class declaration code is shown Below:
#include <iostream>
#include "Transition.h"
using namespace std;
class State: public Transition
{
protected:
StateContext* currentContext;
public:
State(StateContext* Context);
};
The this pointer is only valid inside a class. Your insertMoney function is not declared to be in a class. See http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/.
In your definiton of insertMoney (in the code, not the class), you do not declare it to be a member of hasCredit. You need to use void hasCredit::insertMoney instead.
You probably want to attach insertMoney to a class. Try ClassName::insertMoney in your definition.
The error is telling you exactly why you're getting it.
The solution is to fix the definition of what you intend to be a member function, so that the compiler knows it is a member function. Like this:
// vv THIS WAS MISSING
void HasCredit::insertMoney(int money)
{
...
Your member declarations shown in the question are also in the wrong place. They need to be inside the class body, but outside the constructor. When overriding virtual member functions, you may want to show that to readers by using the virtual and override keywords. (Note, override only works if you have a new, C++11 compiler. For older compilers, use a comment /* override */ instead)
class HasCredit: public State
{
HasCredit (StateContext* Context) : State(Context) { }
// vv THIS....................CANNOT BE INSIDE HERE ^^
virtual void insertMoney(int) override;
...
};
You probably want some of your members to be public as well.
There are a few things wrong:
class definition:
#include <iostream>
#include "State.h"
#include "StateContext.h"
using namespace std;
class HasCredit: public State
{
HasCredit (StateContext* Context) : State(Context) {
}
void insertMoney(int);
void MakeSelectionCoating(int);
void MakeSelectionFilling(int);
void moneyRejected(void);
void addPopTarts(int);
void dispense(void);
};
Note that the methods should be declared outside of the constructor definition.
For the implemenation:
#include <iostream>
#include "HasCredit.h"
using namespace std;
void HasCredit::insertMoney(int money)
{
cout<<"You inserted: " << money;
money = money+this->currentContext->getStateParam(Credit);
this->currentContext->setStateParam(Credit,money);
cout<< "Total: "<<money<<endl;
this->currentContext->setState(Has_Credit);
}
The method must be qualified with the name of the class... otherwise it would be considered a definition of a function... and a function does not have a this

C++: Two classes referencing each other

So to toy with friend functions, I decided to make a Child class and a Mother class. The Mother class has a data member that is a Child. The Child class makes two methods of the Mother class friend functions.
When I compile though, it seems that no matter how I handle inclusions I end up with an error. If the Child is the first one to get defined, I get "Mother is not a class or namespace name" for the line friend void Mother::setChildName(string name); in Child.h. If the Mother is the first one to get defined, I get "Missing type specifier" for the line Child c; in Mother.h.
Is there a way around this? I tried putting class Mother; at the top of the Child.h and class Child; at the top of Mother.h and that didn't seem to help.
Or is this kind of circular reference just always going to fail?
In Mother.h:
#ifndef MOTHER_H_
#define MOTHER_H_
#include <string>
#include "Child.h"
using namespace std;
class Mother {
public:
Mother();
void setChildName(string name);
string getChildName();
private:
Child c;
};
#endif
In Mother.cpp:
#include <string>
#include "Mother.h"
using namespace std;
void Mother::setChildName(string name) {
c.name = name;
}
string Mother::getChildName() {
return c.name;
}
In Child.h:
#ifndef CHILD_H_
#define CHILD_H_
#include <string>
#include "Mother.h"
using namespace std;
class Child {
public:
private:
string name;
friend void Mother::setChildName(string name);
friend string Mother::getChildName();
};
#endif
This particular problem cannot be solved without redesign. Mother needs to be defined before Child, and Child needs to be defined before Mother. The simple way is to make the whole Mother class a friend of Child. That way Child can be defined first.
I think there little practical benefit in making individual methods friends of another class. That would imply that your class is so big that it's responsibilities could be divided into smaller classes.
if Mother will hold pointer to child, you will not have to include child.h. forward deceleration will be enough.