C++ Polymorphism error: no matching function for call to - c++

I'm trying to pass a child object to a function that accepts its parent object with the code:
Rook bRookL();
board[0][0].setPieceObj(bRookL);
and I get the following errors:
ChessBoard.cpp:16:35: error: no matching function for call to 'Space::setPieceObj(Rook (&)())'
board[0][0].setPieceObj(bRookL);
^
In file included from ChessBoard.h:13:0,
from ChessBoard.cpp:5:
Space.h:62:10: note: candidate: void Space::setPieceObj(Piece&)
void setPieceObj(Piece &);
^
Space.h:62:10: note: no known conversion for argument 1 from 'Rook()' to 'Piece&'
Here is my code:
ChessBoard.h:
#ifndef CHESSBOARD_H
#define CHESSBOARD_H
#include "Space.h"
using namespace std;
class ChessBoard {
private:
Space board[8][8]; // board[0][0] is the upper-left corner of the board and
// board[7][7] is the lower-right corner of the board
public:
ChessBoard();
};
#endif /* CHESSBOARD_H */
ChessBoard.cpp:
#include "ChessBoard.h"
#include "Space.h"
#include "Rook.h"
using namespace std;
ChessBoard::ChessBoard() {
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
}
Rook.h:
#ifndef ROOK_H
#define ROOK_H
#include "Piece.h"
using namespace std;
class Rook : public Piece {
public:
Rook() : Piece() {
}
};
#endif /* ROOK_H */
Piece.h:
#ifndef PIECE_H
#define PIECE_H
using namespace std;
class Piece {
public:
Piece() {
}
};
#endif /* PIECE_H */
Space.h:
#ifndef SPACE_H
#define SPACE_H
#include "Piece.h"
#include "Rook.h"
using namespace std;
class Space {
private:
Piece *pieceObj;
public:
void setPieceObj(Piece &);
};
#endif /* SPACE_H */
Space.cpp:
#include "Space.h"
using namespace std;
Space::Space() {
}
void Space::setPieceObj(Piece &p) {
pieceObj = p;
}
Please help. Thanks.

It appears, based on the examination of your code, that setPieceObj() should probably take a pointer argument:
void Space::setPieceObj(Piece *p) {
pieceObj = p;
}
That's one problem. Now, you need to pass a pointer to a Piece object.
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
That's a most vexing parse, that declares a function that returns a Rook, and passes a pointer to it, to setPieceObj().
There's not enough information to determine your clear intent. Perhaps:
Rook bRook;
board[0][0].setPieceObj(&bRookL);

Rook bRookL();
board[0][0].setPieceObj(&bRookL);
1.Rook bRookL(); doesn't do what you expect, it's a declaration of function , which takes no parameters and returns Rook, i.e. Rook (*)() (just as compiler told you).
2.&bRookL will return the address of bRookL, so it will be a pointer. But Space::setPieceObj(Piece &p) expects its paramter to be Piece&. It seems you should change the parameter type to Piece* to make them consistent.
Then
Rook bRookL;
board[0][0].setPieceObj(&bRookL);

Pointer (&bRookL) can't be cast to reference (Piece&).
Most likely solution is in declaration of setPiece that should take pointer:
void setPieceObj(Piece*);
Which should actually make definition to be compilable too:
void Space::setPieceObj(Piece* p) {
pieceObj = p;
}

Related

C++ Class inheritance in different files

I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.
And I get this type of error :
include\Card.h|6|error: expected class-name before '{' token|
BANCNOTE.H
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>
#include "Card.h"
using namespace std;
class Bancnote
{
public:
Bancnote();
Bancnote(string, int ,int ,int );
~Bancnote( );
int getsumacash( );
void setsumacash( int );
int getsumaplata( );
void setsumaplata( int );
int getrest( );
void setrest( int );
string getnume( );
void setnume( string );
void ToString();
protected:
private:
string nume;
int sumacash;
int rest;
static int sumaplata;
};
#endif // BANCNOTE_H
BANCNOTE.CPP
#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;
Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
this->nume=_nume;
this->sumacash=_sumacash;
this->rest=_rest;
this->sumaplata=_sumaplata;
}
Bancnote::Bancnote()
{
this->nume="";
this->sumacash=0;
this->rest=0;
this->sumaplata=0;
}
Bancnote::~Bancnote()
{
cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}
string Bancnote::getnume()
{
return nume;
}
void Bancnote::setnume(string _nume)
{
this->nume=_nume;
}
int Bancnote::getsumacash()
{
return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
return rest;
}
void Bancnote::setrest(int _rest)
{
this->rest=_rest;
}
void Bancnote::ToString()
{
cout<< "-----"<<getnume()<< "-----"<<endl;
cout<<"Suma Cash: "<<this->getsumacash()<<endl;
cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
cout<<"Restul:"<<this->getrest()<<endl;
}
CARD.H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
class Card: public Bancnote
{
public:
Card();
virtual ~Card();
protected:
private:
};
#endif // CARD_H
You have messed up the includes. What you have is more or less this:
Bancnote.h:
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h" // remove this
struct Bancnote {};
#endif
Card.h
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif
When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.
PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)

