2 classes in 2 header files - c++

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"

Related

How can I define 2 classes from the same header file, while one class depends on the other?

Im currently trying to implement a simple path-finding algorithm and need edges and nodes for it. I want to handle the implementation of those in one .h and one .cpp file. Right now I get the error "expected constructor, destructor or type conversion before ...".
I already tried separating both classes into 2 .h and .cpp-files, but that didnt work either. I've tried a lot of solutions provided for that error message, but nothing seems to work and I think there something Im missing right now.
My utilites.cpp file looks a bit like that
#include "utilities.h"
//Class Node
//Public
using namespace std;
Node::Node(string name)
{
this->name = name;
}
//Class Edge
//public
Edge::Edge(Node::Node nSource, Node::Node nTarget, int weight)
{
this->nSource = nSource;
this->nTarget = nTarget;
this->weight = weight;
}
and my utilities.h:
#ifndef UTILITIES_H
#define UTILITIES_H
#include <string>
#include <list>
class Node
{
public:
Node(std::string);
std::string name;
};
class Edge
{
public:
Edge(Node, Node, int);
Node nSource;
Node nTarget;
int weight;
};
#endif /* end of include guard: UTILITIES_H */
If I just use the Class Node, everything works.
But if I want implement Class Edge with the Class Node, I'll get the error previously mentioned. I think it is an easy solve, but I just cant figure it out.
I should say that I already tried it with
Edge::Edge(Node nSource, Node nTarget, int weight)
{
this->nSource = nSource;
this->nTarget = nTarget;
this->weight = weight;
}
but that just gave me the error "No matching function for call to 'Node::Node()'
The problem was that I was missing the curly braces after the default constructor of Node
Node(){};
Now it works as intended.
Thanks for the answers, they made me look at the default constructor closer again...

C++ Beginner : calling default vs custom constructor

Beginner here - but i was uncertain what exactly to search for this (presumably common) question.
I am working on a program where I have a given class (Dictionary). I am supposed to make a concrete class (Word) which implements Dictionary. I should mention that I am not to change anything in Dictionary.
After making a header file for Word, I define everything in word.cpp.
I am unsure if I am doing this correctly, but I make the constructor read from a given file, and store the information in a public member of Word.
(I understand that the vectors should be private, but I made it public to get to the root of this current issue)
dictionary.h
#ifndef __DICTIONARY_H__
#define __DICTIONARY_H__
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Dictionary
{
public:
Dictionary(istream&);
virtual int search(string keyword, size_t prefix_length)=0;
};
#endif /* __DICTIONARY_H__ */
word.h
#ifndef __WORD_H__
#define __WORD_H__
#include "dictionary.h"
class Word : public Dictionary{
public:
vector<string> dictionary_words;
vector<string> source_file_words;
Word(istream &file);
int search(string keyword, size_t prefix_length);
void permutation_search(string keyword, string& prefix, ofstream& fout, int& prefix_length);
};
#endif /* __WORD_H__*/
word.cpp
#include "word.h"
Word(istream& file) : Dictionary(istream& file)
{
string temp;
while (file >> temp)
{
getline(file,temp);
dictionary_words.push_back(temp);
}
}
In word.cpp, on the line "Word::Word(istream& file)", I get this error :' [Error] no matching function for call to 'Dictionary::Dictionary()'.
I've been told this is error is due to "Word's constructor invoking Dictionary's ", but I still don't quite grasp the idea well. I am not trying to use Dictionary's constructor, but Word's.
If anyone has an idea for a solution, I would also appreciate any terms related to what is causing this issue that I could look up - I wasn't even sure how to title the problem.
Your child class should invoke parent constructor, because parent object are constructed before child. So you should write something like:
Word::Word(isteam& file) : Dictionary(file)
{
...
}
Seems its better described here What are the rules for calling the superclass constructor?

C++ Custom Header File - Syntax Error C2061: identifier

I've been looking into Syntax Error C2061 for a while now, and I have come to understand that it is often caused by circular dependencies of header files. However, I believe I should've resolved this in my files yet I continue to have the issue.
Arc.h
#pragma once
#include <string>
using namespace std;
class Node;
class Arc
{
public:
Arc(Node &p_destination, const string &p_mode);
~Arc();
private:
string m_mode;
Node* m_destination;
};
Node.h
#pragma once
#include <string>
#include <vector>
using namespace std;
class Arc;
class Node
{
public:
Node(const string &p_name, const int &p_identifier, const float &p_latitude, const float &p_longitude);
~Node();
void set_arcs(Arc* p_arc) { m_arcs.push_back(p_arc); } //Line that causes the error
private:
std::vector<Arc*> m_arcs;
//Other Private Variables removed
};
The header files have both been included in the corresponding cpp files. Any help on this matter will be greatly appreciated!
Edit: Full Error Message below
"Syntax Error: identifier 'Arc'"
The problem is that the name "Arc" is already in use by a method in the global namespace. Either rename your class to an unused name or place it in a namespace which is not the global namespace.
You have a circular dependecy in you files. Arc depends on Node and Node depends on Arx. This cannot work, because you must include Arc in Node and also Node in Arc.
Forward declaration helps here a little bit but you put a using inside the header file. You shouldn't do that because then your Node and Arc is inside std. Look here for further clarification.
"using namespace" in c++ headers

Class pointer to different .h file

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.

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.