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.
Related
Hi I am currently working on a tournament creator program using QT Creator. I have been working on it at work during my lunch breaks and it has ran fine, but I have brought it home to get it finished for this weekend and it doesnt work.
The error which I am getting is
F:\Documents\Coding\TournamentOrganiser\startscreen.cpp:-1: error: multiple
definition of `StartScreen::StartScreen(QWidget*)'
I have literally only taken the folder placed it on a USB, copied it onto my desktop and ran it and this occurs, when it worked fine at home.
I assume that it is some kind of QT Creator configuration setting but just in case these are the files which are involved in the issue. If anyone can help me out with this it would be much appreciated as I need this working on my desktop for Saturday when I will need to use the app.
template <typename T> using shp = std::shared_ptr<T>;
globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#include "startscreen.h"
#include "tournamentcreator.h"
#include "player.h"
#include <memory>
#include "util.h"
#include "matchups.h"
namespace globals{
extern shp<StartScreen> g_StartScreen;
extern shp<TournamentCreator> g_TournamentCreator;
extern shp<std::vector<Player>> g_PlayerData;
extern shp<MatchUps> g_MatchUps;
}
#endif // GLOBALS_H
main.cpp
#include "startscreen.h"
#include "tournamentcreator.h"
#include "matchups.h"
#include <QApplication>
#include "util.h"
namespace globals{
shp<StartScreen> g_StartScreen;
shp<TournamentCreator> g_TournamentCreator;
shp<std::vector<Player>> g_PlayerData;
shp<MatchUps> g_MatchUps;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
globals::g_StartScreen = std::make_shared<StartScreen>();
globals::g_TournamentCreator = std::make_shared<TournamentCreator>();
globals::g_PlayerData = std::make_shared<std::vector<Player>>();
globals::g_MatchUps = std::make_shared<MatchUps>();
globals::g_StartScreen->show();
return a.exec();
}
startscreen.h
#ifndef STARTSCREEN_H
#define STARTSCREEN_H
#include <QWidget>
namespace Ui {
class StartScreen;
}
class StartScreen : public QWidget
{
Q_OBJECT
public:
explicit StartScreen(QWidget *parent = 0);
~StartScreen();
private slots:
void on_newEventButton_clicked();
private:
Ui::StartScreen *ui;
};
#endif // STARTSCREEN_H
startscreen.cpp
#include "startscreen.h"
#include "ui_startscreen.h"
#include "globals.h"
StartScreen::StartScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::StartScreen)
{
ui->setupUi(this);
}
StartScreen::~StartScreen()
{
delete ui;
}
void StartScreen::on_newEventButton_clicked(){
this->hide();
globals::g_TournamentCreator->show();
}
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.
When I compile, i get the error: 'Input' does not name a type on line 17
#ifndef GAME_H
#define GAME_H
#include "input.h"
// Can forward declare "class Input(GLFWwindow*);" here but it still
// gives the same error.
class Game
{
public:
Game(Input* input);
~Game();
void Input();
void Update();
void Render();
private:
Input* mpInput; //error here.
};
#endif // GAME_H
Input.h looks like this.
#ifndef INPUT_H
#define INPUT_H
#include <GLFW/glfw3.h>
#include <vector>
class Input
{
public:
Input(GLFWwindow* window);
~Input();
bool GetKey(int keyCode);
bool GetKeyDown(int keyCode);
bool GetKeyUp(int keyCode);
void Update();
private:
GLFWwindow* mpWindow;
std::vector<bool> mCurrentKeys;
std::vector<bool> mDownKeys;
std::vector<bool> mUpKeys;
const int NUM_KEYCODES = 256;
};
#endif // INPUT_H
I have no clue what is going on here. I had a similar problem yesterday, and couldn't figure it out, so I tried saving the project, closing Code::Blocks, restarting Code::Blocks, and reopening the project. Then I compiled it and it worked for no apparent reason. No code was changed or anything. I tried reloading the project here too, but it still gives the same error.
You have method Input in your class code. Mixing names of methods and types is generally a bad idea. Consider this code in one of your class Game methods:
Input();
Is this a method Input call or you are trying to create Input class instance? Try renaming your Input method.
what am I doing wrong?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When i try to compile and run the program, it says:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
Here's the strange thing, if I change 'FileOperations FileController;' to 'FileOperations *FileController;'(Obviously this compiles wrongly, because the rest of my codes that you can't see havn't been adapted to '->' instead of '.')
Then if I change it back to 'FileOperations FileController;' it lets me compile the program once (And it works fine), then it has the error the next time I try to compile it.
I'm using Qt 5.0.
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
I assume that, in your .cpp file, you are using
#include "fileoperations.h"
Then, in fileoperations.h, you are including mainwindow.h which again includes fileoperations.h which is basically correct, since you are using a FileOperations object as parameter. But, due to the guards, class FileOperations is not seen by the compiler this time, hence FileOperations is unknown when used as parameter in your method. You need to break this dependency:
In fileoperations.h, use a forward declaration for Ui::MainWindow and remove the #include "mainwindow.h":
namespace Ui {
class MainWindow;
}
...
Since you are holding a FileOperations object in your class, you need the full class declaration. This means you have to include the header, you cannot simply forward declare the class like you are doing now. If you hold only a pointer, and do not have any code in your header that attempts to dereference the pointer, then the forward declaration is enough.
EDIT You have a cyclical include. You are including mainwindow.h in fileoperations.h. You can fix if by removing that include completely.
You have circular include issue, mainwindow.h and fileoperations.h include each other, try to remove below line from fileoperations.h
#include "mainwindow.h"
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.