c++ Creating a class in seperate .cpp and .h files - c++

I am trying to declare the class "graphics" but in graphics.cpp, I get the error.
'graphics' is not a class or namespace name.
it says the location of the error is
graphics::graphics()
I am using Visual Studio 2010, and in my code, graphics is highlighted as a class.. yet it is apparently not considered a class by graphics.cpp? Does anyone know what the problem is here?
Here is my code
//graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
struct SDL_Window;
struct SDL_Renderer;
class graphics
{
public:
graphics();
~graphics();
private:
SDL_Window* _window;
SDL_Renderer* _renderer;
};
#endif
and then
//graphics.cpp
#include "graphics.h"
#include "stdafx.h"
graphics::graphics() {}
graphics::~graphics() {}

If you are using Pre-compiled headers
#include "SDL.h"
#include "graphics.h"
#include "stdafx.h" <<<<< must always be included before anything else
Change to
#include "stdafx.h"
#include "SDL.h"
#include "graphics.h"
The compiler should output this error along with your given error.

Related

Include file includes itself with guards

I have two header files, animation.h and hero.h,
here is the code for animation.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
#ifndef ANIMATION
#define ANIMATION
//Class
#endif
And for hero.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
#ifndef HERO
#define HERO
//Class
#endif
I get the error message #include file "" includes itself even when using include guards.
I am new to using include guards and such so this may be a very trivial mistake.
Thanks for any help
You should put the header guards before anything else.
On gcc and MSCyou can also use
#pragma once
instead. Probably also on other more modern compilers. Simpliy put this at the top of your include, instead of the #ifdef....
animation.h
#ifndef ANIMATION
#define ANIMATION
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
//Class
#endif
hero.h
#ifndef HERO
#define HERO
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
//Class
#endif
Or animation.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
//Class

C++ multiple definition errors in 3-way header include

I have 3 header files defining objects:
Point3d.h
#ifndef POINT3D_H
#define POINT3D_H
class Ray3d;
class Vector3d;
#include "Ray3d.h"
#include "Vector3d.h"
class Point3d {
...
};
#endif
Vector3d.h
#ifndef VECTOR3D_H
#define VECTOR3D_H
class Point3d;
class Ray3d;
#include "Ray3d.h"
#include "Point3d.h"
class Vector3d {
...
};
#endif
and Ray3d.h
#ifndef RAY3D_H
#define RAY3D_H
class Point3d;
class Vector3d;
#include "Point3d.h"
#include "Vector3d.h"
class Ray3d {
...
};
#endif
I won't include the .cpp files, but all the functions are defined there.
And then I have this class:
Transform.h
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <Eigen/Dense>
#include "../../geometry/Ray3d.cpp"
#include "../../geometry/Point3d.cpp"
#include "../../geometry/Vector3d.cpp"
using Eigen::MatrixXd;
class Transform {
...
};
#endif
AND FINALLY I have this subclass:
Translation.h
#ifndef TRANSLATION_H
#define TRANSLATION_H
//#include <Eigen/Dense>
#include "Transform.h"
//#include "../../geometry/Point3d.cpp"
//#include "../../geometry/Vector3d.cpp"
//#include "../../geometry/Ray3d.cpp"
using Eigen::MatrixXd;
class Translation : public Transform {
...
};
#endif
The problem is when I try to compile Translation.cpp:
g++ Transform.cpp Translation.cpp
I get a multiple definition of function error for every method in Ray3d, Point3d, and Vector3d. What can I do do avoid this? Should I be including less? Is my g++ command wrong? Thanks!
I'm also aware that I'm doing both forward declaration and includes in the first 3 headers, but that was the only way I could get those to compile. Part of the problem maybe?
You should not include the cpp files in transform.h
"but that was the only way I could get those to compile. Part of the problem maybe?"
You compile and link .cpp files separately, instead of including them (i.e. being seen from the preprocessor).
Your compiler command line should look rather like
g++ ../../geometry/Ray3d.cpp
../../geometry/Point3d.cpp
../../geometry/Vector3d.cpp
Transform.cpp Translation.cpp
-o MyExecutable