c++ 'class' has not been declared - passing a class to a class

so at the moment I am trying to run a method within a <translator> class, by passing it an instance of a <bintree> class from my main.cpp. The following is my code, with the error that I am recieveing on the bottom. Im sure I am just missing some aspect to passing parameters, but for the life of me I cannot figure it out.
main.cpp (area where it creates bintree and where it is passed) bottom line most relevant
if (validFile == true)
{
//Create bintree through insert. Rebalance follows
bintree<morseNode> morseTree;
for (int count = 0; count < 26; count++)
{
char letter = morseCodes[count].letter;
string code = morseCodes[count].code;
morseNode node;
node.letter = letter;
node.code = code;
morseTree.insert(node);
}
morseTree.rebalance();
translator fileTranslator(outputFile);//create instance of translator
//Read and translate files based on conversion type
if (translatorType != "e" || translatorType != "E") //English -> Morse Conversion
{
validFile = readFile(inputFile, translatorType, morseCodes, inputList);
if (validFile == true)
{
fileTranslator.engToMorseTranslation(inputList, morseCodes);
}
}
else //Morse -> English Conversion
{
validFile = readFile(inputFile, translatorType, morseCodes, inputList);
if (validFile == true)
{
fileTranslator.morseToEngTranslation(inputList, morseTree);
//Here is where it sends morseTree that is throwing ^^ the error.
}
}
I am receiving it through translator.h (edit: it knows the consts for morseNode)
#ifndef TRANSLATOR_H
#define TRANSLATOR_H
#include <string>
#include <iostream>
#include <list>
//I tried #include "bintree.h" here. this did not work
using namespace std;
class translator
{
private:
string outName;
list<char> morseOutput;
public:
void morseToEngTranslation(list<char> &myList, bintree<morseNode> &myTree)
{
//functions here.. seemed irrelevant as i just wanted to show how i am
//receiving the parameters
}
};
#endif
bintree is not mine, it was provided. the starting declarations as follows. It is very long so and the functions themselves are not important for this issue, so i wont include them.
#ifndef BINTREE_H_
#define BINTREE_H_
#include <stdexcept>
namespace treespc
{
// forward class declaration
template <typename dataType> class bintree;
template <typename dataType> class binnode;
#include "const_iterator.h"
#include "binnode.h"
/********************************************************\
template class for a binary tree
\********************************************************/
template <typename dataType> class bintree
{
public:
//....
private:
//....
};
}
and the errors i receive are:
translator.h:79:52: error: ‘bintree’ has not been declared
void morseToEngTranslation(list<char> &myList, bintree<morseNode> &myTree)
translator.h:79:59: error: expected ‘,’ or ‘...’ before ‘<’ token
void morseToEngTranslation(list<char> &myList, bintree<morseNode> &myTree)
thank you in advance to anyone who can at least point me in the right direction :)
Give the namespace for bintree, either using using namespace treespec or treespc::bintree
#ifndef TRANSLATOR_H
#define TRANSLATOR_H
#include <string>
#include <iostream>
#include <list>
#include "bintree.h"
using namespace std;
class translator
{
private:
string outName;
list<char> morseOutput;
public:
void morseToEngTranslation(list<char> &myList, treespc::bintree<morseNode> &myTree)
{
//functions here.. seemed irrelevant as i just wanted to show how i am
//receiving the parameters
}
};
#endif
ifndef BINTREE_H_
#define BINTREE_H_
Are you missing a # here?
UPDATE: You must include bintree header or use forward declaration (be careful as your class is inside the namespace) See answers here :Why can't I forward-declare a class in a namespace like this?

Debugging C++ compiler error

I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ‘)’ before ‘n’
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
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 have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.

Member variable in scope of one member function but not the other

I'm seriously confused why this is happening. I get an error 'enzyme_acronyms_ was not declared in this scope'. It points to my writeAcronym function but not getAcronym, and both use enzyme_acronyms_. What can possibly cause this?
SequenceMap.h
#ifndef SequenceMap_h
#define SequenceMap_h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SequenceMap
{
private:
string recognition_sequence_;
vector<string> enzyme_acronyms_;
public:
string getAcronym();
void writeAcronym(string an_enz_acro);
}
SequenceMap.cpp
#include "SequenceMap.h"
string SequenceMap::getAcronym()
{
return enzyme_acronyms_[0]; //works fine
}
void writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro); //enzyme_acronyms_ not declared in this scope
}
You've missed the SequenceMap:: qualification on the second function definition:
void SequenceMap::writeAcronym(string an_enz_acro)
It must be declared like this:
void SequenceMap::writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro);
}
You forgot the class scope SequenceMap::.

Data "member not declared in this scope"

I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.