C++/SDL problem of double inclusion - c++

I'm getting this error from compilator:
1>Linking...
1>main.obj : error LNK2005: "int g_win_flags" (?g_win_flags##3HA) already defined in init.obj
1>main.obj : error LNK2005: "struct SDL_Surface * g_screen" (?g_screen##3PAUSDL_Surface##A) already defined in init.obj
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>.\Debug\Heroes are back!.exe : fatal error LNK1169: one or more multiply defined symbols found
It looks like that g_win_flags and g_screen are twice included, but I don't understand why.
Here is the source:
main.cpp
#include <iostream>
#include "dec.h"
#include "init.h"
int main(int argc, char *argv[]){
init();
return 0;
}
dec.h
#ifndef DEC_H
#define DEC_H
#include <SDL.h>
#include <iostream>
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
using namespace std;
int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
SDL_Surface *g_screen = NULL;
#endif
init.h
#ifndef INIT_H
#define INIT_H
bool init();
#endif
init.cpp
#include "dec.h"
bool init(){
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1){
cerr << "Unable to initialize SDL" << endl;
return false;
}
g_screen = SDL_SetVideoMode(640, 480, 0, g_win_flags);
return true;
}
Can someone help? Thanks in advance and have a nice day :)

You define and initialize the variables in the header.
You should just declare them in the header (dec.h) without any initializers:
extern int g_win_flags;
extern SDL_Surface *g_screen;
Then define them once in one file - presumably dec.cpp - with the initializations.
As it was, you were defining them in every source file that included 'dec.h' and then running foul of the ODR - One Definition Rule.

in dec.h you want
extern int g_win_flags;
extern SDL_Surface *g_screen;
and then to define and initalise them in just dec.cpp
Update:
#include "dec.h"
int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
SDL_Surface *g_screen = NULL;
The general rule of thumb is "nothing in a header file should take up any space in the output of the compiler". (There are exceptions to this obviously)
In practice this means extern variable declarations are fine, as are function declarations, but not definitions.

You have included files into two different source files (init.cpp and main.cpp) that define instantiated variables.
You need a way to be sure they are 'externed' in all but one source file.

Well, I tried to do what you've told me guys, but compilator is complaining:
1>.\heroes are back!\dec.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\heroes are back!\dec.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\heroes are back!\dec.cpp(4) : error C2040: 'g_screen' : 'int' differs in levels of indirection from 'SDL_Surface *'
Here is the dec.h
#ifndef DEC_H
#define DEC_H
#include <SDL.h>
#include <iostream>
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
using namespace std;
extern int g_win_flags;
extern SDL_Surface *g_screen;
#endif
dec.cpp
#include "dec.h"
g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
g_screen = NULL;
What is wrong in this code? Sorry for asking dumb questions, but I'm only learning C++ :)

Related

C++ - Static function using vector<derivedclass> don't find declared identifier

