Does not match in any class? - c++

I've encountered an issue when testing my code.
void Library::checkOutBook(std::string pID, std::string bID); {
bool patronIDMatch = false, bookIDMatch = false;
int bookOnFile=-1, patronOnFile=-1;
for (int i=0; i<members.size(); i++) {
if (pID==members[i].getIdNum()) {
patronIDMatch=true;
patronOnFile=i;
break;
}
else continue;
for the first line here, I'm getting the following errors:
Library.cpp:68: error: prototype for ‘void Library::checkOutBook(std::string, std::string)’ does not match any in class ‘Library’
Library.cpp:68: error: expected unqualified-id before ‘{’ token
But I'm matching it as it appears in my Library class as such
//Library.hpp
#ifndef LIBRARY_HPP
#define LIBRARY_HPP
#include <string>
#include <vector>
#include "Patron.hpp"
class Library {
private:
std::vector<Book*> holdings;
std::vector<Patron*> members;
int currentDate;
public:
Library();
void addBook(Book*);
void addPatron(Patron*);
std::string checkOutBook(std::string pID, std::string bID);
std::string returnBook(std::string bID);
std::string requestBook(std::string pID, std::string bID);
std::string payFine(std::string pID, double payment);
void incrementCurrentDate();
Patron* getPatron(std::string pID);
Book* getBook(std::string bID);
};
#endif
How would I go about fixing this?
i am prohibited from altering the header file unfortunately :(
edit:
void Library::returnBook(std::string bID); {
bool bookIDMatch=false;
string tempPatronID;
int bookOnFile=-1;
for (int i=0; i

It doesn't match because the function you're trying to define returns void:
void Library::checkOutBook(std::string pID, std::string bID)
but the declaration in the Library class says it returns std::string:
std::string checkOutBook(std::string pID, std::string bID);
Also, remove that semicolon between the ) and the { in the function header.

Related

Error using "enum" within a class

I get the following error message about my enum, and I can't figure out why.
I have only 1 header file and only 1 source file where I implemented all the functions (and main). I declared the enum within class "Survivor", in "public". I didn't forget to include the header file in the source file. Please help me see what I've done wrong.
"error: 'e_status' does not name a type"
This is my header file:
class Survivor
{
public:
enum e_status {SINGLE, MARRIED, RELATIONSHIP};
char* get_name();
int get_age();
e_status get_status();
void set_name(char n[]);
void set_age (int a);
void set_status (e_status s);
const void print();
private:
char name [20];
int age;
e_status status;
};
This is the relevant part in my Source file:
e_status Survivor::get_status()
{
return status;
}
You have to use a qualified name
Survivor::e_status Survivor::get_status()
{
//...
}
The type e_status is a member of the class Survivor.
A return type of a member function is not searched in the class scope while for example a parameter type is used in the class scope.
Consider the following two function definitions.
#include <iostream>
class Survivor
{
public:
enum e_status {SINGLE, MARRIED, RELATIONSHIP};
e_status get_status();
void set_status (e_status s);
//...
private:
e_status status;
};
Survivor::e_status Survivor::get_status()
{
return status;
}
void Survivor::set_status (e_status s)
{
status = s;
}
int main()
{
return 0;
}
You're trying to reference outside the class; do this:
Survivor::e_status Survivor::get_status()

Variable or field declared void C++

I am making a school assignment, but I am getting a strange error. I have tried to google it, but nothing helped.
So I have a file called main.cpp. Within this file I have some includes and code.
This:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
#include "RentalAdministration.h"
#include "Limousine.h"
#include "Sedan.h"
void addTestDataToAdministration(RentalAdministration* administration)
{
string licencePlates[] = {"SD-001", "SD-002", "SD-003", "SD-004", "LM-001", "LM-002"};
for (int i = 0; i < 4; i++)
{
Car* sedan = new Sedan("BMW", "535d", 2012 + i, licencePlates[i], false);
administration->Add(sedan);
}
for (int i = 4; i < 6; i++)
{
Car* limousine = new Limousine("Rolls Roys", "Phantom Extended Wheelbase", 2015, licencePlates[i], true);
administration->Add(limousine);
}
}
int main( void )
{
RentalAdministration administration;
addTestDataToAdministration(&administration);
}
So the compiler tells me that the variable: "RentalAdministration administration" does not exist.
So if we have look in my rentaladministration header. We see this:
#ifndef RENTALADMINISTRATION_H
#define RENTALADMINISTRATION_H
#include <vector>
#include "car.h"
class RentalAdministration
{
private:
std::vector<Car*> Cars;
Car* FindCar(std::string licencePlate);
Car* FindCarWithException(std::string licencePlate);
public:
std::vector<Car*> GetCars() const {return Cars;}
bool Add(Car* car);
bool RentCar(std::string licencePlate);
double ReturnCar(std::string licencePlate, int kilometers);
void CleanCar(std::string licencePlate);
RentalAdministration();
~RentalAdministration();
};
#endif
This is the exact error:
src/main.cpp:18:34: error: variable or field ‘addTestDataToAdministration’ declared void
void addTestDataToAdministration(RentalAdministration* administration)
^
src/main.cpp:18:34: error: ‘RentalAdministration’ was not declared in this scope
src/main.cpp:18:56: error: ‘administration’ was not declared in this scope
void addTestDataToAdministration(RentalAdministration* administration)
Help will be appreciated!
Edit:
I am getting warnings in sublime for the Sedan and Limousine headers. Something that has to do with some static constants. I think it was called a GNU extension. Maybe it has something to do with it.
Even when I comment the call of that function out. I get the same error.
I am calling that function nowhere else.
Some people say that the cause might be in these headers:
#ifndef LIMOUSINE_H
#define LIMOUSINE_H
#include "Car.h"
//c
class Limousine : public Car
{
private:
bool needsCleaning;
bool hasMiniBar;
static const double priceperkm = 2.5;
public:
double Return(int kilometers);
void Clean();
bool GetHasMiniBar() const { return hasMiniBar;}
void SetHasMiniBar(bool value) {hasMiniBar = value;}
Limousine(std::string manufacturer, std::string model, int buildYear, std::string licencePlate, bool hasminiBar);
~Limousine();
};
#endif
2:
#ifndef SEDAN_H
#define SEDAN_H
#include "Car.h"
//c
class Sedan : public Car
{
private:
int lastCleanedAtKm;
bool hasTowBar;
bool needsCleaning;
static const double priceperKm = 0.29;
public:
void Clean();
int GetLastCleanedAtKm() const {return lastCleanedAtKm;}
void SetLastCleanedAtKm(bool value){ lastCleanedAtKm = value;}
bool GetHasTowBar() const {return hasTowBar;}
void SetHasTowBar(bool value) {hasTowBar = value;}
bool GetNeedsCleaning() const {return needsCleaning;}
void SetNeedsCleaning(bool value){needsCleaning = value;}
Sedan(std::string manufacturer, std::string model, int buildYear, std::string licencePlate, bool hastowBar);
~Sedan();
};
#endif
class Limousine : public Car
{
private:
static const double priceperkm = 2.5;
...
}
Remove the static and declare the member simply as const double, example:
class Limousine : public Car
{
private:
const double priceperkm = 2.5;
...
}
The error message ‘RentalAdministration’ was not declared in this scope indicates that the right header file for RentalAdministration was not included. Check the file names to make sure class declaration for RentalAdministration is in the right file.
Restarting the terminal has somehow solved this error. I got another error this time, which I solved already. I missed the destructor. It stood in the header file, but not in the cpp file.
Buggy terminals...

No default constructor exists for class c++ [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
Hello,
I'm trying to instantiate an anonymous object with a std::string variable 'name'. But intellisenen gives me error saying
E0291 no default constructor exists for class "Player" GoldGame e:\C++ Projects\Hello World\GoldGame\GoldGame.cpp 17
I have provided a constructor which can just take a std::string variable since other parameters are provided with default value.
Can you guys shed some light on this?
What confuses me even more is that when I change
Player(name);
to
Player a(name);
or to
Player("test");
then intellisense becomes totally fine with those.
GoldGame.cpp
#include "stdafx.h"
#include "Creature.h"
#include "Player.h"
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name: ";
std::string name;
std::cin >> name;
Player(name);
return 0;
}
Creature.h
#pragma once
#include <string>
class Creature
{
public:
Creature(const std::string &name, const char symbol, const int health, const int damage, const int gold);
~Creature();
//getters
const std::string& getName() { return m_name; }
const char getSymbol() { return m_symbol; }
const int getHealth() { return m_health; }
const int getDamage() { return m_damage; }
const int getGold() { return m_gold; }
//health, gold and dead
void reduceHealth(const int healthMinus);
void addGold(const int gold);
bool isDead();
private:
std::string m_name;
char m_symbol;
int m_health;
int m_damage;
int m_gold;
};
Creature.cpp
#include "stdafx.h"
#include "Creature.h"
Creature::Creature(const std::string & name, const char symbol, const int health, const int damage, const int gold)
:m_name(name), m_symbol(symbol), m_health(health), m_damage(damage), m_gold(gold)
{
}
Creature::~Creature()
{
}
void Creature::reduceHealth(const int healthMinus)
{
m_health -= healthMinus;
}
void Creature::addGold(const int gold)
{
m_gold += gold;
}
bool Creature::isDead()
{
if (m_health>0)
{
return true;
}
else
{
return false;
}
}
Player.h
#pragma once
#include "Creature.h"
#include <string>
class Player :
public Creature
{
public:
Player(const std::string &name, const char symbol='#', const int health=10, const int damage=1, const int gold=0);
~Player();
const int getLevel() { return m_level; }
void levelUp();
bool hasWon();
private:
int m_level;
};
Player.cpp
#include "stdafx.h"
#include "Player.h"
Player::Player(const std::string & name, const char symbol, const int health, const int damage, const int gold)
:Creature(name,symbol,health,damage,gold)
{
}
Player::~Player()
{
}
void Player::levelUp()
{
++m_level;
}
bool Player::hasWon()
{
if (m_level>=20)
{
return true;
}
else
{
return false;
}
}
Player(name); does not do what you think it does. It declares a new variable name of type Player and calls a default constructor. If you want to instantiate an anonymous Player variable then you need to write
(Player(name));
// or
Player{name}; // list initialization since C++11

Request for member, non class-type

i'm getting huge amount of errors in my code, and it all boils down to the following:
Library.cpp:89: error: request for member ‘getLocation’ in ‘((Library*)this)->Library::holdings.std::vector<_Tp, _Alloc>::operator[] [with _Tp = Book*, _Alloc = std::allocator](((long unsigned int)bookOnFile))’, which is of non-class type ‘Book*’
i'm just so confused as to how I should write the following:
Patron* matchPatron = &members[patronOnFile];
if (PatronIDMatch == true && bookIDMatch == true) {
if (holdings[bookOnFile].getLocation() == ON_SHELF) {
holdings[bookOnFile].setCheckedOutBy(matchPatron);
holdings[bookOnFile].setLocation(CHECKED_OUT);
holdings[bookOnFile].setDateCheckedOut(currentDate);
members[patronOnFile].setCheckedOutBooks(&holdings[bookOnFile]);
cout << members[patronOnFile].getName() << " check out successful"
<< holdings[bookOnFile].getTitle() << ".";
(i have the error code for so many lines, this one is just for the getLocation line.)
and my header is the following:
//Library.hpp
#ifndef LIBRARY_HPP
#define LIBRARY_HPP
#include <string>
#include <vector>
#include "Patron.hpp"
class Library {
private:
std::vector<Book*> holdings;
std::vector<Patron*> members;
int currentDate;
public:
Library();
void addBook(Book*);
void addPatron(Patron*);
std::string checkOutBook(std::string pID, std::string bID);
std::string returnBook(std::string bID);
std::string requestBook(std::string pID, std::string bID);
std::string payFine(std::string pID, double payment);
void incrementCurrentDate();
Patron* getPatron(std::string pID);
Book* getBook(std::string bID);
};
#endif
am i just writing my code wrong here? how should I be writing it? i can provide my entire program if needed.
EDIT:
Patron* matchPatron = &members[patronOnFile];
You have a vector of Book*, which are pointers, but you are using dot notation when you call holdings[bookOnFile].getLocation(). Same goes for the other method calls. Try replacing with holdings[bookOnFile]->getLocation(), etc.

Unable to use member function inside a class

I am currently learning about classes and am having a problem in my class implementation file.
In my class header/specification file Book.h , I have the public member function setPages.
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "Author.h"
#include "Publisher.h"
class Book
{
private:
std::string _title;
std::string _edition;
int _pages;
int _copyrightYear;
Author _author;
Publisher _publisher;
public:
Book (std::string title, Author author, Publisher publisher)
{_title = title; _author = author; _publisher = publisher;}
void setPages (int pages);
void setCopyYear (int copyrightYear);
void setEdition (std::string edition);
std::string getTitle () const;
std::string getEditon () const;
int getPages () const;
int getCopyYear () const;
Author getAuthor () const;
Publisher getPublisher () const;
};
#endif
In my Book.cpp implementation file I have
#include <string>
#include "Author.h"
#include "Publisher.h"
#include "Book.h"
void Book::setPages (int pages)
{
_pages = pages;
}
I keep getting the error that Book is not a classname or namespace but I don't see what I've done wrong. I included my Book header file and checked to make sure everything was spelled correctly in the class. I've done the same thing in my other classes and it is working so I don't see why this isn't.
Any help appreciated thanks.
Here is Publisher.h and Author.h
#ifndef PUBLISHER_H
#define PUBLISHER_H
class Publisher
{
private:
std::string _name;
std::string _address;
std::string _phoneNumber;
public:
Publisher (std::string& name)
{_name=name;}
void setAddress (std::string address);
void setNumber (std::string phoneNumber);
std::string getAddress () const;
std::string getNumber () const;
bool operator==(std::string name)
{
if (_name == name)
return true;
else
return false;
};
#endif
and Author.H
#ifndef AUTHOR_H
#define AUTHOR_H
class Author
{
private:
std::string _name;
int _numberOfBooks;
public:
Author(std::string& name)
{_name = name;}
void setNumOfBooks (int numberOfBooks);
int getNoOfBooks () const;
bool operator==(std::string _name)
{
if (this->_name == _name)
return true;
else
return false;
}
};
#endif
Until #ahenderson decides to turn his comments into an answer:
bool operator==(std::string name) in "Publisher.h" is missing a brace in your example. is that actually in your code or a copy and paste error?
bool operator==(std::string name)
{
if (_name == name)
return true;
else
return false;
Oops, no brace here!
Suggestion: Simplify your operator== method:
The expression _name == name will already return true or false. No need to put it into an if clause that returns true or false.
Try this:
bool operator==(const std::string& name)
{
return (_name == name);
}
In the above example, the expression is evaluated and the result is returned, directly.
Also, you may run into compiler issues if your variables begin with an underscore, '_'. Change your naming convention so this issue doesn't raise its ugly head. Two common practices are to append a suffix, name_, or prefixing with something like m_name.