Compiling problems with wstring in c++ - c++

I need to refactor a .dll for a Zinc based Flash application.
After copy&paste a class from the master to the branch, I'm getting some strange compiling errors:
GameInfo.h(15): error C2146: syntax error : missing ';' before identifier 'm_wsVersion'
GameInfo.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
GameInfo.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
The addressed code:
// removed the comments
#include "stdafx.h"
#include <string.h>
class GameInfo {
public:
UINT m_uiGameId;
wstring m_wsVersion; // Line 15
UINT m_uiCheckSum;
wstring m_wsFilePath; // Same error report as on line 15
public:
static BOOL createFromFile(wstring path, GameInfo &target); // error "error C2061: syntax error : identifier 'wstring'" thrown
};
I use Visual Studio 2010 and in the IDE itself everything is okay, no syntactical errors or something like that. And as said I did not touch the code, headers seem fine.
Has anyone a clue what about this error?

Try using the string header, and qualifying the namespace:
#include <string>
class GameInfo {
....
std::wstring m_wsVersion;
};

#include <string> is the right standard include in C++ for string classes and use std::wstring.
I strongly recommend AGAINST using a using namespace std; inside one of your headers, as you would force anybody using the header to pull in the std stuff into the global namespace.

Related

C++ .h and .cpp file separate