while working on a personal project on a new language, I 've come across a annoying compiling time error where a static member of my constants.h (constants class) doesn't find the class name (Quark which is a derived class of Particle, those two in the same file "Particle.h") used in parameter...
Here is the compilation error output:
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(41): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(41): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C3861: 'Quark': identifier not found
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2514: 'std::vector' : class has no constructors
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(46): error C2663: 'std::vector<_Ty,_Alloc>::vector' : 8 overloads have no legal conversion for 'this' pointer
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C3861: 'Quark': identifier not found
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2514: 'std::vector' : class has no constructors
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(51): error C2663: 'std::vector<_Ty,_Alloc>::vector' : 8 overloads have no legal conversion for 'this' pointer
Here the crashing code:
constants.h
#pragma once
#include "stdafx.h"
#include <vector>
#include "Particle.h"
class constants
{
public:
static enum nucl
{
proton = 0
,neutron = 1
};
static std::vector<Quark> getNuclVal(nucl type)
{
if (type == nucl::proton)
{
std::vector<Quark> result({ Quark(std::string("up")), Quark(std::string("up")), Quark(std::string("down")) });
return result;
}
else
{
std::vector<Quark> result({ Quark(std::string("up")), Quark(std::string("down")), Quark(std::string("down")) });
return result;
}
};
// More functions and constants
};
Particle.h
#pragma once
#include <vector>
#include "Entity.h"
#include "constants.h"
class Particle : public Entity
{
private:
// some stuff
public:
// some stuff
};
class Quark : public Particle
{
public:
Quark(std::string &const);
};
and there is my Quark class definition in Particle.cpp
#include "stdafx.h"
#include "Particle.h"
#include "constants.h"
#include <string>
#include "Entity.h"
Particle::Particle(std::string &const name, Size size)
: Entity::Entity()
, m_charge(0) //en e
, m_mass(0) //en electron Volt (eV)/c²
, m_Size(size)
, m_name(name)
{
};
Quark::Quark(std::string &const name) // declaration stuff
: Particle(name, Size::Sub_Atomic)
{
constants::quark type = constants::getQuarkByName(m_name);
setMass(constants::getMass(type));
setCharge(constants::getCharge(type));
}
// various stuffs
There is my precompiled header which include every sources I need in my program (to let you see compilation order) stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>
// TODO: reference additional headers your program requires here
#include "Math.h"
#include "SciNumber.h"
#include "PhyEngine.h"
#include "Options.h"
#include "ConfigFile.h"
#include "Chameleon.h"
#include "Entity.h"
#include "Particle.h"
#include "constants.h"
I wonder if I need to separate my derived classes of Particle to specific files or not (It would add a lot of files..... T-T), what do you think about it?
here is my class diagram (open with Visual studio) to help you visualize the whole project: download it here via my dropbox
You have a circular dependency in your headers. Particle.h includes constants.h and constants.h includes Particle.h. You'll need break the loop.
You get the error because when Particle.h is included, it'll at first include constants.h. Then constants.h will try to include Particle.h. #pragma once will prevent infinite recursion, but also means that rest of the Particle.h is not yet included before the contents of the constants.h. Thus the compiler complains about missing declarations.
You should be able to break the loop by breaking your headers and possibly classes into smaller ones*. The static member functions of constants are not strictly related to each other, are they? In fact, from what I've seen, it should probably be a namespace rather than a class**. Stuff in namespace can be conviniently declared in separate headers while a class must be defined entirely in one.
*Assuming you actually need to. If Particle.h doesn't depend on constants.h, then simply remove the include.
**I may be wrong about that since you didn't show the entire class.
Break off the part of constants that depends on Quark (and anything that depends on anything that depends on Quark, recursively) into another class in another header and don't include that one inside Particle.h. Or break off parts of Particle.h that depend on constants.h and don't include that one in constants.h.
stdafx.h also includes constants.h which in turn includes stdafx.h. You'll need to fix all circular includes.
Try moving
#include "constants.h"
to the bottom of Particle.h. What currently happens is that Particle.h goes through constants.h before it has declared the types.
constants.h is not re-including particle.h because of pragma #once (without which you would have circular dependencies and nothing would work anyway).
You also don't need the #include "constants.h" in stdafx.h as it is already included in Particle.h
I hope this helps!

C++ MS Visual Studio Exp 2013 IntelliSense: this declaration has no storage class or type specifier

