C++ compiler error base class issue - c++

I'm getting this->
error -type ‘Ship’ is not a direct base of ‘CruiseShip’-
I cannot figure it out. This is where the error is occurring I am assuming. I am not really sure I guess how I should be calling the base class?
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
//A print function that overrides the print function in the base class.
//The CruiseShip class's print function should display only the ship's
//name and the maximum number of passengers.
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include <string>
using namespace std;
class Ship;
class CruiseShip{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.cpp
#include "Ship.h"
#include <iostream>
using namespace std;
string name;
string built;
Ship::Ship(){
}
Ship::Ship(string n, string b)
{
name = n;
built = b;
}
//accessors and mutators methods
string getName()
{
return name;
}
string getBuilt()
{
return built;
}
//A virtual print function that displays
//the ship's name and the year it was built
void print()
{
cout<<"Name:"<<getName()<<"\nYear built:"<<getBuilt()<<endl;
cout<<"-------------------------"<<endl;
}
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif

You need to derive CruiseShip from Ship:
class CruiseShip : public Ship {
this:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
^^^^^^^^^^^^
is a base class constructor call, but you have not derived CruiseShip from Ship, yet compiler knows that Ship is a class type.

Related

Why is my the second class not inheriting and modifying a method properly?

In the second class I want to only add numbers to string, and I am getting the error "main.cpp:38:19: error: ‘virtual void NumericInput::add(char)’ is private within this context
38 | input->add('1');' for every time I class add for Numeric object. What did I do wrong here, isn't everything already public? Thank you!!
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
class TextInput
{
public:
string s="";
virtual void add(char c)
{
s+=c;
}
string getValue()
{
return s;
}
};
class NumericInput : public TextInput
{
//modified
void add(char c)
{
if(isdigit(c))
{
s+=c;
}
}
};
int main()
{
NumericInput* input = new NumericInput();
input->add('1');
input->add('a');
input->add('0');
cout<<input->getValue();
}
All declarations in a class are private by default if there's no public access specifier. You should also override your virtual function in NumericInput.
Add public before in NumericInput
class NumericInput : public TextInput
{
public:
//modified
void add(char c)
{
if(isdigit(c))
{
s+=c;
}
}
};

class function inaccessible c++(beginner problem)

Hello so i want to create a header file class which name testing and also its cpp but for some reason this is inaccessible i dont know why
testing.h
#ifndef TESTING_H
#define TESTING_H
#include <string>
using namespace std;
class testing
{
string Name;
void printname(string name);
};
#endif
testing.cpp
#include <iostream>
#include "testing.h"
using namespace std;
void testing::printname(string name) // inaccessible in my main i dont know what reason :(
{
Name = name;
cout<<Name<<endl;
}
main
#include <iostream>
#include "testing.h"
using namespace std;
using std::string;
int main()
{
testing tester;
tester.printname("JPR"); //error since testing::printname is inaccessible no idea
return 0;
}
If you don't specify the visibility of the members, they are private.
You can either use a struct (visibility is public):
struct testing
{
string Name;
void printname(string name);
};
or you can specify that printname is public:
class testing
{
public:
void printname(string name);
private:
string Name;
};
Try the following:
testing.h
#ifndef TESTING_H
#define TESTING_H
#include <string>
class testing
{
public:
// Better to pass the parameter as const reference to avoid performing a copy.
void printname(const std::string& name);
private:
std::string Name;
};
#endif
testing.cpp
#include <iostream>
#include "testing.h"
void testing::printname(const std::string& name)
{
Name = name;
std::cout << Name << std::endl;
}
main.cpp
#include "testing.h"
int main()
{
testing tester;
tester.printname("JPR");
return 0;
}
In C++:
A class defined with the keyword class has private access for its members and its base classes by default.
Add public: access modifier to your class defition, to mark method as public:
class testing
{
string Name;
public:
void printname(string name);
};
when you create a class, every member function and member variable is set in default as private, which means that they won't be accessible. To make your function public you need to change this in your code:
class testing
{
private: //is private in default, i add it for better readabilty
string Name;
public:
void printname(string name);
};
Worth to mention that you (almost) ALWAYS want to keep all member variables private!

How to include class which uses base class

The question is hard to ask but I'll try to explain:
I have a class World that import Class Species, and holds list of Species.
I need to have pointer to class World in class Species, but if I import Class World before declaring Species errors occurs.
Example in pseudo code:
import B
Class A
{
list of B
}
Class B
{
Class A* a
}
so I need to import class A to make pointer to it, but when I do Class B is not declared in clas A.
my code:
#pragma once
#include <iostream>
class Species
{
protected:
std::string speciesName;
int strengh;
int effort;
int posX;
int posY;
int specieSymbol;
public:
Species();
Species(std::string speciesName, int strengh, int effort, int posX, int posY);
int getPosX();
int getPosY();
void setPosX(int x);
void setPosY(int y);
int getSpeciesSymbol();
std::string getSpeciesName();
virtual void action() = 0;
virtual void colision() = 0;
//void setWorld(World* w);
//World* getWorld();
~Species();
};
#pragma once
#include <iostream>
#include "Species.h"
#include <list>
#include "Board.h"
class World
{
private:
std::list <Species*> species;
Board* board;
public:
World(Board *board);
void addSpecies(Species* s);
void printSpecies();
void printWorld();
void Update();
~World();
};

C++ program using vector of pointers of inherited classes compile but doesn't run

I have an abstract class called box that only has a string parameter called type and a virtual string that only returns that parameter. This class is the father of another class called Reward that has the constructor that fills the the parameter type and the same function of Box. I'm trying to put them in a class called Board that is basically a vector of the pointers of these classes, I have a constructor that just fills the vector with rewards. The problem comes that I compille it but doesn't run and I cannot find where is the problem. Thanks
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Box{
protected:
string type;
public:
virtual string getType()=0;
};
class Reward : public Box{
public:
string getType(){
return type;
}
Reward(){
type = "Reward";
}
};
class Board{
private:
vector<Box *> Table;
public:
Board(){
for (int i=0;i<=30;i++){
Table[i]=new Reward;
}
}
string getBoxes(int i){
return Table[i]->getType();
}
};
int main(){
Board game;
string toPrint=game.getBoxes(2);
cout<<toPrint;
return 0;
}

Inheritance (through files) unable to find class?

Im trying to use Inheritance through multiple header and cpp files for a text game that I'm writing.
I have my base class of Weapon. Which is in the file Weapon.h
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
I am then trying to inherit form the base Weapon.h class to a Bow and Sword class. I am sure I am including the file correctly but when I try to compile I get the error "error: expected class name class Blade : public Weapon" The same error for the Bow class.
#include "Weapon.h"
#include "Crossbow.h"
using namespace std;
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
#include "Weapon.h"
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
Does anyone know why this is happening? Google isn't coming up for anything useful either. Thanks
MCVE (I think)
//In Base.h
class Base
{
public:
int function();
private:
};
//In Base.cpp
int Base::function()
{
randomshit
return 0;
}
//In Inherit.h
#include "Base.h"
class Inherit : public Base
{
public:
int function():
private:
};
Getting error: "expected class name class Bow : public Weapon"
EDIT: Turns out I needed to include "#pragma once" and that solved almost everything. Thanks for the help guys.
You don't use any include guards, so your file Weapon.h is probably included multiple times, leading to the compilation error.
To know more about include guards: https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Include_Guard_Macro
Your header Weapon.h would then become:
#ifndef WEAPON_H_INCLUDED
#define WEAPON_H_INCLUDED
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
#endif // WEAPON_H_INCLUDED
Do the same for all other header files.
Once you did that, remove all unnecessary includes and do a clean rebuild.
This is maybe not an answer but it just is impossible to post it as comment
This compiles (but does not link !!) on my Visual Studio 2013.
#include <string>
using namespace std;
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
But it is likely to fail on older compilers because of the initialisation in the declaration as string Type = "Melee";.
Note that using namespace std; comes before the declaration of class Weapon.