Multiple Syntax Errors When Referencing Another Class in Include File [duplicate] - c++

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 years ago.
Have two files:
CIoHandler.h
#pragma once
#include "CSession.h"
class CIoHandler
{
public:
CIoHandler();
~CIoHandler();
void AuthenticateUser();
void ConvertUnicodeToAscii();
void DoNTLMAuth();
void GetAndSetSocket();
void GetElevatedToken();
void GetHeaderMessage();
void GetUserNameW();
void HandleOperatorMessage();
void Init(CSession *cSession);
void IsLocalAccount();
int IsLoopBack();
void IsSPNFQDNName();
int IsSPNLocalInterface();
void IsSPNPresentInRegistry();
void IsTimedOut();
void IssueReadFromSocket();
void OnDataFromSocket();
void OnReadFromPipeCompletion();
void ParseAndValidateAccount();
void ProcessAuthenticationLine();
void ProcessCommandLine();
void ProcessDataFromSocket();
void ReadRegistryKey();
void SendDetailsAndAskForLicense();
void SendMessageToClient();
void SendTerminateString();
void SetPrivilege();
void WriteMessageToClientDirectly();
void WriteToClient();
void WriteToServer();
void WriteToSocket();
};
CSession.h
#pragma once
#include "CIoHandler.h"
class CSession
{
private:
CIoHandler* _CIoHandler;
public:
CSession();
~CSession();
void CollectPeerInfo();
void FreeInitialVariables();
void GetRegistryValues();
void Init();
void IsAnAdministratorOrMember();
void SecpSetIPandPort();
void Shutdown();
void WaitForIO();
};
When compiling with Visual Studio 2019 get the following errors:
CSession.h(8,13): error C2143: syntax error: missing ';' before '*'
CSession.h(8,13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
CSession.h(8,26): error C2238: unexpected token(s) preceding ';'
CIoHandler.h(16,22): error C2061: syntax error: identifier 'CSession'
CIoHandler.h(16,22): error C2061: syntax error: identifier 'CSession'
Removing
#include "CIoHandler.h"
private:
CIoHandler* _CIoHandler;
From CSession.h it compiles
What do I need to so I can use CIoHandler in CSession class definition

You have a cyclic include dependency. To break the cycle replace
#include "CIoHandler.h"
with a forward declaration.
class CIoHandler;

Related

I don't understand this error when passing paramters by reference

I'm trying to run this program in another file(main.cc) but I get an error on this class, where this is the error.
ERROR
./Library.h:15:24: error: expected ')'
Library(Book[] &book, int &currentNumOfBooks);
^
./Library.h:15:16: note: to match this '('
Library(Book[] &book, int &currentNumOfBooks);
^
CODE SOURCE:
class Library{
public:
Book booksLibrary[MAX_ARR_SIZE];
int currentNumOfBooksLibrary;
Library();
Library(Book[] &book, int &currentNumOfBooks);
void addBook(Book &book);
void print();
private:
}:

14 errors in vs 2017 i think to do with it not recognising my classes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i fixed it there is no need for this question now thank you for your help
i have some errors on my program in vs 2017 that sometimes go and sometimes stay. i think it is not recognising my classes and i think its something to do with the #include not working properly as in the past i have had a single similar error before (something like ; missing before identifier) which stopped when I forward declared instead of #included please help.
form class
#pragma once
#include "IContain.h"
#include <vector>
#include "Rect.h"
class Form : public IContain
{
public:
Form();
~Form();
bool paint() override;
bool init() override;
Rect<int> Rect() override;
bool hasfocus() override;
Graphics& getgraphics() override;
bool add(IStaticControl & control) override;
bool remove(IStaticControl & control) override;
bool update() override;
pixelvec mousepos() override;
private:
std::vector<IStaticControl> controls;
};
control interface class
#pragma once
#include "Graphics.h"
#include "Rect.h"
#include "IContain.h"
class IStaticControl
{
public:
IStaticControl();
~IStaticControl();
//actions
virtual bool init() = 0;
virtual bool paint(Graphics& gfx) = 0;
virtual void update() = 0; // check for input that matches this' conditions. check container's input
virtual void show() = 0;
virtual void hide() = 0;
//info
virtual Rect<int> rect() = 0;
virtual IContain& Container() = 0;
virtual bool rezizeable() = 0;
virtual bool hasfocus() = 0;
};
event class
#pragma once
#include <vector>
#include "EventBase.h"
#include <queue>
template<typename EventArgsT>
class Myevent
{
public:
Myevent(size_t handlerCount, size_t instanceCount, std::queue<EventBase>&
eventqueue) : Eventintances(instanceCount), handlers(handlerCount) {this->eventqueue = eventqueue};
~Myevent() {};
inline void raise(EventArgsT args) {eventqueue.push(Inst(args, handlers));};
size_t addhandler(void(*handler)(EventArgsT) , bool& success = false) {
size_t size = handlers.size;
for (size_t i; i >= size; i++) {
if (handlers[i] = NULL) { handlers[i] = handler; success = true;
return i; }
}
return 0;
};
void removehandler(size_t i) { handlers[i] = NULL };
class Inst : EventBase {
EventArgsT args;
public:
Inst(EventArgsT args, std::vector<void(*)(EventArgsT)>& handlers) {
this->args = args;
this->handlers = handlers;
};
// go to each function in handlers and invoke them with args
void process() override {};
private:
std::vector<void(*)(EventArgsT)>& handlers;
};
private:
std::vector<void(*)(EventArgsT)> handlers;
std::vector<Inst> Eventintances;
std::queue<EventBase>& eventqueue;
};
control container interface class
#pragma once
#include "Myevent.h"
#include "graphics.h"
#include "IStaticControl.h"
class IContain
{
public:
virtual bool paint() = 0;
virtual bool init() = 0;
virtual Rect<int> rect() = 0;
virtual bool hasfocus() = 0;
virtual Graphics& getgraphics() = 0;
virtual bool add(IStaticControl & control) = 0;
virtual bool remove(IStaticControl & control) = 0;
virtual bool update() = 0;
virtual pixelvec mousepos() = 0;
// same for keyboard input
private:
};
These are the errors
Error C2433 'IStaticControl::IContain': 'virtual' not permitted on data declarations c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2433 'IStaticControl::IContain': 'virtual' not permitted on data declarations c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2061 syntax error: identifier 'IStaticControl' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\icontain.h 17
Error C2061 syntax error: identifier 'IStaticControl' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\icontain.h 18
Error C2143 syntax error: missing ';' before '&' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2143 syntax error: missing ';' before '&' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2238 unexpected token(s) preceding ';' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2238 unexpected token(s) preceding ';' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
please help
update: there are 10 errors
the code has changed very slightly
You have cyclic dependencies. IStaticControl.h includes IContain.h, and IContain.h includes IStaticControl.h.
(You neglected to mention the names of your files, but they can be deduced from the error messages.)
This is why IContain is undefined in this line:
virtual IContain& Container() = 0;
... so the compiler thinks you're trying to declare a member variable called IContain as an implicit int:
virtual int IContain; & Container() = 0;
This is why it's complaining about data members being virtual, implicit int, and syntax errors involving ; and &.

C2039: Class is not a member of Namespace

Mage/Interface/Context.h
#pragma once
#include <Mage/Interface/Element.h>
#include <Mage/Renderer/RenderingContext.h>
#include <Mage/Renderer/VertexBuffer.h>
#include <glm/glm.hpp>
namespace Mage {
namespace Interface {
class Context {
protected:
RenderingContext* ctx;
VertexBuffer* vbo;
glm::mat4 projection;
Mage::Interface::Frame* uiParent;
public:
Context(RenderingContext* ctx);
~Context();
void render();
Mage::Interface::Frame* createFrame();
};
}
}
Mage/Interface/Element.h
#pragma once
#include <vector>
#include <Mage/Interface/Context.h>
#include <glm/glm.hpp>
namespace Mage {
namespace Interface {
class Element {
protected:
Mage::Interface::Context* ctx;
std::vector<Element*> children;
glm::vec3 position;
float scale;
public:
virtual void draw();
void attach(Element* child) {
this->children.push_back(child);
}
inline glm::vec3 getPosition() {
return this->position;
}
float getScale() {
return this->scale;
}
};
// Frame is an untextured, single colour quad. Frame may contain other
// Elements.
class Frame : public Element {
public:
Frame();
Frame(glm::vec3 pos);
Frame(float width, float height);
Frame(glm::vec3 pos, float width, float height);
};
}
}
This gives me the following errors:
Error C2039 'Context': is not a member of 'Mage::Interface' Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
Error C2238 unexpected token(s) preceding ';' Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
Error C2143 syntax error: missing ';' before '*' Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
When I take out Mage::Interface::Context* ctx, the code compiles fine. I figured I must have missed a semi colon, but I can't see it - it all seems to check out just fine to me.
You have a circular dependency. Element.h includes Context.h and Context.h includes Element.h, that's not going to work.
The way to solve that is to forward-declare types instead of including their headers whenever you can, it'll also reduce compile times.

symbol cannot be used in a using-declaration

I've got a header in which my base problem is with the using keyword.
#ifndef SHAPEFACTORY_H__
#define SHAPEFACTORY_H__
#include <istream>
#include <map>
#include <string>
#include "shape.h"
/* thrown when a shape cannot be read from a stream */
template<class T>
class WrongFormatException { };
template<class T>
class ShapeFactory
{
public:
using createShapeFunction=Shape<T>*()(void);
static void registerFunction(const std::string &, const createShapeFunction *);
static Shape<T> *createShape(const std::string &);
static Shape<T> *createShape(std::istream &);
private:
std::map<std::string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory<T> *getShapeFactory();
};
#endif
And I've got some errors which I can't resolve.
1>shapefactory.h(21): error C2873: 'createShapeFunction' : symbol cannot be used in a using-declaration
1>shapefactory.h(29) : see reference to class template instantiation 'ShapeFactory<T>' being compiled
1>shapefactory.h(21): error C2143: syntax error : missing ';' before '='
1>shapefactory.h(21): error C2238: unexpected token(s) preceding ';'
1>shapefactory.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>shapefactory.h(22): error C2143: syntax error : missing ',' before '*'
1>shapefactory.h(26): error C2065: 'createShapeFunction' : undeclared identifier
1>shapefactory.h(26): error C2059: syntax error : '>'
1>shapefactory.h(29): error C2143: syntax error : missing ';' before '}'
1>shapefactory.h(29): fatal error C1004: unexpected end-of-file found
Any idea would be great.
It seems that the compiler does not support the alias decladation. Substitute it for a typedef declaration. For example (at least the code is compiled)
#include <map>
#include <string>
template <typename T>
class Shape;
template<class T>
class ShapeFactory
{
public:
typedef Shape<T>* createShapeFunction(void);
static void registerFunction(const std::string &, const createShapeFunction *);
static Shape<T> *createShape(const std::string &);
static Shape<T> *createShape(std::istream &);
private:
std::map<std::string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory<T> *getShapeFactory();
};
int main()
{
return 0;
}

Class instance variables inside of class cause compiler errors

The compiler is giving me the following complaints about the following class:
class AguiWidgetBase
{
//variables
AguiColor tintColor;
AguiColor fontColor;
//private methods
void zeroMemory();
virtual void onPaint();
virtual void onTintColorChanged(AguiColor color);
void (*onPaintCallback)(AguiRectangle clientRect);
void (*onTintColorChangedCallback)();
public:
AguiWidgetBase(void);
~AguiWidgetBase(void);
void paint();
void setTintColor(AguiColor color);
AguiColor getBackColor();
};
Warning 13 warning C4183: 'getBackColor': missing return type; assumed to be a member function returning 'int' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 1 error C2146: syntax error : missing ';' before identifier 'tintColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 10 error C2146: syntax error : missing ';' before identifier 'getBackColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 4 error C2146: syntax error : missing ';' before identifier 'fontColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 8 error C2061: syntax error : identifier 'AguiRectangle' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 17
Error 7 error C2061: syntax error : identifier 'AguiColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 16
Error 9 error C2061: syntax error : identifier 'AguiColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 25
This should be working, I'm including the headers for those classes.
This is the h file:
//integer Point class
class AguiPoint {
int x;
int y;
public:
int getX();
int getY();
void setX(int x);
void setY(int y);
void set(int x, int y);
AguiPoint(int x, int y);
AguiPoint();
std::string toString();
std::string xToString();
std::string yToString();
};
//floating version of Agui Point
class AguiPointf {
float x;
float y;
public:
float getX();
float getY();
void setX(float x);
void setY(float y);
void set(float x, float y);
AguiPointf(float x, float y);
AguiPointf(AguiPoint p);
AguiPointf();
std::string toString();
std::string xToString();
std::string yToString();
};
//Integer rectangle class
class AguiRectangle {
int x;
int y;
int width;
int height;
public:
bool isEmpty();
int getTop();
int getLeft();
int getBottom();
int getRight();
AguiPoint getTopLeft();
AguiPoint getBottomRight();
};
class AguiColor {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
void verifyColorBounds();
public:
AguiColor(int r, int g, int b, int a);
AguiColor(float r, float g, float b, float a);
AguiColor();
int getR();
int getG();
int getB();
int getA();
};
Thanks
I include the main header in the WidgetBase and the main header includes the base types before it includes the widgetbase
It seems you are not including the headers in the right order.
C++ is very sensitive to the order in which identifiers are declared. The compiler will process a source file (and any #include-s it finds along the way) in a sequential order and for every (non-builtin) identifier, the compiler must have seen a declaration before you can use it.
If you already have the headers included i'd guess you need to fully qualify the names because of a namespace or that Aguiwidgitbase is in the wrong namespace? either way check the namespaces in the headers you included
Check header files and namespaces. You may also need forward declarations.
You may have a problem of include dependencies. Header guards preventing multiple definition (#pragma once, or #ifndef HEADER #define HEADER) can block your inclusion.
One answer can be forward declarations... for pointers to class, not for class members though (need to know storage size at compile time).