error C2511 : overloaded member function not found in Class - c++

This is the error im recieving.
Error 4 error C2511: 'Vector<T> Menus::sortListBy(sortType,Vector<T>)'
: overloaded member function not found in 'Menus' z:\documents\visual
studio 2010\projects\oo_cw\oo_cw\menus.cpp 410 1 OO_CW
I believe this is something to do with me trying to use an enum that is included in a header but doesnt seem to be carried over to the other classes.
Here are the 2 headers involved and the function im struggling with::
Menus.cpp:
Vector<connections> Menus::sortListBy(sortType sortKey,Vector<connections> sortList){}
Menus.h
#pragma once
#include "std_lib_facilities.h"
#include "Airport.h"
class Journey;
#include <string>
typedef enum {BACK,FORWARD,INVALID,OPTIONS} result;
typedef enum {BOOK,VIEW,EXIT} firstChoice;
class Menus
{
public:
Menus(void);
~Menus(void);
firstChoice firstMenu();
Airport bookingMenuFirst(Vector<Airport>);
Airport bookingMenuSecond(Vector<connections>,Vector<Airport>);
airlines bookingMenuThird(Airport,Airport,Journey&);
string bookingMenuDate();
bool showReciept(string,string,string,string,double,double,double);
string showRecieptNames();
void readReciept(string);
void optionMenu(Journey);
Vector<connections> sortListBy(sortType,Vector<connections>);
};
Journey.h
#pragma once
#include "std_lib_facilities.h"
#include "Airport.h"
#include <string>
#include "Menus.h"
enum sortType {PRICE,TIME} ;
class Journey
{
public:
Journey(void);
~Journey(void);
//accessors
Airport getStart();
Airport getEnd();
string getDate();
airlines getAirline();
string getStringAirline();
double getTime();
double getPrice();
sortType getSort();
//modifiers
void setStart(Airport);
void setEnd(Airport);
void setPrice(double);
void setTime(double);
void setAirline(airlines);
void setDate(string);
void saveBooking();
void setSort(sortType);
private:
Airport startAirport;
Airport endAirport;
double price;
double time;
string date;
airlines airline;
Vector<string> splitBy(string,string);
sortType sortingBy;
};
Menus.cpp header statements
#include "Menus.h"
#include "std_lib_facilities.h"
#include "Airport.h"
#include "Journey.h"
#include <string>
using namespace std;

The enum sortType is defined in Journey.h
however, is not visible in menu.h and you are using enum sortType as input argument in the declaration of member function Menus::sortListBy(sortType,Vector<connections>); in the definition of the Menus class.
In Menus.h remove the forward declaration of class Journey; and replace it with #include "Journey.h".
Remove #include "Menus.h" in Journey.h.
There should be no problem, since you don't have circular dependency issues between Journey and Menus.

Related

Issues with function implementation in multiple header files

I am currently working on a project with several other people. We originally had one header file with class and method declarations and one .cpp file with implementation/main. We decided to try and move the function implementation to the header files so we could all work on different features at the same time without producing as many merge conflicts in git.
When I compile the code, I get "undeclared identifier" errors and others that follow, leading me to the conclusion that something is wrong with the way my headers are set up. The code is long, so I'll try to provide a relevant example:
(Assignment.h and Student.h contain classes in a similar setup)
Instructor.h:
#pragma once
#include "Assignment.h"
#include "Student.h"
#include "Course.h"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
class Instructor {
private:
std::string instructorName;
std::vector<Course*> courseList;
public:
Course *currentCourse;
Instructor();
Instructor(std::string name);
void setName(std::string name);
std::string getName();
void firstTimeInstructor();
void addCourse();
void addCourse(std::string name);
void removeCourse();
void mainMenu();
void init();
};
... implementation for Instructor methods...
Course.h:
#pragma once
#include "Assignment.h"
#include "Student.h"
#include "Instructor.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Course {
private:
std::string courseName;
std::vector<Student*> studentList;
std::vector<Assignment*> assignmentList;
public:
Course(std::string name);
void setCourseName(std::string name);
std::string getCourseName();
void showCourseOptions();
void addStudent();
void addStudent(std::string first, std::string last);
void addAssignment();
void addAssignment(std::string name, double points);
void print();
void courseMenu();
};
...implementation for Course methods...
First error: instructor.h(17): error C2065: 'Course': undeclared identifier
(Similar ones follow: Student, Assignment, etc.)
I was studying this post for a while, but I wasn't really able to find a definitive answer and I wasn't sure if it even applied to my situation.
I know some of this (such as the using namespace std or even declaring functions in the header) may be bad practice, but we're all a bit new to this so any guidance on the subject would be appreciated.

