What is going on?
#include "MyClass.h"
class MyOtherClass {
public:
MyOtherClass();
~MyOtherClass();
MyClass myVar; //Unknown type Error
};
Suddenly when I include the .h and write that var Xcode gives me tons of errors... and also the unknown type error.
How can it be unknown when the .h is included right there?
Here is the NodeButton.h file which would correspond to the MyClass.h in the example
#pragma once
#include "cinder/Vector.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Color.h"
#include "cinder/ImageIo.h"
#include "cinder/Timeline.h"
#include "cinder/app/AppBasic.h"
#include "cinder/App/App.h"
#include "Node.h"
#include "CursorMano.h"
using namespace ci;
using namespace ci::app;
using namespace std;
using namespace is;
typedef boost::shared_ptr<class NodeButton> NodeButtonRef;
class NodeButton : public Node2D
{
public:
NodeButton (CursorMano *cursor, string imageUrl, bool fadeIn = false, float delay = 0.0f);
virtual ~NodeButton ();
//methods
void update( double elapsed );
void draw();
void setup();
//events
bool mouseMove( ci::app::MouseEvent event );
//vars
CursorMano *mCursor;
gl::Texture mImageTexture;
Anim<float> mAlpha = 1.0f;
bool mSelected = false;
private:
};
And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.
#pragma once
#include <list>
#include <vector>
#include "cinder/app/AppBasic.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"
#include "NodeButton.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CursorMano {
public:
CursorMano (AppBasic *app);
~CursorMano ();
void mueveMano(Vec2i);
void update();
void draw();
void play(int button);
void reset(int button);
Vec2i mMousePos;
NodeButton mButtonCaller; //this gives the unknow type error
private:
AppBasic *mApp;
gl::Texture mFrameTexture;
qtime::MovieGl mMovie;
int mIdButton;
};
You have a circular dependency of your header files.
NodeButton.h defines NodeButton class which CursorMano.h needs to include so that compiler can see definition for NodeButton but NodeButton.h itself includes CursorMano.h.
You will need to use forward declarations to break this circular dependency.
In NodeButton.h you just use an pointer to CursorMano so You do not need to include the CursorMano.h just forward declare the class after the using namespace declarations.
using namespace std;
using namespace is;
class CursorMano;
It's probably a result of the circular dependency between you two header files (NodeButton includes CursorMano and CursorMano includes NodeButton). Try removing the #include "CursorMano.h" in NodeButton.h and add class CursorMano; before your NodeButton declaration.
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.
Error C2079 'Message::simbolo' uses undefined class 'Symbol'
is generated on this line when compiling
Symbol simbolo;
This is my C++ code:
class Message
#pragma once
#include <string>
#include "Symbol.h"
#include "SharedEditor.h"
class SharedEditor;
class Symbol;
class Message
{
private:
SharedEditor* sender;
int action; //1 inserted 2 deleted
Symbol simbolo;
public:
Message();
Message(SharedEditor* sender, Symbol nuovosimbolo, int action);
~Message();
};
class Symbol
#pragma once
#include "SharedEditor.h"
#include <vector>
class SharedEditor;
class Message;
class Symbol
{
char character;
int siteID;
SharedEditor* generator;
std::vector<int> position;
public:
Symbol();
Symbol(char ch, SharedEditor* source, int ID, std::vector<int> pos);
~Symbol();
};
class SharedEditor:
#pragma once
#include "NetworkServer.h"
#include "Message.h"
#include "Symbol.h"
class Message;
class Symbol;
class SharedEditor
{
private:
NetworkServer& _server;
int _siteId;
std::vector<Symbol> _symbols;
//other functions
public:
SharedEditor(NetworkServer ns);
~SharedEditor();
void process(const Message& m);
};
class NetworkServer:
#pragma once
#include <iostream>
#include <vector>
#include <queue>
#include "SharedEditor.h"
#include "Message.h"
class SharedEditor;
class Message;
class NetworkServer
{
private:
std::vector<SharedEditor*> connected;
std::queue<Message> codaMessaggi;
public:
int connect(SharedEditor* sharedEditor);
void disconnect(SharedEditor* sharedEditor);
void send(const Message& m);
NetworkServer();
~NetworkServer();
};
You need to rework your various header files to break the dependency cycle.
The general rule is: if you only need pointers or references to a type T, you can get away with a forward declaration (class T;) instead of a full class declaration (class T { ... }, typically behind a #include).
For the example above I will go over each file and what you need:
Symbol.h needs a forward declaration of SharedEditor, as it only uses SharedEditor*.
Message.h needs a forward declaration of SharedEditor, but a full #include "Symbol.h" (compiler needs to know how big a Symbol is to calculate the size of Message)
SharedEditor needs a forward declaration of Message (passed as a reference), a full #include "NetworkServer.h" (passed as a parameter) and a full #include "Symbol.h" (used in a vector)
NetworkServer needs a forward declaration of SharedEditor (only used with pointers), but a full #include "Message.h" (used in a queue)
If you still have two classes that need full includes of each other, search stack overflow for "C++ dependency cycle" or somesuch.
I'm trying to code a game with Irrlicht, but I got stuck with this problem meanwhile I was separating the code into .h and .cpp files.
The main error in the compiler is 'node is protected within this context".
Node is an attribute of "GameObjectOverworld", and was invoked from "Player" (GameObjectOverworld child class)
This worked fine until I separated the code in .h and .cpp files.
The GameObjectOverworld.h
#ifndef __GAMEOBJECTOVERWORLD_H__
#define __GAMEOBJECTOVERWORLD_H__
#include <irrlicht.h>
#include <stdio.h>
#include "GameObject.h"
class GameObjectOverWorld : public GameObject{
protected:
scene::ISceneNode* node = nullptr;
public:
GameObjectOverWorld() {}
core::vector3df getPosition(){return node->getPosition();}
};
#endif
The player.h
#ifndef __PLAYER_H__
#define __PLAYER_H__
#include <irrlicht.h>
#include <stdio.h>
#include "GameObject.h"
#include "GameObjectOverworld.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class Player : public GameObjectOverWorld{
private:
std::string name ="";
float speed = 15.0f;
public:
Player() = default;
void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux){}
void move (char axis, int direction, float frameDeltaTime){}
};
#endif
And player.cpp (the one which sends the error)
#include "Player.h"
void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux){
Player::node = smgraux->addCubeSceneNode(10.0f, 0, 0, core::vector3df(15.0f, 0.0f, 45.0f), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
if (node)
{
node->setMaterialTexture(0, driveraux->getTexture("Materials/madero.jpg"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
}
}
You forgot to link the method addPlayerModel to the class:
Change:
void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)
To
void Player::addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)
Also change Player::node to this->node because your 'node' attribute is not static.
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 have 3 classes namely Board, Game, and AI
i want to have the object chessBoard of Board class to be use by Game and AI, and also by the Board Class. I want them to access that single chessBoard object (sharing)
problem is, it gives me the infamous fatal error LNK1169: one or more multiply defined symbols found. Also, there are Board.h,Game.h and AI.h (only declarations in them), and i also have their corresponding .cpp files. Each .h files have guards included (#ifndef _XXXXXX_H_)
I tried to include Board chessBoard inside Board.h file (just below the class), and it seems guards are not working.
Error 7 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\Board.obj CHESSv3
Error 8 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\Game.obj CHESSv3
Error 9 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\ProgramEntryPoint.obj CHESSv3
AI.h
#ifndef _AI_H_
#define _AI_H_
#include <iostream>
#include <string>
using namespace std;
struct node {
string position;
string nextPosition;
float score;
int level;
float totalscore;
node* up;
node* right;
node* left;
bool approved;
string move;
};
class AI {
private:
//string board;
//string board[8][8];
int score1;
int maxscore;
int totalscore;
public:
void GetBoard(string[][8]);
void AnalyzeMyPositions();
void ExecuteAdvanceHeuristicMove();
};
#endif
Game.h
#ifndef _GAME_H_
#define _GAME_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Game {
public:
char WhosTurn();
bool Playable();
bool GetMoveFromPlayer();
void TurnOver();
Game();
private:
char turn;
};
#endif
Board.h
#ifndef _BOARD_H_
#define _BOARD_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Board {
public:
bool SquareChecker(string);
bool MoveChecker(string);
Board();
void PrintBoard();
int Execute(string);
void UnExecute();
string CB[8][8];
private:
char turn;
vector<string> BoardRecord;
stack<string> CBR;
//string CB[8][8];
};
Board chessBoard;
#endif
If you really want to do this, you need to make the declaration of the object extern in the header Board.h:
extern Board chessBoard;
You then provide a declaration in Board.cpp:
Board chessBoard;
Much better, though would be to create it in enclosing code and pass it (by reference) to the constructors of the other classes.
What you are looking for is the Singleton design pattern which can achieved in the following way:
// Board.h
class Board {
private:
static instance_;
public:
static Board *instance();
}
// Board.cpp
Board *Board::instance_ = NULL;
Board *Board::instance() {
if (!instance_)
instance_ = new Board();
return instance_;
}
Mind that this pattern can either be seen as good or bad, if you don't like using it you should consider passing the reference of an instantiated Board class to all the requiring ones and keep it stored in each object as an instance variable. Something like:
Game::Game() {
this->board = new Board();
this->ai = new AI(board);
// or better this->ai = new AI(this) so AI can access all game methods
}
Your problem could be that there could be 3 different chessBoard definitions because you might be adding 3 different #include "Board.h". Please make only a single object at a place where you have more control rather than creating it globally inside Board.h
Did you try it like this? Include the necessary include declarations only in the .cpp files.
//Board.h
class Board {};
//Game.h
class Board;
class Game {
Board* myBoard;
public:
void setBoard(Board*);
};
//AI.h
class Board;
class AI {
Board* myBoard;
public:
void setBoard(Board*);
};
void main() {
Board chessBoard;
Game g;
g.setBoard(&chessBoard);
AI ai;
ai.setBoard(&chessBoard);
}