c++ class definition in separate file not found - c++

I am trying to make a game in c++ using SDL2.0 and visual studio 2010 express edition. I want to make a class to hold all info about a level, like map structure, starting position, Finish position, power-up locations. This class is used in multiple source files, so I created a new .cpp file in which I would store the class and a .h file that declares the class. The problem is that it doesn't compile. I get the following errors:
1>------ Build started: Project: Game, Configuration: Debug Win32 ------
1> gameMain.cpp
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gamemain.cpp(33): error C2079: 'GameMap' uses undefined class 'level'
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gamemain.cpp(36): error C2664: 'gameInit' : cannot convert parameter 3 from 'int *' to 'level *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> gameLevelObject.cpp
1> gameInit.cpp
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gameinit.cpp(27): error C2027: use of undefined type 'level'
1> c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gamelevelobject.h(3) : see declaration of 'level'
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gameinit.cpp(27): error C2440: '<function-style-cast>' : cannot convert from 'short' to 'level'
1> Source or target has incomplete type
1> Generating Code...
The actual game is created as a DLL, so I can add things like a menu, or a video to the body.
These are the sources that cause the error:
gameMain.cpp:
// STD
#include <string>
#include <IOstream>
#include <map>
// GAME
#include "Global_Includes.h" //Include all SDL libs
#include "Global_Mapping.h" //Include object that tells program what to do when this part is finished
#include "Global_Events.h" //Include object to control events
#include "Global_Screen.h" //Include object to control screen
// LOCAL
#include "gameLevelObject.h" //Include level class
#include "gameInit.h" //Include function for initialisation
// DLL EXPORT
#include "gameMain.h"
mapping game(screen* Display, short int Lvl){
mapping ReturnValue;
level GameMap; //Error: Incomplete type
gameInit(Lvl, Display, &GameMap);
}
gameInit.cpp:
// STD
#include <map>
// GAME
#include "Global_Includes.h" //Include all SDL libs
#include "Global_Screen.h" //Include object to control screen
#include "gameLevelObject.h" //Include level class
#include "gameInit.h" //Include header so compiler knows where gameInit is defined
bool gameInit(short int lvl, screen* Display, level* object){
*object = level::level(lvl); //Error: Incomplete type
return true;
}
gameLevelObject.cpp:
// GAME
#include "Global_Includes.h" //Include all SDL libs
// STD
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
// LOCAL
#include "gameLevelObject.h" //Include header so compiler knows where class level is defined
class level{
SDL_Surface* total_map;
int lives;
int livesMINUS;
int livesPLUS;
int position[2];
std::vector<std::string> map;
std::vector<std::pair<int, int> > energyTILES;
public:
level(short int lvl){\*constructor definition*\}
};
gameLevelObject.h:
#include <vector>
class level;
I read in the book "The programming language c++", written by Bjarne Stroustrup, that this is how you should include files.
The question is: why doesn't the compiler know that class level is defined in gameLevelObject.cpp? and why is the function gameInit found by the compiler and class level not? I used the same method to link them to gameMain.cpp

Related

xmemory0 Error When Trying to Build Project

I'm developing some additional options for a game (which means I don't have knowledge about whole game's methods, classes etc.).
Anyway, I have been trying to build the project for codes I've developed to test them. I've encountered some DirectX, Windows SDK Version, Compiler problems but managed to solve them.
I've downloaded source codes through SVN, so whole project is actually working. I just couldn't manage to build it in my local laptop or desktop computer.
This time I'm getting xmemory0 error:
Code: C2280
Description: 'std::pair::pair(const std::pair &)':
attempting to reference a deleted function File: xmemory0 Line:840
Code part of the error is:
// xmemory0 internal header (from <memory>)
#pragma once
#ifndef _XMEMORY0_
#define _XMEMORY0_
#ifndef RC_INVOKED
#include <cstdint> /* for uintptr_t */
#include <cstdlib>
#include <limits>
#include <new>
#include <xutility>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,_STL_WARNING_LEVEL)
#pragma warning(disable: _STL_DISABLED_WARNINGS)
#pragma push_macro("new")
#undef new
... // other code lines
template<class _Objty,
class... _Types>
void construct(_Objty *_Ptr, _Types&&... _Args)
{ // construct _Objty(_Types...) at _Ptr
::new ((void *)_Ptr) _Objty(_STD forward<_Types>(_Args)...); // HERE IS GETTING ERROR
}
... // other code lines

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

