just getting an odd error and I'm not entirely sure as to why.
I have 4 files (two headers and two implementations). The issue is inside the headers:
The main file ONLY includes the Station.h, which is why Stations.h is included inside it.
Station.h
#ifndef STATION_H
#define STATION_H
#include "Stations.h"
#include <string>
enum PassType{student, adult};
class Station{
std::string station_name;
unsigned int student_passes;
unsigned int adult_passes;
public:
Station();
void set(const std::string&, unsigned, unsigned);
void update(PassType, int);
unsigned inStock(PassType) const;
const std::string& getName() const;
};
#endif
Stations.h
#ifndef STATIONS_H
#define STATIONS_H
#include "Station.h"
namespace w2{
class Stations{
Station *station;
public:
Stations(char *);
void update() const;
void restock() const;
void report() const;
~Stations();
};
}
#endif
It doesn't know what Station is. I'm getting the following error:
./Stations.h:9:2: error: unknown type name 'Station'; did you mean 'Stations'?
Station *station;
What exactly am I missing here?
You are #includeing Stations.h in Station.h. As a result, the compiler sees class Stations before class Station. In this case, it doesn't appear that Station requires Stations, so you can simply remove the include.
If Station did need to know about Stations, then you'd have to use a forward declaration in one of the headers or the other (and be careful not to use the forward-declared class in a way that required the full definition).
Don't forget to put a semicolon after you declare the Stations class:
class Stations {
Station *station;
};
U need to do the forward declaration.
Remove the #include "Stations.h" from the Station.h
#ifndef STATIONS_H
#define STATIONS_H
#include "Station.h"
namespace w2{
class Station;
class Stations{
Station *station;
public:
Stations(char *);
void update() const;
void restock() const;
void report() const;
~Stations();
};
}
#endif
Related
Having trouble understanding why I'm getting an 'undeclared identifier' error when I've made sure to include the header file that has the declaration of the class I'm making a vector of.
#pragma once
#include <vector>
#include "Member.h"
class Party {
private:
std::vector<Member> members;
public:
Party();
int get_party_size();
void add_member(Member new_member);
Member& get_member(int num);
};
Here's "Member.h"
#pragma once
#include <vector>
#include <string>
#include "Party.h"
class Member
{
private:
int hp;
bool is_stunned;
bool is_alive;
public:
Member();
~Member();
int get_hp();
bool get_is_stunned();
bool get_is_alive();
void take_damage(int amt);
void stun();
virtual void turn(std::vector<Party>& parties, int my_party, int my_member_number);
virtual std::string get_class_name();
};
Pretty new to the language, so sure I'm missing something obvious.
You have circular dependency between Member and Party
Remove the line
virtual void turn(
std::vector<Party>& parties,
int my_party,
int my_member_number);
in Member and remove the #include "Party.h" in Member.h
Instead think along the lines that a Party is just a collection of Members so there is no need for an individual Member to know about the container
So after input from #some-programmer-dude you could also solve it by adding a forward declaration in your Member.h instead of including the Party.h
class Party;
class Member { ... }
I'm designing a class hierarchy that follows a diamond pattern, and I'm trying to debug through about a million errors right now; however, most of them are simple fixes that I should be able to figure out. However, I'm having difficulty understanding the compiler's complaints in this one.
Basically, I start off with a simple Entity class that has two derived classes: Buyer and Seller. A fourth class Retailer, in turn, is descended from both classes - that is, it uses multiple inheritance(and yes, I know what kind of mess that's asking for, unfortunately that's exactly the point of the project).
for reference, the header files for my classes is as follows:
Entity.h
#pragma once
#include <string>
class Entity {
public:
Entity(std::string &, std::string &, double);
/*Accessor methods for private members*/
std::string getName();
std::string getID();
double getBalance();
/*Mutator methods for private members*/
void setName(std::string &);
void setID(std::string &);
void setBalance(double);
/*Additional methods*/
virtual void list();
virtual void step() = 0;
protected:
/*Private members of the entity class*/
std::string name;
std::string id;
double balance;
};
for the Buyer.h file
#pragma once
#include "Entity.h"
#include "Order.h"
#include "Seller.h"
#include <queue>
#include <string>
class Seller;
class Buyer : virtual public Entity {
public:
Buyer(std::string, std:: string, double);
virtual ~Buyer() { }
void addSeller(Seller *);
std::queue<Seller *> getSellers();
void addOrder(Order *);
void list();
void step() override;
protected:
std::queue<Order *> orders;
std::queue<Seller *> sellers;
};
For Seller.h
#pragma once
#include "Entity.h"
#include "Order.h"
#include "Buyer.h"
#include "Inventory.h"
#include <string>
#include <vector>
class Buyer;
class Seller : virtual public Entity {
public:
Seller(std::string, std::string, double);
virtual ~Seller() {}
void addBuyer(Buyer *);
std::vector<Buyer> getBuyers();
void setInventory(Inventory *);
Inventory * getInventory();
void list();
double fillOrder(Order *);
void step();
protected:
Inventory inventory;
std::vector<Buyer *> buyers;
};
And finally for Retailer.h
#pragma once
#include "Buyer.h"
#include "Seller.h"
#include <string>
class Retailer : public Buyer, public Seller {
public:
Retailer(std::string, std::string, double);
virtual ~Retailer() { }
void list();
void step();
};
The majority of the errors I get when trying to compile these files are along the lines of
Buyer.h:9:7: note: candidate expects 1 argument, 0 provided
Seller.h:14:3: note: candidate expects 3 arguments, 0 provided
Which is odd, because for that first line, I shouldn't even have to provide an argument and the second one is the definition of the constructor....
Basically, what I'm failing to understand is what does the compiler mean by a line of code expecting a different number of arguments than were provided? Should I be including default constructors that use no arguments? Is there something wrong with the way they're declared? I can also post the code for my .cpp files if necessary, although they don't seem to be mentioned a lot by the compiler error reports.
It means that the compiler is considering that function for overload resolution, but it's not a match because of differing number of arguments.
I'm getting this error. It's like the compiler does not recognise my declarations
g++ -c main.cc
In file included from Storage.h:7:0,
from Server.h:5,
from Control.h:8,
from main.cc:5:
Serializer.h:11:36: error: ‘Storage::UpdateType’ has not been declared
Serializer.h:12:45: error: ‘Storage::UpdateType’ has not been declared
make: *** [main.o] Error 1
Anyone have an idea what this error is about because the enum has already beeen declared. The affected code is below:
Serializer.h
#ifndef SERIALIZER_H
#define SERIALIZER_H
#include "Storage.h"
class Storage;
class Serializer{
public:
Serializer();
void serialize(List&, Storage::UpdateType&, std::string&);
void deserialize(std::string&, Storage::UpdateType&, List&);
};
#endif
Storage.h
#ifndef STORAGE_H
#define STORAGE_H
#include "List.h"
#include "Interface.h"
#include "Movie.h"
#include "Serializer.h"
class Storage{
public:
enum UpdateType {ADD, DELETE, RETRIEVE};
Storage();
~Storage();
List* list;
void retrieve(List*);
void update(UpdateType, List*);
void handleRequest(string&, string&);
private:
//Serializer serial;
};
#endif
The problem you have is that your code is interpreted as follows:
#ifndef STORAGE_H
#define STORAGE_H
// replacing the include with the Serializer.h file
#include "List.h"
#include "Interface.h"
#include "Movie.h"
// replacing the include with the Storage.h file
#ifndef STORAGE_H // returns true
#endif
class Storage;
class Serializer{
public:
Serializer();
// here Storage::UpdateType is still unknown to the compiler
void serialize(List&, Storage::UpdateType&, std::string&);
void deserialize(std::string&, Storage::UpdateType&, List&);
};
#endif
class Storage{
public:
// as it gets declared here
enum UpdateType {ADD, DELETE, RETRIEVE};
Storage();
~Storage();
List* list;
void retrieve(List*);
void update(UpdateType, List*);
void handleRequest(string&, string&);
private:
//Serializer serial;
};
#endif
the best solution, imho, would be to extract the enum UpdateType from your storage class and forward declare it in Serializer. Or even declare it within the Serializer header, as Storage is encapsulating Serializer stuff, so basically, the UpdateType stuff should be in the same encapsulated context as the Serializer stuff.
Another solution would be #Nim suggests, to include Storage from Serializer, forward declare Serializer in Storage and include Serializer in main. But it may deceives the way you have thought your design out, reversing the encapsulation of Serializer and Storage.
Finally, I'm really not sure if this is possible and how would be the syntax, but if it is, you could simply forward declare enum Storage::UpdateType in Serializer. And I guess that's what you wish would be possible.
I want to compile the Rigi source code but I get some error while compiling:
adt/object.h: At global scope:
adt/object.h:35:18: error: ‘class RigiObject RigiObject::RigiObject’ is inaccessible
adt/chararray.h:51:13: error: within this context
make: *** [cl_arcflags.o] Error 1
Here our two files.
object.h:
#ifndef OBJECTH
#define OBJECTH 1
#include <stdio.h>
#ifndef STREAM_H
#include <iostream>
#endif
#ifndef __STRING_H
#include <string.h>
#endif
#ifndef __STDLIB_H
#include <stdlib.h>
#endif
#ifndef _CCHEADER_H_
#include "CCheader.h"
#endif
extern char* indent_line(int);
class RigiObject;
typedef RigiObject* ObjectPtr;
#define Oberr(a) fprintf(stderr,"ERROR :: Generic Object Routine Called :: %s\n","a");
class RigiObject {
public:
RigiObject() {/*Oberr(RigiObject)*/;}
~RigiObject() {/*Oberr(~RigiObject)*/;}
// Routines that are really described by the Derived Classes
virtual int Printout(int) const
{Oberr(printout); return (int) 0;}
virtual unsigned int Hash() const
{Oberr(hash); return (unsigned int) 0; }
virtual RigiBool isEqual(void* a) const
{Oberr(isEqual); a = NIL;
(void) abort();
return (RigiBool) RigiFalse;}
virtual void Delete_class(ObjectPtr)
{Oberr(delete_type);}
virtual void* Create_class();
virtual void* Duplicate_class();
};
#endif
and chararray.h:
#ifndef CHARARRAYH
#define CHARARRAYH
#ifndef ARRAYOBIDH
#include "array.h"
#endif
#ifndef CHARTYPEH
#include "chartype.h"
#endif
class CharArray;
typedef CharArray* CharArrayPtr;
class CharArray : public Array {
int slot;
public:
// Routines to initialize and destroy the class.
CharArray(unsigned int size = CLTN_DEFAULT_CAPACITY);
CharArray(const CharArray&);
~CharArray();
// Functions that are Required to Use this Class as an Object
// .... all routines the same as in Class Array.......
// Routines that are required by a Collection class and derived classes
// of Collections. [See Array Class for these routines.]
virtual unsigned int size() const {return slot;}
// .... all routines the same as in Class Array.......
// Routines specific to this class
void operator=(const CharArray&);
RigiBool operator==(const CharArray&) const;
void Create(char*);
void Create(char*,int);
void Create(int, char*);
void Add(char*);
void Add(CharType&);
void Addob(RigiObject& ob)
{Array::Add(slot++,&ob);}
void Append(char*);
char* Concat(char);
int FindIndex(char*);
char* Remove()
{return ((CharTypePtr)Array::Remove(--slot))->string();}
ObjectPtr Pop()
{return (Array::Remove(--slot));}
ObjectPtr Look(int i)
{return (Array::At(i));}
void Empty();
virtual unsigned int Size() const
{return slot;}
char* Peek();
char* At(int);
};
#endif
What's wrong with the code?
Assuming that the type RigiBool in the declaration RigiBool operator==(const CharArray&) const; is not defined in one of the headers "array.h" "chartype.h" I think you should include the header containing definition of the type, and just to be sure the "object.h" too.
For cases where a header file uses values of class type variables (not pointers and references) it is recommended to include the headers that contain the class definition. Otherwise a simple forward declaration should be enough.
The types RigiBool, RigiObject and ObjPtr are not available in chararray.h: you need to include object.h (plus whatever else defines RigiBool if CCHeader.h doesn't) -- similarly for RigiBool and RigiFalse in object.h
// somewhere at the top of chararray.h
#include "object.h"
Note: if you define virtual member functions in RigiObject, you should declare the destructor virtual as well
Note: You already have include guards in the #included headers, no need to put them around the #include directives -- doing otherwise indicates (falsely in your case) that you are doing conditional compiling
//chararray.h
#ifndef CHARARRAYH
#define CHARARRAYH
#include "array.h"
#include "chartype.h"
...
//object.h
#ifndef OBJECTH
#define OBJECTH
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "CCheader.h"
...
It's hard to say from the little information but I would assume RigiBool is a derived class of RigiObject? When you now reference RigiBool in chararray.h it has to know the RigiObject base class but the RigiObject also needs to know about RigiBool. So you can't declare the base class without knowing the derived RigiBool. Try if forward declaring RigiBool in object.h helps to break the cycle.
I have this annoying error in my program.
"Vehicle" is the Base class.
"Bicycle" extends this class.
#ifndef BICYCLE_H
#define BICYCLE_H
#include "Vehicle.h"
#include <string>
#include <iostream>
using namespace std;
//Template class which is derived from Vehicle
template<typename T>
class Bicycle: public Vehicle
{
public:
Bicycle();
Bicycle(int, int, string, int);
~Bicycle();
//Redefined functions inherited from Vehicle
void move(int, int); // move to the requested x, y location divided by 2
void set_capacity(int); // set the capacity value; can't be larger than 2
};
Above is the Bicycle.h file (I do not have .cpp file for this class)
#ifndef VEHICLE_H
#define VEHICLE_H
#include "PassengerException.h"
#include <string>
#include <iostream>
using namespace std;
//ADD LINE HERE TO MAKE IT A TEMPLATE CLASS
template<typename T>
class Vehicle
{
public:
Vehicle(); //default contstructor
Vehicle(int, int, string, int); // set the x, y, name, and capacity
virtual ~Vehicle(); //destructor; should this be virtual or not???
//Inheritance - question #1; create these functions here and in Bicycle class
string get_name(); // get the name of the vehicle
void set_name(string); //set the name of the vehicle
void print(); // std print function (GIVEN TO YOU)
//Polymorphism - question #2
virtual void move(int, int); // move to the requested x, y location
virtual void set_capacity(int); // set the capacity value
//Operator overloading - question #3
Vehicle<T> operator+(Vehicle<T> &secondVehicle) const;
//Exceptions - question #4
T get_passenger(int) throw(PassengerException); // get the passenger at the specified index
void add_passenger(T) throw(PassengerException); // add passenger and the current passenger index
void remove_passenger() throw(PassengerException); // remove a passenger using current passenger index
protected:
int x_pos;
int y_pos;
string name;
int capacity;
T *passengers;
int current_passenger;
};
Above is the Vehicle.h file. I do not have .cpp for this either.
Also, what do the ifndef define endif mean? Do I have to use those? Are they required?
And, do their names have to be formatted like that?
class Bicycle: public Vehicle
Vehicle is a template, so you need this:
class Bicycle: public Vehicle<T>
The #ifndef and #define and #endif are called header guards and are used to prevent your header file from being included more than once, causing things (classes) to be declared more than once.
The ifndef define and endif are necessary for the actual base files, i.e. the c++ files themselves. Yes they are required, if you plan to use those functions and variables accordingly. Yes, their names have to be formatted that way, that's the way directives or in some cases flags must be formatted.
You have to put #endif at the end of your header files. These are so called define guards to prevent multiple inclusion of header files. See more at Include guard.