typedef in class. In another class error: type has not been declared

In Hire.h i declared two typedefs. In Customer.h i would be able to use this typedefs. How to fix it?
#pragma once
#include "typedefs.h"
#include "Vehicle.h"
#include "Customer.h"
namespace rent {
typedef std::shared_ptr<Hire> shared_Hire_t;
typedef std::map<uuid_t, shared_Hire_t> mapHirePtr_t;
class Hire
{
public:
Hire(date_t start_date, Vehicle *vehicle, Customer *customer);
virtual ~Hire();
private:
uuid_t uuid;
date_t start_date;
date_t end_date;
uint_t hire_days;
double day_price;
double cost;
Vehicle *vehicle;
Customer *customer;
};
}
-
#pragma once
#include <iostream>
#include <map>
#include <memory>
#include "typedefs.h" //uint_t, uuid_t
#include "CustomerType.h"
namespace rent {
class Hire;
class Customer
{
public:
enum EnumCustomerType {standard, medium, premium}; //to set a CustomerType
Customer(std::string name, std::string surname, std::string pesel, double balance = 0.0, EnumCustomerType enum_customer_type = standard);
virtual ~Customer();
void add_hire(shared_Hire_t hire);
void end_hire(shared_Hire_t hire);
protected:
std::string name;
std::string surname;
std::string pesel;
double balance;
EnumCustomerType enum_customer_type;
double discount;
mapHirePtr_t current_hires;
uint_t hires_count;
private:
std::unique_ptr<CustomerType> customer_type;
};
}
-
Customer.h:31:17: error: 'shared_Hire_t' has not been declared void
add_hire(shared_Hire_t hire);
^ Customer.h:32:31: error: 'shared_Hire_t' has not been declared
void end_hire(shared_Hire_t hire);
^ Customer.h:42:3: error: 'mapHirePtr_t' does not name a type mapHirePtr_t current_hires;
^ Customer.cpp:87:14: error: prototype for 'void
rent::Customer::add_hire(rent::shared_Hire_t)' does not match any in
class 'rent::Customer'
void Customer::add_hire(shared_Hire_t hire)
^ In file included from Customer.cpp:1:0: Customer.h:31:8: error: candidate is: void
rent::Customer::add_hire(int) void add_hire(shared_Hire_t hire);
^ Customer.cpp:94:14: error: prototype for 'void rent::Customer::end_hire(rent::shared_Hire_t)' does not match any in
class 'rent::Customer'
void Customer::end_hire(shared_Hire_t hire)
^ Customer.cpp:1:0: Customer.h:32:22: error: candidate is: void rent::Customer::end_hire(int)
void end_hire(shared_Hire_t hire);
PS. How to separate two blocks of code in an appropriate way in stackoverflow?
You have to include Hire.h in Customer.h for the typedefs to be visible.
As that would introduce a circular reference i would suggest putting the typedefs in a seperate file.
You need to #include "Hire.h in Customer.h to make the definitions available there.
Unfortunately, that creates a circular dependency between the headers.
Fortunately, Hire.h doesn't need the definitions of Customer and Vehicle, so you can stop including them and just forward-declare the classes.
Hire.h:
#include "typedefs.h"
namespace rent {
class Vehicle;
class Customer;
class Hire;
typedef std::shared_ptr<Hire> shared_Hire_t;
typedef std::map<uuid_t, shared_Hire_t> mapHirePtr_t;
class Hire
{
// ...
Customer.h:
#include <iostream>
#include <map>
#include <memory>
#include "typedefs.h" //uint_t, uuid_t
#include "CustomerType.h"
#include "Hire.h"
namespace rent {
class Customer
{
// ...

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.

expected class name before '{' token. C++ inheritance

I've spent quite a few hours researching and trying to figure out why I'm getting this error. Basically the three files that have to do with the inheriting are CollegeMember.h, Employee.h, and EmpAcademicRecord.h. Employee. is inheriting from CollegeMember.h and EmpAcademicRecord.h is inheriting from Employee.h. Like this CollegeMember <- Employee <- EmpAcademicRecord. The error occurs in EmpAcademicRecord.h. Heres the three files.
CollegeMember.h
#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "Employee.h"
#include "Student.h"
using namespace std;
// ****************************************************************************
// Class Definitions follow
typedef char* String;
// The CollegeMember class
class CollegeMember
{
protected:
int ID_Number;
string FirstName, LastName;
string AddressLine1, AddressLine2, StateProv, Zip;
string Telephone;
string E_Mail;
string answer, answer2, answer3, answer4;//used as sort of booleans for use with validation
// member functions
public:
CollegeMember ( ); // constructor
CollegeMember(const CollegeMember&); //overloaded constructor
void Modify (CollegeMember Member);
void InputData(int x);
string Summary ( ); //summary
string PrintMe(); //fully describes
}; // End of CollegeMember class declaration
Employee.h
#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "EmpAcademicRecord.h"
#include "EmpEmploymentHistory.h"
#include "EmpExtraCurricular.h"
#include "EmpPersonalInfo.h"
#include "EmpPublicationLog.h"
using namespace std;
// ****************************************************************************
// Class Definitions follow
typedef char* String;
// The Employee Class
class Employee: protected CollegeMember
{
float Salary;
protected:
string Department, JobTitle;
// Member Functions
public:
Employee ( ); // constructor
void Modify (Employee ThisEmp);
void InputData(int x);
void SetSalary (float Sal) // Specified as an in-line function
{ Salary = Sal;}
float GetSalary ( ) {return Salary;} // Specified as an in-line function
string Summary ( ); //summary
string PrintMe(); //fully describes
}; // End of Employee class declaration
EmpAcademicRecord.h
#include <iostream>
#include <cstdlib>
#include<ctype.h>
#include<string.h>
using namespace std;
typedef char* String;
class EmpAcademicRecord: protected Employee{ //error occurs on this line
protected:
int ReferenceNumber;
string Institution;
string Award;
string start;
string end;
public:
EmpAcademicRecord();
void InputData (int x);
void Modify(EmpAcademicRecord ThisRec);
void Summary();
};
Any help with this would be greatly appreciated.
That sort of error is usually caused by the type not being defined when you try to use it.
In this case, it appears that you may have included EmpAcademicRecord.h without having first included Employee.h (the includes at the top of the former do not show the latter).
In other words, at the point where the compiler sees:
class EmpAcademicRecord: protected Employee { //error occurs on this line
it has no idea what the Employee class is.
It may be a simple matter of adding:
#include "Employee.h"
to the top of that file, it's a little difficult to be certain since we don't have the code files. In any case, it's certainly a good first step.
Since you have EmpAcademicRecord.h being included by Employee.h, that will probably result in an infinite recursion.
You could fix that with include guards, but I can't see why you need that particulat inclusion. EmpAcademicRecord depends on Employee, not the other way around.

Classes as parameters error

In Weapon.h, when I try and take a class 'Entity*' as a parameter, it says "Syntax error: identifier 'Entity'" when I compile. Additionally when I roll over the text 'target', Visual C++ Express 2010 gives me the text " *target". The Entity class is fine and I'm pretty sure it's included correctly.
(I won't post Player.h as it's unnecessary - see Library.h - but it has a header guard and includes Entity.h)
Library.h:
#ifndef _LIBRARY_
#define _LIBRARY_
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <map>
#include <exception>
#include <sstream>
//file includes
#include "Globals.h"
#include "Player.h"
#include "Exception.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
//prototypes that require "Library.h"
bool Poglathon(std::vector<std::string>& text,Player *player);
bool PoglathonTown(std::vector<std::string>& text,Player *player);
std::map<std::string,Weapon*> init_weapons(void);
std::map<std::string,Armour*> init_armour(void);
std::map<std::string,Consumable*> init_consumables(void);
#endif //__LIBRARY__
Weapon.h:
#ifndef _WEAPON_H_
#define _WEAPON_H_
#include "Shopable.h"
class Weapon : public Shopable{
private:
int Damage;
public:
Weapon(int c,int d,std::string n) : Shopable(c,n),Damage(d){}
std::string getDesc() const{
return getName()+"\t"+tostring(Damage)+"\t"+tostring(Cost);
}
int getDamage() const{return Damage;}
int DamageTarget(Entity* target){
int DamageDealt = 0;
//do damage algorithm things here
return DamageDealt;
}
};
#endif
Shopable.h:
#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_
#include "Library.h"
class Shopable{
protected:
std::string Name;
int Cost;
std::string Description;
public:
Shopable(int c, std::string n):Cost(c),Name(n){}
std::string getName() const{return Name;}
int getCost() const {return Cost;}
virtual std::string getDesc() const = 0;
};
#endif
Entity.h:
#ifndef _ENTITY_
#define _ENTITY_
#include "Library.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
class Entity{
public:
void printStats() const;
void heal(double health);
std::string name;
protected:
//player stats
double str; //strength
double wis; //wisdom
double ref; //reflex
double hp; //health points
double maxHp; //maximum health points
double i; //initiative
double inte; //intelligence
double c; //courage
int gold; //gold
int xp; //experience
int ap; //armour points
int wd; //weapon damage
int lvl; //level
int sp; //skill points
Weapon* weapon;//weapon
Armour* cArmour;//current armour
};
#endif
In C++, classes must be declared before they are referenced. You are #include-ing Weapon.h in Entity.h, but at that point, the compiler doesn't know about the existence of class Entity.
You will either need to change the order in which things are declared, or add a forward declaration "above" class Weapon. It can simply be:
class Entity;
That tells the compiler that there is such a name as Entity. However, it doesn't tell it anything about what members it has, so you can't actually do anything with it, other than declare variables of Entity * and Entity &, and pass them around.
Your headers include each other because your classes refer to each other. (But your compiler doesn't suffer from a stackoverflow because of your include guards - that's a good thing!)
You should arrange your header files hierarchically, ie there are files at the 'top' which #include nothing and files 'below' which include some of the top ones and so-on down the hierarchy. But at no point should there be 'loops'.
In order to break your loops in your code, any classes that refer to each other should forward declare any mutual dependencies and only refer to dependency names and not their members.
e.g.
Entity.h
class Weapon;
class Entity{
...
Weapon* weapon;
};
Weapon.h
class Entity;
class Weapon{
...
int DamageTarget(Entity* target);
};
Notice how Weapon.h only refers to Entity*.
You will need to define int Weapon::DamageTarget(Entity* target) in Weapon.cpp
#include <entity.h>
Or forward-declare only in the header
class Entity;
This makes compilation a bit faster (you still need to include it to use in the implementation).
Weapon.h doesn't #include Entity.h, or anything that recursively includes it. Therefore, it doesn't know about the class Entity.