Class pointer to different .h file - c++

Helo stack people, I need your help in work I make. So I have to .h files first one is Course and the second is Student and I try to create function call getCourses but Unfortunately It's not going so well.My realization "Course ** courses" do not pass compiling and I do not know why not. I would appraise if you can help me to Understand my mistake and help me to fix them thanks.
getCourse - return list of courses
my Course .h file
#ifndef _CORSE_H
#define _CORSE_H
#include <iostream>
#include "Student.h"
class Course
{
public:
void init(std::string getName, int test1, int test2, int exam);
std::string getName();
int* getGrades();
double getFinalGrade();
private:
std::string _name;
int _exam;
int _test1;
int _test2;
};
#endif
My Student .h file-
#ifndef _STUDENT_H
#define _STUDENT_H
#include <iostream>
#include "Course.h"
class Student
{
public:
void init(std::string name, Course** courses, int crsCount);
std::string getName();
void setName(std::string name);
double getAvg();
int getCrsCount();
Course** getCourses();
private:
std::string _name;
Course** courses;
int _crsCount;
};
#endif
My get course function -
Course** student::getCourses()
{
return(this->courses);
}
The problem in the "Course** getCourses();"initialization and this applies also to the init function and Course** getCourses(); function.
error C4430: missing type specifier - int assumed. Note:C++ doe not support default-int

You have a circular dependency - each header tries to include the other, and you end up with one class defined before the other. This gives errors, because you have to declare a type before you can use its name.
Course doesn't depend on Student at all, so just remove the #include from that file.
The definition of Student only uses pointers to Course so it doesn't need the full definition. It only needs to know that the class exists, so you can replace the #include with a declaration:
class Course;
A couple more points:
both headers should include <string> since they use std::string; but not <iostream> since they don't use any I/O streams;
names beginning with an underscore and a capital, like _CORSE_H, are reserved. You should remove the underscores.
you've mis-capitalised Student in the final code snippet.

Besides what Mike Seymour wrote your course function uses lowercase student in
Course** student::getCourses()
Whereas you declared the class with a capital S. Case does matter.

Related

'Cashier' was not declared in this scope

I have this piece of code
#ifndef STATION_H
#define STATION_H
#include <vector>
#include "Dispenser.h"
#include "Cashier.h"
//class Cashier;
class Station
{
private:
int price;
int dispenser_count;
int cashier_count;
std::vector<Dispenser> dispensers;
std::vector<Cashier> cashiers;
public:
Station(int, int, int);
int GetPrice() const { return price; }
Dispenser *LookForUnoccupiedDispenser(int &id);
Dispenser *GetDispenserByID(int id);
Cashier *LookForUnoccupiedCashier();
};
#endif // STATION_H
When I have the class Cashier line commented, the compiler fails with a 'Cashier' was not declared in this scope error even though Cashier.h was included. Uncommenting it makes it possible to compile but I'm concerned that it shouldn't be happening.
Cashier.h
#ifndef CASHIER_H
#define CASHIER_H
#include "Station.h"
class Station;
class Cashier
{
private:
bool busy;
Station *at_station;
public:
Cashier(Station *employer);
bool IsBusy() const { return busy; }
};
#endif // CASHIER_H
How is it possible? It's clearly declared in the header file and as far as I know, #include does nothing more than pasting the content of a file into another one.
Thank you for the answers in advance!
Your station.h includes cachier.h. This is an example of cyclic dependency. Given that you only have a pointer to Station in Cachier I'd suggest removing the dependency of station.h and forward declare it.
An #include is literally nothing more than verbatim copy and paste of that file into the current compilation unit. The guards protect you from the effect of an infinite include cycle, but due to the guards one of the #includes does nothing, i.e. does NOT suck in the declaration (nor definition) of the respective class. This results in the error you get. In station.h the compiler has never seen any mention of the Cachier type when it sees the Station type.

2 classes in 2 header files

