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.
Related
I'm trying to add inheritance to my objects based on SFML and I can't solve a problem with errors
*expected class-name before '{' token*
and when I try to solve it by pre-declaring my parent class I'm getting stopped by
*invalid use of incomplete type 'class Object'*
I made Rectangle and Circle class with Object as parent class
Object.hpp
#ifndef OBJECT_HPP_INCLUDED
#define OBJECT_HPP_INCLUDED
#include "../MainClass.hpp"
#include "../Data/Data.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>
class Object
{
public:
Object();
virtual ~Object();
virtual void update() = 0;
virtual void draw() = 0;
};
#endif // OBJECTPARENT_HPP_INCLUDED
in Object.cpp file I only have definitions of constructor and deconstructor
Rectangle.hpp
#ifndef RECTANGLE_HPP_INCLUDED
#define RECTANGLE_HPP_INCLUDED
#include "Object.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include <iostream>
#include <cstdlib>
class Object; // <--- without this line *expected class-name before '{' token*, but with it *invalid use of incomplete type 'class Object'*
class Rectangle : public Object
{
public:
Rectangle(/*args*/);
virtual ~Rectangle();
//some functions
void update();
void draw();
private:
//some members
};
#endif // RECTANGLE_HPP_INCLUDED
and Circle.hpp
#ifndef CIRCLE_HPP_INCLUDED
#define CIRCLE_HPP_INCLUDED
#include "Object.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include <iostream>
#include <cstdlib>
class Object; // <--- without this line *expected class-name before '{' token*, but with it *invalid use of incomplete type 'class Object'*
class Circle: public Object
{
public:
Circle(/*args*/);
virtual ~Circle();
//some functions
void update();
void draw();
private:
//some members
};
#endif // CIRCLE_HPP_INCLUDED
I don't know what to do, I tried changing the order of all includes, removing stuff and adding it, i searched everywhere but there was no good solution. Thank you for all answers
just insert ' #include "Object.hpp" ' in Circle.hpp and Rectangle.hpp and any other file like them that is using Object class:
#include "ObjectParent.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include "Object.hpp" // <----------
#include <iostream>
#include <cstdlib>
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>
{
I have this code in C++:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "gspace.h"
class myclass {
public:
void update(gspace **);
}
gspace is another class defined in gspace.h. The compiler however is telling me:
include/myclass.h error: ‘gspace’ has not been declared|
Is there anything wrong in what I'm doing?
EDIT:
#ifndef GSPACE_H
#define GSPACE_H
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
class gspace
{
public:
gspace();
Assuming you have even declared a class named "gspace" in gspace.h I could think of two possible errors.
1) class gspace would not be in global scope i.e could be in some namespace.
2) Please check if header gaurds used in gspace not "MYCLASS_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".
I am trying to get a project to compile, and I am running into a problem with incomplete types. Here is the output from make:
In file included from ../common/ClientCommunication.h:13,
from EchoService_stub.h:9,
from EchoService_stub.cpp:1:
../common/Naming_stub.h:16: error: field ‘clientCommunication’ has incomplete type
In file included from EchoService_stub.h:9,
from EchoService_stub.cpp:1:
../common/ClientCommunication.h:20: error: field ‘serverEndpoint’ has incomplete type
make: *** [EchoService_stub.o] Error 1
Here are my header files in question:
In Naming_stub.h:
ifndef NAMING_STUB_H__
#define NAMING_STUB_H__
#include <string>
#include <string.h>
#include "Naming.h"
class ClientCommunication;
class Naming_stub : public Naming {
private:
ClientCommunication clientCommunication;
protected:
// Protected Methods Here.
public:
// Public Methods Here.
};
#endif
In Naming.h
#ifndef NAMING_H__
#define NAMING_H__
#include "Remote.h"
#include "../util/RemoteException.h"
class Naming {
private:
protected:
public:
virtual Remote* lookup(std::string lookupURL) throw (RemoteException);
virtual void bind() throw (RemoteException);
~Naming();
};
#endif // NAMING_H__
Finally, there is the ClientCommunication.h file:
#ifndef CLIENT_COMMUNICATION_H__
#define CLIENT_COMMUNICATION_H__
#include <iostream>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "../util/RemoteTypes.h"
#include "Naming_stub.h"
struct Endpoint;
class ClientCommunication {
private:
int socket_fd;
struct Endpoint serverEndpoint;
protected:
public:
ClientCommunication(const struct Endpoint& serverEndpoint_in);
struct Buffer send(struct Buffer& callBuffer);
~ClientCommunication();
};
#endif // CLIENT_COMMUNICATION_H__
Sorry that this is so long, but there is one more header file. The Endpoint struct is declare in RemoteTypes.h, which looks like the following:
#ifndef REMOTE_EXCEPTION_H__
#define REMOTE_EXCEPTION_H__
#include <netinet/in.h>
struct Endpoint {
int port;
char* service_id;
char* server_ip;
struct sockaddr_in host_name;
};
struct Buffer {
char* contents;
int size;
};
#endif
Thanks!
You cannot declare a field using an incomplete type. You have to declare a pointer instead.
private:
ClientCommunication clientCommunication; // requires #include
ClientCommunication *clientCommunication; // works with incomplete type
Otherwise, you have to include the client communication header in the first header.
ifndef NAMING_STUB_H__
#define NAMING_STUB_H__
#include "ClientCommunication.h"
ClientCommunication should be fully declared in Naming_stub.h before the declaration of Naming_stub, or its clientCommunication member should become a pointer/reference. Only those work with incomplete types. Similar advice applies to Endpoint in ClientCommunication.h.
As far as I can tell, you don't have #include "ClientCommunication.h" or #include "RemoteTypes.h" anywhere
So, #include "RemoteTypes.h" in ClientCommunication.h and #include "ClientCommunication.h" in Naming_stub.h