I'm trying to separate my c++ code to one header and one cpp file, but the interpreter showing some errors.
Here is my code:
Password.h:
#ifndef PASSWORD_H
#define PASSWORD_H
class Password {
private:
string aliasName_;
int hashOfPassword_;
public:
void setAliasName(string aliasName);
void setHashOfPassword(int hashOfPassword);
string getAliasName() { return aliasName_; }
int getHashOfPassword() { return hashOfPassword_; }
};
#endif
Password.cpp:
#include <string>
#include "Password.h"
using std::string;
void Password::setAliasName(string aliasName) {
aliasName_ = aliasName;
}
void Password::setHashOfPassword(int hashOfPassword) {
hashOfPassword_ = hashOfPassword;
}
Errors:
Error C2065 'aliasName_': undeclared identifier X\password.cpp 7
Error C2511 'void Password::setAliasName(std::string)': overloaded member function not found in 'Password' X\password.cpp 6
Error C3646 'aliasName_': unknown override specifier X\password.h 6
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int X\password.h 6
Error C2061 syntax error: identifier 'string' X\password.h 9
Error C3646 'getAliasName': unknown override specifier X\password.h 11
Error C2059 syntax error: '(' X\password.h 11
Error C2334 unexpected token(s) preceding '{'; skipping apparent function body X\password.h 11
Anyone have any ideas ?
You need to move using std::string before the declaration of your class:
#ifndef PASSWORD_H
#define PASSWORD_H
#include <string>
using std::string;
class Password {
…
and remove it from your .cpp file.
Also, you might want to use #pragma once instead of the traditional #ifndef/#define/#endif and finally you might want to make your argument and methods const when need be.
You need to move
#include <string>
using std::string;
To inside Password.h, you can then remove these two lines from Password.cpp.
Just add std:: to every string in your header file and remember, you should never use using or using namespace there! You should also include all the headers to the file in which you use them.

Visual studio errors in compiling

I keep getting the 2 errors listed below for the line that I have also included below, can someone please tell me why?
Error 1 error C2143: syntax error : missing ';' before '<'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line:
deque; <string> get_image_filename_list(string foldername);
First of all, try to include header at the beggining of your source file:
#include <deque>
#include <string>
using namespace std;
Then, try to modify your line like:
deque<string> get_image_filename_list(string foldername);

A dll with a vector of vectors. in one of its classes methods

In my c++ program i'm trying to create a dll that houses the functionality of my a* algorithm.
I encounter a problem when trying to pass the map into it, I first tried to use a 2d array, but that limited my map sizes, so i'm now trying to use a vector in a vector and I keep hitting some odd snag.
In my dlls .h file:
namespace IInterface
{
class IInterface
{
public:
// Sets the map
static __declspec(dllexport) void setMap(int h, int w,vector<vector<byte>> &myarray);
private:
static vector<vector<byte>> mymap;
}
Finaly in the .cpp i have:
#include "IInterface.h"
#include <Windows.h>
#include <stdexcept>
#include <vector>
using namespace std;
namespace IInterface
{
void IInterface::setMap(int h, int w,vector<vector<byte>> &myarray)
{
mymap = myarray;
}
}
Im getting a few errors on compilation even tho the code looks fine to me.
error C2061: syntax error : identifier 'vector' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 7 1 DMAstarDLL
error C2143: syntax error : missing ';' before '<' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2238: unexpected token(s) preceding ';' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2511: 'void IInterface::IInterface::setMap(int,int,std::vector<_Ty> &)' : overloaded member function not found in 'IInterface::IInterface' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.cpp 13 1 DMAstarDLL
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
I looked at some samples, but there was really nothing that matched this scenario. I have a sneaking suspicion i'm forgetting something crucial, but I cant see it. Ideas on getting this to work?
your dlls.h does not include vector type - you should tell the compiler vector definition and include .
Tip: don't use using namespace std; in header file only in cpp. Instead of this use std::vector ...etc.
Secondly, be careful when your dll interface contains stl. This library differs as regards Release and Debug versions, so if you load Release dll in Debug program you could have problems.

C++ Beginner - Trouble using classes inside of classes

I am working on a college project, where I have to implement a simple Scrabble game.
I have a player class (containing a Score and the player's hand, in the form of a std::string, and a score class (containing a name and numeric (int) score).
One of Player's member-functions is Score getScore(), which returns a Score object for that player. However, I get the following error on compile time:
player.h(27) : error C2146: syntax error : missing ';' before identifier 'getScore'
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : warning C4183: 'getScore': missing return type; assumed to be a member function returning 'int'
player.h(35) : error C2146: syntax error : missing ';' before identifier '_score'
player.h(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Here's lines 27 and 35, respectively:
Score getScore(); //defined as public
(...)
Score _score; //defined as private
I get that the compiler is having trouble recognizing Score as a valid type... But why? I have correctly included Score.h at the beginning of player.h:
#include "Score.h"
#include "Deck.h"
#include <string>
I have a default constructor for Score defined in Score.h:
Score(); //score.h
//score.cpp
Score::Score()
{
_name = "";
_points = 0;
}
Any input would be appreciated!
Thanks for your time,
Francisco
EDIT:
As requested, score.h and player.h:
http://pastebin.com/3JzXP36i
http://pastebin.com/y7sGVZ4A
You've got a circular inclusion problem - relatively easy to fix in this case.
Remove the #include "Player.h" from Score.h.
See this question for an explanation and discussion of what you'd need to do if Score actually used Player.
As for the compiler errors you're getting, that's how Microsoft's compiler reports the use of undefined types -you should learn to mentally translate them all into "Type used in declaration not defined".
Your problem is the recursive include: Score.h is trying to include Player.h, and Player.h is trying to include Score.h. Since the Score class doesn't seem to actually be using the Player class, removing #include "Player.h" from Score.h should solve your problem.
You have a circular dependency problem : Score.h includes Player.h and Player.h includes Score.h.
Do solve this problem, delete your include to Player.h in Score.h and define class Player; if you need to use it in Score class.

Trouble compiling a header file in VC++

I just reorganized the code for a project and now I'm getting errors I can't resolve. This header is included by a .cpp file trying to compile.
#include "WinMain.h"
#include "numDefs.h"
#include <bitset>
class Entity
{
public:
Entity();
virtual ~Entity();
virtual bitset<MAX_SPRITE_PIXELS> getBitMask();
virtual void getMapSection(float x, float y, int w, int h, bitset<MAX_SPRITE_PIXELS>* section);
};
I'm getting these compiler errors for the declaration of Entity::getBitMask():
error C2143: syntax error : missing ';' before '<'
error C2433: 'Entity::bitset' : 'virtual' not permitted on data declarations
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
There are more similar errors for the next line as well. It seems like bitset isn't getting included but it clearly is? I can't figure out what's going wrong. WinMain.h includes windows.h, and numDefs.h includes nothing.
Using MS Visual C++ 2008.
Declare the bitset as std::bitset<MAX_SPRITE_PIXELS>.
The bitset template is defined in the std:: namespace, so you either need to reference it by it's full name std::bitset or add using namespace std; somewhere before the class declaration.
I think you need to say std::bitset.
Looks like an error in "numDefs.h"