-
Syntax error 'constant'
I just started a class for a deck of cards. I made four vectors but the compiler gives me this error -
1>c:\users\sterling\documents\visual studio 2008\projects\cards\cards\carddeck.h(14) : error C2059: syntax error : 'constant'
1>c:\users\sterling\documents\visual studio 2008\projects\cards\cards\carddeck.h(15) : error C2059: syntax error : 'constant'
1>c:\users\sterling\documents\visual studio 2008\projects\cards\cards\carddeck.h(16) : error C2059: syntax error : 'constant'
1>c:\users\sterling\documents\visual studio 2008\projects\cards\cards\carddeck.h(17) : error C2059: syntax error : 'constant'
Code:
#include <vector>
#include <iostream>
using namespace std;
#pragma once
class CardDeck
{
public:
CardDeck(void);
~CardDeck(void);
private:
vector <char> spades(13);
vector <char> clubs(13);
vector <char> diamonds(13);
vector <char> hearts(13);
};
That is the the code for it. I don't see what is wrong with it. Do vectors need to be..constant? I have used them a couple times before (but they were int vectors) and never had this error.
-
It looks like you are trying to initialise the vectors to contain 13 elements each, but such initialisation should be done in the constructor initialisation list, not class definition.
By the way, do not use using directives at namespace scope in header files. Qualify vector as std::vector instead.
-
Correct way:
Code:
#include <vector>
#include <iostream>
using namespace std;
#pragma once
class CardDeck
{
public:
CardDeck(void): spades(13), clubs(13), diamonds(13), hearts(13);
~CardDeck(void);
private:
vector <char> spades;
vector <char> clubs;
vector <char> diamonds;
vector <char> hearts;
};
Like with structs, the contents of the members cannot be initialized. This must be done in a constructor.
You could also probably use push_back instead of initializing the size.
-
You can initialize a struct's members:
Code:
int main (void)
{
struct standardDeck {
int spades[13];
int clubs[13];
int diams[13];
int hearts[13];
}
proof = {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, }
};
const struct standardDeck PROOF = {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, }
};
return 0;
}
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions
"ComeauTest.c", line 9: warning: variable "proof" was declared but never referenced
proof = {
^
"ComeauTest.c", line 16: warning: variable "PROOF" was declared but never referenced
const struct standardDeck PROOF = {
^
In strict mode, without -tused, Compile succeeded
There is no problem with constant or variable structs being initialized the old fashioned way.
-
That unfortunately only works on PODs, ie non-class types.
-
It technically works with a compile time expression just as C does, except invoking the copy constructor to initialize:
Code:
int main (void)
{
class cProof
{
};
struct wrapper
{
cProof p;
};
cProof empty;
wrapper wrapIt = {
empty
};
return 0;
}
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions
"ComeauTest.c", line 13: warning: variable "wrapIt" was declared but never referenced
wrapper wrapIt = {
^
In strict mode, without -tused, Compile succeeded
I'm sure it breaks only when structs are actually made out to be C++ classes (i.e. any constructors, et alli). Constants may only work if the expression was also constant thanks to the type strictness. I don't see why it wouldn't work with template class objects either, but that might depend on the order which things are compiled. The point is that C++ does support the intializer syntax completely for C constructs even if you're doing C++ things.
Sorry for being a language lawyer but you caught me while bored.
-
Yes, this should work. Only if a struct contains member functions or constructors would it be considered a non-POD. There may be other rules too, but those are the ones I remember.
So long as it is a POD, the initializer list works and works just like you would expect, with copy constructors and all.