i have a problem with following code:
Generator.h:
#pragma once
class Generator
{
public:
friend class BagObject;
Generator(void);
~Generator(void);
...
void generator(int);
private:
BagObject *object;
vector<BagObject> data; //Error c4430
};
and this is a error:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
there is 6 more errors but i believe that they should disappeared after solving this problem.
this is the cpp file. I couldn't paste it on the first time.
Generator.cpp
#include "stdafx.h"
#include "Generator.h"
#include "BagObject.h"
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
Generator::Generator(void)
{
srand(time(NULL));
}
Generator::~Generator(void)
{
data.clear();
}
void Generator::generator(int ld)
{
for (int i = 0; i<ld; i++)
{
object = new BagObject(rand(),rand(),i);
data.push_back(object);
}
}
int main()
{
Generator *g = new Generator;
g->generator(10);
return 0;
}
Either you forgot to include header
#include <vector>
or forgot to write directive
using namespace std;
In any case it would be better to write
#include <vector>
//...
std::vector<BagObject> data;
^^^^^
You have to include the header <vector> in all headers where there is a reference to std::vector.
Other answers are correct, but cryptic. In plain English, your header does not know about BagObject class. You included BagObject.h in the .cpp, but you should have included it in the .h.
It also does not know about vector for the same reason.
I am guessing, you were under impression that .cpp had to use #include, but .h did not. This is a common misunderstanding of beginners in C++. Headers need to include all referenced class declarations, hence you need to elevate your includes from .cpp into your .h.
Move two mentioned includes into the header and it will work.
vector may not be instantiated with an incomplete type. In order to have vector<BagObject> data; in the header, the header must also have #include "BagObject.h".
(This is in addition to the changes recommended in Vlad's answer)
I know your problem is solved but in My case the same error was caused due to cyclic includes (i.e. I had accidentally included the .h file in one of the .h file included in it)
TextureManager.h (The file with the error)
// This is TextureManager.h
#pragma once
#include "Game.h"
#include "GameObject.h"
/*
texture manager class
*/
GameObject.h
// This is GameObject.h
#pragma once
#include "game.h"
#include "TexutureManager.h" // Accidental
/*
*/
I thought it may be worth noting this is also one of the ways to get this error.

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++ compilation error when adding windows.h

I need to write small tool in C++
I've never used C++ as programming language before (I have couple years of Java dev experience) and .NET
I've started a new project in VS , when I am adding in my Header file of my class
#include <windows.h> I am getting the following error:
Error 1 error C2143: syntax error : missing ';' before '*' c:\program files\microsoft sdks\windows\v7.0a\include\servprov.h 96 1 CppLog
For now my class even doesn't have any real functions and looks like
in header
class TheTool
{
public :
void Foo();
};
in cpp
void TheTool::Foo(){};
and the project doesn't get compiled.
plz any suggestions ? Maybe a compiler doen't set up good ?
this is how the Header file looks like
#pragma once
#include "stdafx.h"
#include <stdio.h>
//#include <Windows.h>
//#include <winuser.h>
//#include <windowsx.h>
//#include <time.h>
class TheTool
{
public :
void Foo();
};
When I am uncommenting the include I am starting to get this compilation error.
BTW , how can i know the compiller setting ?

error C2504: 'ios' : base class undefined

I am trying my hands on sample codes from a book, and so I am not entirely sure what I may have wrong in the header file I have so far.
I keep getting the following error messages.
Error 2 error C2061: syntax error : identifier 'streambuf'
Error 1 error C2504: 'ios' : base class undefined
Error 5 IntelliSense: identifier "streambuf" is undefined
// StdAfx.h HEADER FILE
**************************
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include <iostream>
#include <strstream>
#include <iomanip>
#include <ios>
#include <stdio.h>
#include <tchar.h>
#include "targetver.h"
// Conios HEADER FILE
**************************
#include "Stdafx.h"
class Conios :virtual public ios{
protected:
public:
Conios(void);
~Conios(void);
Conios(streambuf* Buffer);
};
ios is in the std-namespace. So either use use namespace std; or extend from std::ios instead of just ios.
If you are using use namespace use it only in your implementation-files like *.cpp or *.cxx, do not write use namespace ... your header files - ever!.
And the same goes for streambuf.