I created two separate projects in MS Visual Studio, W59Components.cpp(console) and Frequency.cpp(DLL). I created a class ReadFile in W59Components>ReadFile.h and am trying to use that class in a different MS project Frequency.cpp. I did look at other forums on the topic with no luck.
For some reason I am getting an IntelliSense error, "IntelliSense: this declaration has no storage class or type specifier."
Compiler Errors:
error C2146: syntax error : missing ';' before identifier 't'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'ReadFile' : redefinition; previous definition was 'function'
see declaration of 'ReadFile'
Here is a snippet of the code:
#include "stdafx.h"
#include <windows.h>
#include <string>
#include "../../scripts/DLLs/Wave59_SDK.h"
using namespace std;
#include "ReadFile.h"
ReadFile t; // Should Reconize "ReadFile" as a class
//#include "../../scripts/DLLs/demo/Demo.h"
// ---------------------------------------------------------------------------
extern "C" double __declspec(dllexport) average(WAVE59_DATASTRUCT *price_ptr,
int currentptr, int *int_args, int num_int_args, double *double_args,
int num_double_args, char **string_args, int num_string_args);
How do you #include a header file from a different MS Visual Studio project?
Here is the header file, I didn't want to burden you with the function definition:
/* ReadFile.h
* Reads a incomming file and splits it into a multidemensional array */
# include <istream>
# include <fstream>
#include<string>
using namespace std;
class ReadFile {
public:
ReadFile(){};
~ReadFile(){};
void ReadTheFile(string path = "C:\\Users\\Owner\\Documents\\Wave59\\TestWriteFile.txt");
void split(string delimiter, string text, string pArray[]);
int getColumnNum(){ return this->columnNum; }
int WriteFile(string message[], int lenMessage, string path = "C:\\Users\\Owner\\Documents\\Wave59\\W59Components-WrittenFile.txt");
double f2Array[20][1000];
private:
void readFile(double pArray[], string path = "C:\\Users\\Owner\\Documents\\Wave59\\TestWriteFile.txt");
void sortArray(double returnArray[], int modNum);
// Initalize VARS
const int SIZE = 10000;
double fArray[10000]; // file data
int columnNum; // Number of colums in the file data, which is different arrays of data
};
Your problem is the winapi defines ReadFile. http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
By including windows.h you are getting a conflict between your class and the winapi ReadFile.

Ffmpeg headers do not compile in windows