I am trying to compile a program that has 2 .h and 3.cpp . I keep getting the same error message: "error: ‘Patrons’ does not name a type Patrons match;"
when I put the whole code in one single .cpp file, I have no errors and it compiles, but for my assignment I need to do it in separate files.
I think I wrote the code right so I dont know why I get the error message.
// class patrons.h
using namespace std;
class Patrons //named it patrons because this is where i have a list of all the patrons
{
int patronscnt;
public:
std::list<string>::iterator PL;
std::list<string> patslist;
string name;
void patronslist();
void addpatron();
void removepatron();
void editpatron();
};
-
// class patron.h
using namespace std;
class Patron //class decleration. Named it patron because it has the information of one patron
{
string x;
string input;//class members
Patrons match;
public:
void ID();
void email();
void phone();
void address();
void borrowstatus();
void finestatus();
void check(string);
//update
};
You could include patrons.h in patron.h.
You accomplish this by adding the following to the top of the patron.h file:
#include "patrons.h"
However in my opinion, it is generally better to store a pointer to an object instead of the entire object. If you were to switch the match variable in patron.h to be a Patrons pointer:
Patrons *match;
Then instead of including patrons.h, you could forward declare the Patrons class, by adding the following to the top of the patron.h file:
class Patrons;
Then if needed you could include patrons.h in your patron.cpp file. Forward declaring will help you from running into circular dependencies.
You need to include Patrons.h in Patrons: #include "Patrons.h"

c++ how declare function returning vector of objects

and I struggle with function which should return vector of objects but for some reason it throws errors all the time, telling that my object is undeclared identifier and vector of this objects is not valid template and points me to .h file where I declare function.
I will appropriate any explanation what that mean and how to fix this. bellow I place code from my class and starting files.
#ifndef SETUPW_H
#define SETUPW_H
#include"Square.h"
#include <iostream>
#include<string>
#include<fstream>
#include<vector>
std::vector<std::ifstream> allText();
std::ifstream loadTxt(std::string txt);
void printByLine(std::ifstream& txt);
std::vector<square> allSquares();//compiler points me to this line and that one bellow
void whichSQ(int sqNum, std::vector<square> sq);
#endif
and my class:
#ifndef SQUARE_H
#define SQUARE_H
#include"player.h"
#include"setupW.h"
#include<iostream>
#include<string>
#include<fstream>
class square
{
public:
square(std::string name, int sqNumber, std::string description, int exits, int object);
void loadSQ(std::ifstream& inFile);
void printSQ();
private:
int mSqNumber;
std::string mName;
std::string mDescription;
int mExits;
int mObject;
};
#endif
The problem arises because you have a circular dependency here. In square.cpp you firstly include square.h. But square.h contains this line #include"setupW.h" (before your class declaration). Therefor the declarations of your functions will appear before the declaration of your square class. That causes the compiler to mutter that square is not declared (at that time) when he reads std::vector<square>.
The most easiest solution would be to simply remove the include, because it is, as far as I can tell, unneccessary.

header file ~synatx error(asking for a parantheses)