SDL and xcode5 - SDL in different classes

I got following problem:
I got my main c++ file, in which i have included
#include <SDL2/SDL.h>
After having written some SDL c++ code, then i want to split up my program into different classes.
the problem is that i try to say:
#include <SDL2/SDL.h>
in the new "Engine" class but i doesn't seem like it is including the SDL.
Im using xcode 5.
The SDL frameworks works fine if i write the code in my main.cpp
Engine class:
#include "Engine.h"
#include <SDL2/SDL.h>
using namespace std;
class Engine
{
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
public:
Engine();
bool init();
bool loadMedia();
void close();
}
Im still on the drawing board with what kind of classes i need.
This is Engine.h
#ifndef __Engine4__Engine__
#define __Engine4__Engine__
#include <iostream>
#include <SDL2/SDL.h>
class Engine
{
}
#endif /* defined(__Engine4__Engine__) */
My xcode5 wont come with suggestions when i write SDL_
The issue is that you have not created Engine.h and are trying to reference it when it doesnt exist. The issue is not with SDL, rather that you are unsure how to create a class in c++.
You need to create both Engine.h and Engine.cpp
Engine.h will look something like
#ifndef ENGINE_H
#define ENGINE_H
#include <SDL2/SDL.h>
class Engine
{
public:
Engine();
bool init();
bool loadMedia();
void close();
private:
SDL_Window *window;
SDL_Surface *screenSurface;
};
#endif
and then you need to create a Engine.cpp file that will look like
#include "Engine.h"
Engine::Engine() :
window(nullptr),
screenSurface(nullptr)
{
}
// Rest of code from header file
See more about creating classes in C++ here.

Undeclared Identifier vector of pointers to objects

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".

Incomplete type C++

I get the following error when I try to execute this code segment : "Menu does not name a type".I know its something to do with the circular references, but for the life of me I can't figure out what. Also, menu, go, and manager are repeatedly giving errors. The code segments are posted below :
#ifndef GO__H
#define GO__H
#include <SDL.h>
#include <iostream>
#include <string>
using std::cout; using std::endl;
using std::string;
#include "ioManager.h"
#include "gui.h"
#include "clock.h"
#include "menu.h"
//class Menu;
class Go {
public:
Go ();
void play();
private:
SDL_Surface *screen;
Gui gui;
Menu menu;
void drawBackground() const;
Go(const Go&);
Go& operator=(const Go&);
};
#endif
Here's Menu :
#ifndef MENU_H
#define MENU_H
#include <SDL.h>
#include <iostream>
#include "ioManager.h"
#include "gui.h"
#include "clock.h"
#include "manager.h"
class Menu {
public:
Menu ();
void play();
private:
const Clock& clock;
bool env;
SDL_Surface *screen;
Gui gui;
Manager mng;
void drawBackground() const;
Menu(const Menu&);
Menu& operator=(const Menu&);
};
#endif
Manager :
#ifndef MANAG_H
#define MANAG_H
#include "go.h"
class Manager {
Go go;
//other code
}
Can you see where the problem is? Error message:
In file included from go.h:13:0,
from manager.h:33,
from manager.cpp:2:
menu.h:28:11: error: field ‘mng’ has incomplete type
manager.h includes go.h which includes menu.h which includes manager.h ...
The class Menu is being defined before it ever gets to the definition of class Manager.
However, class Menu needs a Manager but since the compiler doesn't know about Manager yet it doesn't know how big to make it.
You could forward declare class Manager and make the mng member of Menu a pointer or reference:
class Manager;
class Menu {
...
Manager* mng;
// or this:
//Manager& mng;
...
Here's a good explanation of circular references and how to fix them.
It appears you are missing the semicolon at the end of the declaration of your Manager class in manger.h.
You are also missing the #endif to close your include guard.