Class header files - c++

main.cpp
int main()
{
return 0;
}
cell.h
#pragma once
class _cell {};
cell.cpp
#include "cell.h"
experiment.h
#pragma once
class _experiment
{
_cell cell;
};
experiment.cpp
#include "experiment.h"
#include "cell.h"
Error:
experiment.h(5): error C2146: syntax error : missing ';' before identifier 'cell'
experiment.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
experiment.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
This has been driving me nuts. Any help? Thanks!

You have to #include <cell.h> from experiment.h as otherwise _cell is not defined when used in experiment.h.

Move the include from cell.cpp to the .h
.h:
#pragma once
#include "cell.h"
class _experiment
{
_cell cell;
};
.cpp:
#include "experiment.h"
Classes need the definition of members to contain instances of them.

Related

How do you forward declare a imported TLH namespace so that you can use it in a structure definition?

If I declare this struct in my CDialog header:
struct S_MWB_AUTO_ASSIGN_SETTING
{
bool bIncludeReferencedWeeks{};
bool bAvoidConflicts{};
MSAToolsLibrary::AssignmentType eAssignType;
CString strStartingName;
void (CChristianLifeMinistryEntry::* pfnSetAssignName)(CString);
};
Then, I can use it in the dialog functions. Example:
std::vector<S_MWB_AUTO_ASSIGN_SETTING> vecAutoAssignSettings =
{
{true, true, MSAToolsLibrary::AssignmentType_Host, L"Name 1", &CChristianLifeMinistryEntry::SetVideoHost },
{true, true, MSAToolsLibrary::AssignmentType_CoHost, L"Name 2", &CChristianLifeMinistryEntry::SetVideoCohost },
};
This compiles and builds fine. Now, I tried to move my struct definition into another header file. Snippet:
namespace CChristianLifeMinistryDefines
{
struct S_MWB_AUTO_ASSIGN_SETTING
{
bool bIncludeReferencedWeeks{};
bool bAvoidConflicts{};
MSAToolsLibrary::AssignmentType eAssignType;
CString strStartingName;
void (CChristianLifeMinistryEntry::* pfnSetAssignName)(CString);
};
}
Now it will not compile:
error C2653: 'MSAToolsLibrary': is not a class or namespace name
error C3646: 'eAssignType': unknown override specifier
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
The MSAToolsLibrary namespace is created automatically by a TLH file. The problem is that the wrapper class that uses the TLH file also references the same header I am trying to add the struct too:
#pragma once
#include "ChristianLifeMinistryDefines.h"
#include <map>
#include <vector>
#ifdef _WIN64
#import "..\\..\\MSAToolsLibrary\\MSAToolsLibrary\\bin\\x64\\Release\\MSAToolsLibrary.tlb" raw_interfaces_only named_guids
#else
#import "..\\..\\MSAToolsLibrary\\MSAToolsLibrary\\bin\\x86\\Release\\MSAToolsLibrary.tlb" raw_interfaces_only named_guids
#endif
using namespace CChristianLifeMinistryDefines;
I tried to #include the TLH wrapper header. No joy.
At the moment I have kept the struct in the CDialog header.
I understand about forward declaring structures etc. This feels like a circular reference issue but I cant establish how to forward declare the namespace that is built by the TLH import.

C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I'm using this codeproject: http://www.codeproject.com/Articles/4682/Voice-chat-using-a-client-server-architecture
class mysocket : public CSocket
{
public:
CDialog *dlg;
CString name;
int closeflag;
mysocket();
setparent(CDialog *dlg);
//void OnConnect(int errcode);
void OnReceive(int errcode);
void OnClose(int errcode);
};
But I get this error in numerous places:
C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I think it has something to do with me using the latest version of visual studio (2013).

