I am new to C++ and I am trying to make a little dungeon crawler game. Currently I have multiple vectors declared in my header files but they seem to give multiple errors. I have tried searching for this problem on StackOverflow but the answers don't really seem to work.
Here is one of my header files: (Hero.h)
#pragma once
class Hero {
public:
Hero();
std::string name;
int experience;
int neededExperience;
int health;
int strength;
int level;
int speed;
std::vector<Item> items = std::vector<Item>();
void levelUp();
private:
};
Here is my .cpp file: (Hero.cpp)
#include "stdafx.h"
#include <vector>
#include "Hero.h"
#include "Item.h"
Hero::Hero() {
}
void Hero::levelUp()
{
};
Like I said I am new to C++ so there might be a lot more wrong with my code than I know. This is just a test.
Below are the errors that are shown in the Error list of Visual Studio 2015:
Error C2039 'vector': is not a member of 'std' CPPAssessment hero.h 13
Error C2143 syntax error: missing ';' before '<' CPPAssessment hero.h 13
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int CPPAssessment hero.h 13
Error C2238 unexpected token(s) preceding ';' hero.h 13
Include <vector> in your Hero.h header and consider removing it from your Hero.cpp file as mentioned in the comments below.
std::vector<Item> items = std::vector<Item>(); declares a complete type.
Therefore the compiler needs to know the declaration of std::vector at that point (amongst other things, it's required to establish the compile-time evaluable constant sizeof Hero). The solution is to #include <vector> in the header hero.h, not the source file.
//.h
template<class _Ty>
class std::allocator;
template<class _Ty, class _Alloc = std::allocator<_Ty> >
class std::vector;
//this line in .cpp file. Put it before custom header files.
#include<vector>
Tested in vs 2017.
In this way, no header file included in your custom header file.
But not sure why the save way for stack does not work.
For further information, https://github.com/YagaoDirac/snippet-set-for-cpp. And a discord link inside.
Related
I have a header file that's linked to some others in a project of mine. Here is all the code in the file that's giving me the error:
#ifndef RELATION_H_
#define RELATION_H_
#include <set>
#include <string>
#include "Scheme.h"
#include "Tuple.h"
class Relation
{
public:
void select();
void project();
void rename();
private:
std:string name;
Scheme scheme;
std::set<Tuple> tuples;
};
#endif /* RELATION_H_ */
When I compile, I get an error as follows:
./Relation.h:25:2: error: unknown type name 'std'
std:string name;
^
./Relation.h:25:6: error: use of undeclared identifier 'string'
std:string name;
I have checked for circular dependencies (the two files included here have no other files included in them) and as you can see the <string> class is clearly included. No errors occur with std::set. What then might be causing this problem?
Update: for good measure, I isolated this file and the five associated with it (the .cpp and the two pairs included) and compiled. I got the same error.
I have searched through many posts on here and cannot seem to locate a solution to my problem. I am getting two errors when I try to compile my program, both of them are coming from one of my header files. Here are the errors:
Error 1 error C2146: syntax error : missing ';' before identifier 'datastore'
AND
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
In my cpp file I have scope resolution operators and I don't have any squiggly red lines under anything. Also the program compiled ONCE and then I saved it and reopened the program and it gave me these errors. So I think I originally "tricked" the compiler or something weird. So any help would be awesome!
#ifndef INTERNET_H
#define INTERNET_H
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sstream>
#include "Wininet.h"
#include "Internet.h"
#include "ForexPrices.h"
using namespace std;
class Internet
{
private:
ForexPrices datastore;
BOOL bResult;
char *chPtr0,
*chPtr1,
*chPtr2;
DWORD dw1,
dw2,
dwIndex;
HINTERNET hInet, hRequest;
HINTERNET h_Inet;
char ch_Buffer[4096],
ch_Line[256];
std::ofstream of_OutFile;
public:
Internet();
void openFile();
void internetCheckConnection();
HINTERNET internetopen();
HINTERNET internetconnect();
void internetclose();
void closeFile();
char* grabMargin();
double grabDailyAverageLine();
void setcurrency(char *currencyfiller1);
};
#endif
[error C2146: syntax error : missing ';' before identifier 'datastore'] is a hint that the class before 'datastore' is unknown, which leads to your next error.
[error C4430: missing type specifier - int assumed. Note: C++ does not support default-int] comes as a result of the first error. Because the compiler doesn't know what your ForexPrices class is, it is trying to use something else (I'm no expert on default-int). This is not supported and so you see this error instead.
For some reason your ForexPrices class is unknown. I see that you included the file above, ForexPrices.h. I would make sure that the name of your class is exactly the same in your header file as it is used here. Also make sure it isn't declared in a namespace that you haven't included. If so, you'll need another using statement or reference the class in the namespace (YourNamespace::ForexPrices). It's good practice not too always trust the "squigglies" I think. Visual studio can sometimes goof at least until your solution is fully parsed, but this is more of a problem on very large projects where parsing takes some time.
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.
This is weird. I created a vector just fine in one class but can't create it in another class. He's a representation of what I have:
main.h
#include <Windows.h>
#include <ShellAPI.h>
#include <vector>
#include <string>
#include <iostream>
#include "taco.h"
class MyClass
{
public:
int someint;
vector<int> myOrder;
};
taco.h
#include <vector>
class OtherClass
{
public:
vector<int> otherOrder;
};
And I get compile errors regarding the vector declaration in taco.h:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
What am I missing here? I can uncomment out that second vector declaration and it compiles fine.
Try:
std::vector<int> otherOrder;
vector is part of the std namespace. This means that whenever you use a vector in a header file, you should include the std:: prefix.
The reason that you can sometimes get away with forgetting it is that some included files may have using namespace std; in them, allowing you to leave off the prefix. However, you should avoid the using keyword in header files, for it will pollute the namespace of any files that include it.
For a more detailed explanation of the dangers of using namespace ..., see this thread.
Try std::vector<int>. You're supposed to use the namespace --- I'm assuming you have
using namespace std;
in the main.h someplace. There's a lot of talk on SO as to why using using is bad practice ; I'd recommend that you check it out.
All C++ standard library objects live in the std namespace. Try
class MyClass
{
public:
int someint;
std::vector<int> myOrder;
// ^^^^^
};
std::vector<int> ?
I'm stuck! I have this very simple test code and I can't get it to compile! I have used the same code many times before but now it won't work!
I have this simple program
#include <vector>
#include <iostream>
#include "Rswap.h"
using namespace std;
int main(){
Rswap test();
cin.get();
return 0;}
And then the rswap.cpp...
#include <vector>
#include "Rswap.h"
Rswap::Rswap(){
V.push_back(9);
};
And then the rswap.h...
#ifndef Rswap_h
#define Rswap_h
class Rswap{
public:
vector<int>V;
Rswap();
};
#endif
I'm using Visual studio 2008. Is there something wrong that is obvious and I'm missing or what could it be! As I said I have used this snippet on several differnet occassions and I can't find any difference between this and those that work...
And i have tried both
vector < int > V; and vector <int> V; without any luck
I have stared at this blindly now for some while so I thought it's better to ask here!
rswap.h(7) : error C2143: syntax error : missing ';' before '<'
rswap.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
rswap.h(7) : error C2238: unexpected token(s) preceding ';'
At the point you #include "Rswap.h", you haven't declared using namespace std; yet, so the reference to vector in Rswap.h must be qualified with a namespace. Simply declare the vector with a namespace prefix.
class Rswap{
public:
std::vector<int>V;
Rswap();
};
Also, I suggest you #include <vector> from Rswap.h rather than relying on whoever uses Rswap.h to get the #include order right.
It should be
std::vector<int>V;
Ah, your using namespace std comes too late - it should be inserted BEFORE the rswap.h include. In any case, it's MUCH better to write std::vector instead.
You need to #include <vector> in rswap.h.