I have a template class called item.
I have another class having array of items - class hashTable.
I have third class which derived from hashTable - HSubject.
I'm confused about the using/include that I have to add to each class.
I tried many options but it produced run time errors.
code is attached here:
*item.h*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
enum state { empty, full, deleted };
template <class T, class K>
class item
{
// propeties and functions here
};
*hashTable.h*
#pragma once
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include "item.h"
using namespace std;
// tried adding this but didn't seem to work
//template <class T, class K>
//class item<T, K>;
template <class T, class K>
class hashTable
{
public:
vector<item<T, K>> hashT;
hashTable();
// propeties and functions here
};
*hashTable.cpp*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
#include "hashTable.h"
using namespace std;
template<class T, class K>
hashTable<T, K>::hashTable()
{}
// functions are written here
*HSubject.h*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
class item<list<string>, string>;
using namespace std;
class HSubject :
public hashTable<list<string>, string>
{
public:
HSubject();
// propeties and functions here
};
*HSubject.cpp*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
#include "hashTable.h"
#include "HSubject.h"
#include "item.h"
using namespace std;
HSubject::HSubject() : hashTable()
{
}
// functions are written here
Any help would be greatful. Thanks!
Related
I have to do polymorphism for the go() function but i cant figure out how to access private variable distance. The complier keeps showing that
‘int Transport::distance’ is private within this context
5 | distance = 100;
Here is my code
Transport.h
#ifndef TRANSPORT_H
#define TRANSPORT_H
#include <iostream>
using namespace std;
class Transport{
private:
int distance;
public:
int get_dist_travelled();
virtual void go();
};
#endif
Transport.cpp
#include "Transport.h"
int Transport::get_dist_travelled(){
return distance;
}
Horse.h
#ifndef HORSE_H
#define HORSE_H
#include <iostream>
#include "Transport.h"
using namespace std;
class Horse:public Transport{
public:
void go();
};
#endif
Horse.cpp
#include "Horse.h"
#include "Transport.h"
void Horse::go() {
distance = 100;
}
main.cpp
#include <iostream>
#include "Transport.h"
#include "Horse.h"
using namespace std;
int main(){
Horse kid;
kid.go();
cout<<kid.get_dist_travelled()<<endl;
}
You can on the one hand make the variable protected, or define the class as a friend class.
I have a vector of shared pointers of a pure virtual class IObserver (observers), the vector is a class which inherits IObserver. There is also a second class which inherits IObserver, HomeOwner. I want to cast Homeowner objects to the observers vector, so I can send them to the functions in SecurityStatus but not sure how I go about it. I'd like to do it behind the scenes rather than in the main program. The declaration for both is below.
Thanks!
#pragma once
#include "IObserver.h"
#include "HouseSecurity.h"
#include "SensorInfo.h"
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
// concrete observer
class HomeOwner : public IObserver{
SensorInfo currentSensorState;
string myName;
string myPhoneNumber;
string myEmail;
HouseSecurity &house;
public:
void Update();
void New();
HomeOwner(string, string, string);
};
#pragma once
#include "IObserver.h"
#include "HomeOwner.h"
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
#include <memory>
using namespace std;
// subject
class SecurityStatus{
vector <shared_ptr<IObserver>> observers;
public:
void AttachObserver(shared_ptr<IObserver>);
void DetachObserver(shared_ptr<IObserver>);
void NotifyObservers();
};
I know there are lot of similar questions, but I do not see my mistake here.
I'm trying to inherit this class :
#if !defined(__GRAPHE_H__)
#define __GRAPHE_H__
#include <queue>
#include <map>
#include <set>
#include <iostream>
using namespace std;
template <class S>
class Graphe{
public:
here :
#if !defined(__CARTE_H__)
#define __CARTE_H__
#include <cassert>
#include <istream>
#include <list>
#include <set>
#include <string>
#include "graphe.h"
#include "pointst.h"
using namespace std;
class Carte: public Graphe
{
but I have this error: expected class-name before ‘{’ token
{
I am not even using any functions from the parent class, so that's why I am not posting the whole code.
What I am missing?
The following code should fix the problem;
using namespace std;
template <class S>
class Carte: public Graphe<S>
{
Can't seem to find where the error is. I do not think there is a circular implementation issue. I am pretty sure all of the .h files are included where they need to be. Array is inheriting from BaseArray:
The error:
In file included from driver.cpp:6:0:
Array.h:10:1: error: expected class-name before ‘{’ token
{
BaseArray.h:
#ifndef _BASEARRAY_H_
#define _BASEARRAY_H_
#include <cstring>
template <typename T>
class BaseArray
{
public...
}
#include "BaseArray.inl"
#include "BaseArray.cpp"
#endif // !defined _BASEARRAY_H_
BaseArray.cpp:
#include "BaseArray.h"
#include <stdexcept>
#include <iostream>
//..Constructors, Destructors, Functions...
Array.h:
#ifndef _ARRAY_H_
#define _ARRAY_H_
#include "BaseArray.h"
#include <cstring>
template <typename T>
class Array: public BaseArray
{
public:
......
};
#include "Array.inl"
#include "Array.cpp"
#endif // !defined _ARRAY_H_
Array.cpp
#include "Array.h"
#include "BaseArray.h"
#include <stdexcept>
#include <iostream>
//
// Array
//
template <typename T>
Array <T>::Array (void): BaseArray<T>()
//...more and more code
Change this:
class Array: public BaseArray
to this:
class Array: public BaseArray<T>
since BaseArray is a template class.
Moreover, in your header file you don't put a semicolon at the end of your class.
Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".