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>
{
Related
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!
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();
};
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.
I am trying to compile my code, but I am getting an error with classes. One of the classes compiled fine(Example.h), but the other(Name.h) keeps giving me the class does not name type error. I think it has something to do with circular dependency, but how do I fix that without a forward deceleration?
Name.h
#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif
Example.h
#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{
};
#endif
I saw a post about using forward deceleration, but I need to access memebers from the Example class..
You have a circular dependency, where each header tries to include the other, which is impossible. The result is that one definition ends up before the other, and the name of the second is not available within the first.
Where possible, declare each class rather than including the entire header:
class Example; // not #include "Example.h"
You won't be able to do this if one class actually contains (or inherits from) another; but this will allow the name to be used in many declarations. Since it's impossible for both classes to contain the other, you will be able to do this (or maybe just remove the #include altogether) for at least one of them, which should break the circular dependency and fix the problem.
Also, don't use reserved names like _NAME, and don't pollute the global namespace with using namespace std;
see, here you are including #include "Example.h" in Name.h and #include "Name.h" in Example.h. suppose compiler compiles Name.h file first so _NAME is defined now, then it tries to compile Example.h here compiler wants to include Name.h but the content of Name.h will not be included in Example.h since _NAME is already defined, hence class Name is not defined inside Example.h.
you can explicitly do forward declaration of class Name; inside Example.h
Try this:
Name.h
#ifndef NAMEH
#define NAMEH
#include <iostream>
#include <string>
using namespace std;
//#include "Example.h"
class Example;
class Name{
};
#endif
Name.cpp
#include "Name.h"
#include "Example.h"
...
Example.h
#ifndef EXAMPLEH
#define EXAMPLEH
#include <iostream>
#include <string>
using namespace std;
//#include "Name.h"
class Name;
class Example{
};
#endif
Example.cpp
#include "Example.h"
#include "Name.h"
...
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".