I am trying to link to ffmpeg under windows, but have run into difficulties. Inclusion of ffmpeg headers causes hundreds of compilation errors which i can't easily fix.
1) timestamp.h uses snprintf instead of _snprintf. I have tried to add it as a macro, like this:
#ifdef Q_OS_WIN
#define snprintf _snprintf
#endif
#define __STDC_CONSTANT_MACROS
namespace ffmpeg {
extern "C" {
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
} }
but it didn't help. Why would the macro not propagate inside?
2) There again, PRId64 isn't defined. I have defined __STDC_CONSTANT_MACROS before inclusion of timestamp.h, but definition isn't retrieved from inttypes.h
In compiler output, it looks like:
ffmpeg\include\libavutil/timestamp.h(42) : error C3861: 'snprintf': identifier not found
ffmpeg\include\libavutil/timestamp.h(42) : error C2146: syntax error : missing ')' before
identifier 'PRId64'
You did include them into a extern "C" right?
extern "C" {
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
Is this a typo? You talk about "snprintf" ("n", not "m").
#define smprintf _snprintf
Even then, timestamp.h probably redifines it or includes something that does.

Vs2012 compiler error C2143 : missing ; before *

With my luck it's probably something very obvious that's slipped past me, but I've been struggling with C2143 for quite a while and I'm stumped.
game.h(21): error C2143: syntax error : missing ';' before '*'
game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Game.h
#ifndef GAME_H_
#define GAME_H_
#include <irrlicht.h>
using namespace irr;
using namespace irr::core;
using namespace irr::scene;
using namespace irr::video;
using namespace irr::io;
using namespace irr::gui;
#include <iostream>
#include "CInput.h"
#include "CAssets.h"
using namespace rtsirr;
IrrlichtDevice *device = 0;
IVideoDriver *driver = 0;
ISceneManager *manager = 0;
CAssets *assets = 0; // Line 21, error here
#endif
CAssets.h
#ifndef ASSETS_H_
#define ASSETS_H_
#include "Game.h"
namespace rtsirr {
class CAssets
{
public:
CAssets();
virtual ~CAssets();
ITexture* getTexture(stringw name);
IMesh* getMesh(stringw name);
IAnimatedMesh* getAnimatedMesh(stringw name);
void load();
private:
map<stringw, ITexture *> *textures;
map<stringw, IMesh *> *meshes;
map<stringw, IAnimatedMesh *> *animatedMeshes;
};
}
#endif
It seems that CAssets isn't being recognized as a valid type, but I can't figure out why. What's causing the issue?
Thanks.
You have a cyclic dependency in your includes. Game.h includes CAssets.h which in turn includes Game.h before even getting to define CAssets. The result from the preprocessor would be different, depending on the order of includes.
From your sample code, it seems that Game.h doesn't really need to know much about CAssets other than that is a type. You could replace the inclusion of CAssets.h with a forward declaration:
class CAssets;
You can even provide a CAssets_fwd.h that does only that. Otherwise, you will need to break the cyclic dependency between those two headers.

C++ Error C2447? What am I missing

I don't know if I'm going crazy, or just that everything I've read on this error doesn't apply to my situation. But I'm getting these errors when I compile my project:
1>f:\program files\testengine\testengine\testengine\game.cpp(10) : error C2061: syntax error : identifier '{ctor}'
1>f:\program files\testengine\testengine\testengine\game.cpp(11) : error C2143: syntax error : missing ';' before '{'
1>f:\program files\testengine\testengine\testengine\game.cpp(11) : error C2447: '{' : missing function header (old-style formal list?)
1>f:\program files\testengine\testengine\testengine\game.cpp(15) : error C2059: syntax error : 'public'
1>f:\program files\testengine\testengine\testengine\game.cpp(16) : error C2143: syntax error : missing ';' before '{'
1>f:\program files\testengine\testengine\testengine\game.cpp(16) : error C2447: '{' : missing function header (old-style formal list?)
So, I Google'd the error, and everyone said this is caused by things like extra and/or missing semicolons and brackets. But I've looked over my code a lot (there's not very much!) and I don't see any of that, unless of course, as I previously suggested, I'm going crazy...
Game.h
#ifndef _SBE_CGAME_
#define _SBE_CGAME_
class CGame
{
public:
CGame();
~CGame();
void DoLoop();
};
#endif //_SBE_CGAME_
Game.cpp
#include "base.h"
extern CGame* m_gGame;
CGame::CGame()
{
//
}
~CGame::CGame()
{
//
}
public void CGame::DoLoop()
{
SwapBuffers(hDC);
}
Base.h
#include <windows.h> // Header File For Windows ==NEEDS TO COME BEFORE GL.H AND GLU.H==
#include <gl\gl.h>
#include <gl\glu.h>
#include "Properties.h"
#include "Game.h"
#include "Renderer.h"
#ifndef _SBE_BASE_
#define _SBE_BASE_
extern CGame* m_gGame;
#endif //_SBE_BASE_
Globals.cpp
#include "base.h"
//=================================================================================
// Here is where we define all the global variables
//=================================================================================
CGame* m_gGame = new CGame();
What am I overlooking? I will admit, its been a while since I've programmed C++, but I reread class definition articles and all sorts of things. I have this not-so-strange feeling that its going to be something very silly, that I should have seen.
In Game.cpp:
~CGame::CGame()
should be
CGame::~CGame()
And drop the public keyword on the definition of CGame::DoLoop.
You need to write CGame::~CGame() instead of ~CGame::CGame() for the destructor. It's always the class name first (CGame) and only then the member name (~CGame).
#include <windows.h> // Header File For Windows ==NEEDS TO COME BEFORE GL.H AND GLU.H==
#include <gl\gl.h>
#include <gl\glu.h>
#include "Properties.h"
#include "Game.h"
#include "Renderer.h"
#ifndef _SBE_BASE_
#define _SBE_BASE_
extern CGame* m_gGame;
#endif //_SBE_BASE_
Why are you only include-guarding part of this file?
#ifndef _SBE_BASE_
#define _SBE_BASE_
#include <windows.h> // Header File For Windows ==NEEDS TO COME BEFORE GL.H AND GLU.H==
#include <gl\gl.h>
#include <gl\glu.h>
#include "Properties.h"
#include "Game.h"
#include "Renderer.h"
extern CGame* m_gGame;
#endif //_SBE_BASE_
Anyway, my guess is something weird in Properties.h or Renderer.h
you have mis-declared your destructor
it should be
CGame::~CGame()