I cannot define a string array for unknown reasons on C++. I need to randomly get a string from this array.
Been trying the following:
string bands[] = { "Rammstein", "Slipknot", "Franz Ferdinand", "Gorillaz" };
I'm getting the following as error:
error C2146: syntax error : missing ';' before identifier 'bands'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C3845: 'SpurdoSparde::Form1::bands': only static data members can be initialized inside a ref class or value type
Just a reminder, I'm using a Windows forms applications. Not sure if it makes any difference whatsoever.
It seems you are not including string and/or not using std::string. The following works:
#include <string>
int main()
{
std::string bands[] = { "Rammstein", "Slipknot", "Franz Ferdinand", "Gorillaz" };
}
If you are after a dynamically sized collection of strings, you should prefer std::vector<std::string> over a C-style array. If you need fixed size, have a look at std::array<std::string, N> or tr1 or boost alternatives if you don't have C++11.
You either didn't include #include <string> or need to add the std:: namespace qualifier to your type:
std::string bands[] = { ... };
Prefer this over doing using namespace std;.
Also, you might should prefer to use a std::vector rather than a plain old C-style array:
std::vector<std::string> bands = { ... };
Related
My question relates to three files and how they relate to each other:
In one file I have a bunch of predefined types such as Uint, int32, etc.
In the other two I have a class that is used to categories exceptions (which is mostly static functions) and definitions for the class.
All of the types are in the file Types.h, with a macro which allows the types to be defined globally:
namespace Enigma {
//Omitted Types
typedef std::uint32_t Uint32;
typedef std::string string;
//Omitted Types
}
#if defined(USING_GLOBAL_TYPES)
using namespace aNamespace;
#endif
In the other files I have the following (or similar to it anyway):
Header file:
#include "Types.h"
namespace Enigma {
class ExceptionCategory {
typedef Uint32 CategoryID;
static CategoryID GetIDFromName(const string& name) noexcept;
};
}
Source file:
Engima::ExceptionCategory::CategoryID Enigma::ExceptionCategory::GetIDFromName(const string& name) noexcept {
//Omitted Code
}
Now the problem lies within the Source file according to the error messages the Compiler is throwing at me which include the following:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error: missing ',' before '&'
error C2511: 'Enigma::ExceptionCategory::IDType Enigma::ExceptionCategory::GetIDFromName(const int) noexcept': overloaded member function not found in 'Enigma::ExceptionCategory'
Edit: Major Rewording
In the cpp file, remove the double colon before the namespace.
aNamespace::int32 aNamespace::aClass::aStaticFunction() noexcept {
//does a thing
}
Quite a simple mistake:
Was missing the namespace Enigma before string in the source code.
It's unusual because it has been working fine for ages until suddenly it stopped working
Engima::ExceptionCategory::CategoryID Enigma::ExceptionCategory::GetIDFromName(const Enigma::string& name) noexcept {
//Omitted Code
}
practice.h
struct CandyBar
{
string name;
double weight;
int calories;
};
practice.cpp
#include <iostream>
#include <string>
#include "practice.h"
using namespace std;
int main()
{
CandyBar snacks{ "Mocha Munch", 2.3, 350 };
cout << snacks.name << "\t" << snacks.weight << "\t" << snacks.calories << endl;
return 0;
}
when I build the solution, I get the errors:
practice.h(5): error C2146: syntax error : missing ';' before identifier 'name'
practice.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2440: 'initializing' : cannot convert from 'const char [12]' to 'double'
There is no context in which this conversion is possible
practice.cpp(20): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
practice.cpp(20): error C2078: too many initializers
practice.cpp(22): error C2039: 'name' : is not a member of 'CandyBar'
practice.h(4) : see declaration of 'CandyBar'
what is cause of all the errors? why won't the variables get recognized as fields of the struct?
The issue is that when the headers parser there is no type string.
The best way is to include the namespace e.g.
struct CandyBar
{
std::string name;
double weight;
int calories;
};
This does not show up in the cpp file as you have using namespace std;
You could put the using line before the #include "practice.h" but that is considered bad style as the header is now not self contained and you could have namespace conflicts.
You need to include in practice.h.
Like so:
#include <string>
struct CandyBar
{
std::string name; // And also std:: before string, as Praetorian pointed out
double weight;
int calories;
};
The include is not required, but you must either import namespace std or fully qualify its usage. So either repeat the using statement or declare name as type std::string.
You should use "#ifndef", "#define". Because maybe header files can call to initialize some times. Therefore you take errors. Look this: C++’ta Seperated Compilation II – Header File Kullanmak
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.
At the top of my file main.h I have:
#include <vector>
class Blah
{
public:
Blah(){}
~Blah(){}
protected:
vector<int> someVector;
public:
//methods
};
When I try to compile, the vector declaration line gives the errors:
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 ';'
I can't figure out what's causing this. Anybody see what I'm doing wrong?
The vector class is part of the std namespace. You need to replace your declaration with std::vector<int> instead.
It's in the std namespace:
std::vector<int> someVector;
vector is part of the std namespace and so you need to add std:: to your declaration:
std::vector<int> someVector;
Since the suggestion was made in another answers, I want to also discourage the use of using namespace std since it is considered bad practice
Instead of using,
std::vector someVector;
Always try to use,
using namespace std;
Because it will help you not to type 'std::' again and again, and it is not considered a good practice.
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.