Adding a derived class causes ghost errors [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 years ago.
My base class (State.h):
#pragma once
#include <Windows.h>
#define WIN32_LEAN_AND_MEAN
#include <gl\GL.h>
#include <gl\GLU.h>
#include "StateManager.h"
class State
{
public:
virtual ~State();
virtual void update(double dt){}
virtual void render(){}
};
The derived class(State_Test.h):
#pragma once
#include "State.h"
class State_Test : public State
{
public:
State_Test();
~State_Test();
};
The errors it gives are in a different class (StateManager.h):
#pragma once
#include "State.h"
#include <map>
#include <string>
class StateManager
{
public:
StateManager();
~StateManager();
std::map<std::string, State *> m_StateMap;//error C2976: 'std::map' : too few template arguments | error C2065: 'State' : undeclared identifier | error C2059: syntax error: '>'
std::string m_CurrentState;
void AddState(std::string stateId, State *state);//error C2061: syntax error : identifier 'State'
void ChangeState(std::string stateId);
};
Also there's this warning:
Warning 5 warning C4005: '_malloca' : macro redefinition c:\program files (x86)\microsoft visual studio 12.0\vc\include\crtdbg.h 586 1
Syntactically it looks correct, VS doesn't underline anything, and this code had worked before without error. I'm just wondering if anybody has seen this error before or knows what the cause of it is?
You have circular dependency problem: State.h includes StateManager.h, which includes State.h.
Since StateManager uses only pointer to State, remove this include and forward declare State:
#pragma once
//#include "State.h" -> remove this
#include <map>
#include <string>
class State; //-> add this
class StateManager
{
public:
StateManager();
~StateManager();
std::map<std::string, State *> m_StateMap;
std::string m_CurrentState;
void AddState(std::string stateId, State *state);
void ChangeState(std::string stateId);
};

How do I pass this object to another Object and access its variables using a friend class (c++)

So I have searched high and low and maybe I don't know what question I'm trying to ask, and if that's the case please help me figure out what that might be.
I am trying to print out the color of myCar object from the car class, using a function from the myMan object from the man class. I (think) I have the man class as a friend to the car class so I dont know why I cant access the cars private color variable. Can someone tell me why I can't access the cars private color variable? I have to use friend.
I have been trying to do this for more then 20 hours now and I'm at my wits end.I have included all the files I have because I have seen it done other ways (all in one file) and can't seem to reproduce it broken up like this.
Error 1 error C2433: 'man' : 'friend' not permitted on data declarations car.h line 14
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int car.h line 14
Error 3 error C2248: 'car::color' : cannot access private member declared in class 'car' man.cpp line 13
Error 4 error C2061: syntax error : identifier 'car' man.h line 16
Error 5 error C2660: 'man::printCarColor' : function does not take 1 arguments source.cpp line 16
Error 6 error C2061: syntax error : identifier 'car' man.h line 16
car.h
#pragma once
#include <iostream>
#include <string>
#include "man.h"
using namespace std;
class car
{
public:
car();
car(string);
~car();
friend man;
private:
string color;
};
car.cpp
#include "car.h"
car::car()
{
}
car::car(string c){
color = c;
}
car::~car()
{
}
man.h
#pragma once
#include <iostream>
#include <string>
#include "car.h"
using namespace std;
class man
{
public:
string name;
man();
man(string);
void printCarColor(car);
~man();
};
man.cpp
#include "man.h"
man::man()
{
}
man::man(string newname)
{
name = newname;
}
void man::printCarColor(car mycar){
cout << mycar.color;
}
man::~man()
{
}
source.cpp
#include <iostream>
#include <string>
#include "car.h"
#include "man.h"
using namespace std;
int main()
{
car myCar("green");
//cout << myCar.color;
man myMan("Jim");
myMan.name;
myMan.printCarColor(myCar);
system("pause");
return 0;
}
You have a circular include dependency. car.h includes man.h and man.h includes car.h. This cannot work. Remove #include "man.h" from car.h, use friend class man instead of friend man. – n.m.
** hope this isn't a bad way of answer this, credit goes to ^

How to avoid header file duplication?

In my program I got two header files named "invader.h" and "game.h". In game.h I include invader.h, and because I wanto to pass a pointer of the current game instance to an invader instance. I also include game.h in invader.h, but I got compile error. If I remove game.h from invader.h, it works fine. I already added include guard in each header files. Based on what I have found so far, I added a forward declaration of game class in invader.h, because what I need, is a pointer to the game instance in invader.h. But when I want to call a function of game in invader.cpp, it says pointer to incomplete class type is not allowed. What should I do to solve this problem?
Game.h
#ifndef GAME_H
#define GAME_H
#include "Tank.h"
#include "Invader.h"
#include "Block.h"
#include "Bullet.h"
class Game
{
private:
Tank tank;
Invader invaders[11][5];
Block blocks[4];
bool logicRequiredThisLoop = false;
public:
Game();
void initEntities();
Tank* getTank(){return &tank;};
Invader* getInvaders(){return &invaders[0][0];};
Block* getBlocks(){return &blocks[0];};
void updateLogic();
};
#endif
Invader.h
#ifndef INVADER_H
#define INVADER_H
#include "Entity.h"
class Game; //forward declaration of class Game
class Invader: public Entity
{
private:
Game* game;
public:
Invader(){};
Invader(Game*,char*,int,int,int,int,int,int);
void move(long delta);
void doLogic();
};
#endif
Invader.cpp
#include "Invader.h"
Invader::Invader(Game* game,char* sprite,int x,int y,int dx,int dy,int width,int height):Entity(sprite,x,y,dx,dy,width,height)
{
this->game = game;
}
void Invader::move(long delta)
{
if ((dx<0)&&(x<=10))
{
game->updateLogic();
}
if ((dx>0)&&(x>=390))
{
dx = -dx;
y -= dy;
}
x+=dx;
}
in Invader.cpp when I try to call updateLogic() which is a member function of Game class, an error occurs saying the pointer to an incomplete class is not allowed
Actually to be simple the most basic thing I want to know here is: in my code Game class has an invader type member variable, so how can I call member functions of Game class in invader class?li,e I said if I include Invader.h in Game.h and include Gameh.h in Invader.h an compile error occurs.
this is what in get when I include Game.h in Invader.h:
1>ClCompile:
1> Invader.cpp
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C2146: syntax error : missing ';' before identifier 'invaders'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2143: syntax error : missing ';' before '*'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): warning C4183: 'getInvaders': missing return type; assumed to be a member function returning 'int'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2065: 'invaders' : undeclared identifier
what should i do to solve this problem?
As a First understand what an Incomplete type means:
What leads to incomplete types?
If you cannot use Forward declarations without the type being Incomplete type then you shoud re-visit your design because something is wrong there.
You will need to provide the source code if you need a more detailed answer.
EDIT:
You need to include Game.h in Invader.cpp.
//Invader.cpp
#include "Invader.h"
#include "Game.h"
I will explain this in reference to two files i have at my local box right now.
server.h and worker.h
server.h:
#ifndef SERVER_H_
#define SERVER_H_
typedef struct _conf conf;
typedef struct _worker worker;
typedef struct _logger logger;
typedef struct _server server;
struct _server {
/* config */
conf *cfg;
/* socket */
int fd;
struct event_base *base;
struct event *signal;
/* workers */
worker **w;
/* log */
logger *log;
};
...
...
#endif /* SERVER_H_ */
worker.h
#ifndef WORKER_H_
#define WORKER_H_
#include <pthread.h>
typedef struct _server server;
typedef struct _worker worker;
struct _worker {
pthread_t t;
struct event_base *base;
struct evhttp *http;
server *s;
};
...
...
#endif /* WORKER_H_ */
As you can see both server and worker structs make a reference to each other which is solved by forward declarations at the top of .h files: e.g.
typedef struct _worker worker;
at top of server.h is sufficient for it to make a reference to worker struct.
You may want to check complete files for further reference here: https://github.com/abhinavsingh/pulsar/tree/master/include
Hope that helps.