i'm having the fallowing header file. I get this error: expected ')' before 'A' why is this?
I tried to rewrite and to replace... i`m out of ideas and i dont know what may be the root of the problem...
#ifndef UICONSOLE_H_
#define UICONSOLE_H_
#include "Catalog.h"
#include <string>
using namespace std;
class UIconsole{
public:
UIconsole(Catalog A); // error here.
void runUI();
private:
void showMenu();
string getString();
int getOption();
void addStudent();
void removeStudent();
void editStudent();
void printStudent();
void printAllStudents();
void addAssignment();
void removeAssignment();
void editAssignment();
void printAssignment();
void printAllAssignment();
void printAllUnder5();
void sortAlphabetically();
void searchById();
};
#endif /* UICONSOLE_H_ */
edit, with the content of a dependency header:
#ifndef CATALOG_H_
#define CATALOG_H_
#include <string>
#include "UIconsole.h"
#include "Catalog.h"
#include "StudentRepository.h"
#include "StudentValidator.h"
using namespace std;
class Catalog{
private:
StudentRepository studRepo;
StudentValidator studValid;
public:
Catalog(StudentRepository stre, StudentValidator stva):studRepo(stre),studValid(stva){};
void addNewStudent(string name, int id, int group);
void removeStudent(string name);
void editStudent(int name, int id, int group);
Student seachStudent(string name);
};
#endif /* CATALOG_H_ */
Your Catalog.h file has a couple of unnecessary #include directives:
#include "UIconsole.h"
#include "Catalog.h"
Get rid of these from the particular file.
The #include "Catalog.h" is unnecessary, but harmless (because of the include guards). However, the #include "UIconsole.h" causes the declaration of class UIconsole to be processed before the declaration of class Catalog. So when the compiler hits the
UIconsole(Catalog A);
line it still has no idea what Catalog is.
Another thing that's unrelated to this problem but should be fixed is the
using namespace std;
directives in the header files. That's a bad practice that should be avoided - in header files you should generally specify the full name of types in the std namespace:
void addNewStudent(std::string name, int id, int group);
void removeStudent(std::string name);
Forcing a namespace into the global namespace on all users of a header can cause problems when there's a name conflict (essentially, you're removing the ability for users to control name conflicts with namespaces if you force the directive on them).
You have a circular include: Catalog includes Console, which in turn includes catalog again. Now the safeguard prevent the infinite include, but it does not magically solve the problem.
Lets assume that in your case, Catalog is included first:
The compiler does:
include Catalog. h
include Console.h
Include Catalog.h but actually skip the content because of the safeguard.
continue with processing Console.h but it has not seen the Catalog class yet.
You need to solve the circular include. One way would be to put a pointer instead of an object Catalog and have a forward declaration. Or you can simply remove the include UIconsole.h from Catalog.h, as it does not seem to be needed in the header.

Another 'x was not declared in this scope'

this is my first question here.
Writing some code, i receive this error from g++: "Entity was not declared in this scope", in this context:
#ifndef Psyco2D_GameManager_
#define Psyco2D_GameManager_
#include <vector>
#include "Entity.h"
namespace Psyco2D{
class GameManager{J
private:
std::vector<Entity> entities;
};
}
#endif
This is the content of Entity.h:
#ifndef Psyco2D_Entity_
#define Psyco2D_Entity_
#include <string>
#include "GameManager.h"
#include "EntityComponent.h"
namespace Psyco2D{
class Entity{
friend class GameManager;
private:
/* Identificatore */
std::string _name;
/* Components list */
std::map<const std::string, EntityComponent*> components;
protected:
Entity(const std::string name);
public:
inline const std::string getName() const{
return this->_name;
}
void addComponent(EntityComponent* component, const std::string name);
EntityComponent* lookupComponent(const std::string name) const;
void deleteComponent(const std::string name);
};
}
#endif
If i use std::vector<class Entity> instead of std::vector<Entity> it works.
Why?
Thanks to all =)
The problem is you have a cyclic dependency. Take out #include "GameManager.h" in Entity.h, since you don't actually need it in that header. (Up-vote this answer, which first pointed it out.)
Note the guards are actually the problem; but don't take them out! You just need to minimize the includes you have, and declare (and not define) types when possible. Consider what happens when you include Entity.h: As some point it includes GameManager.h, which in turn includes Entity.h. At this point, Entity.h already has its header guard defined, so it skips the contents. Then the parsing of GameManager.h continues, where upon it runs into Entity, and rightfully complains it is not defined. (Indeed, this is still the process of including GameManager.h in the first inclusion of Entity.h, far before Entity is defined!)
Note your numerous edits demonstrate why it's important to post real code, not re-synthesized code. You need real details to get real answers.
Old:
Entity is in the Psyco2D namespace. You need to specify that:
class GameManager{
private:
std::vector<Psyco2D::Entity> entities;
};
Assuming the first snippet is part of GameManager.h you have a circular header dependency. I believe you can fix this by changing the GameManager.h include in Entity.h to class GameManager; instead.
Additionally as GMan noted, Entity is in a namespace and you need to qualify Entity with the namespace name.
Remove the Psyco2D-namespace and it will work without declaring "class Entity".