![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 66
| Syntax error 'constant' 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);
};
|
| SterlingM is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| 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.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,783
| 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;
};
You could also probably use push_back instead of initializing the size.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #4 |
| MENTAL DETECTOR Join Date: Apr 2006 Location: United States
Posts: 3,292
| 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
__________________ <Niggawatts> Writing is both mechanical and organic <Niggawatts> It's like a cyborg dragon. <Niggawatts> Writing is like a cyborg dragon. |
| whiteflags is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,783
| That unfortunately only works on PODs, ie non-class types.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| MENTAL DETECTOR Join Date: Apr 2006 Location: United States
Posts: 3,292
| 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
Sorry for being a language lawyer but you caught me while bored.
__________________ <Niggawatts> Writing is both mechanical and organic <Niggawatts> It's like a cyborg dragon. <Niggawatts> Writing is like a cyborg dragon. Last edited by whiteflags; 10-22-2009 at 03:21 AM. Reason: wrote a declaration by mistake |
| whiteflags is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,783
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| what am I missing? (Program won't compile) | steals10304 | C Programming | 3 | 08-25-2009 03:01 PM |
| more then 100errors in header | hallo007 | Windows Programming | 20 | 05-13-2007 08:26 AM |
| We Got _DEBUG Errors | Tonto | Windows Programming | 5 | 12-22-2006 05:45 PM |
| Using VC Toolkit 2003 | Noobwaker | Windows Programming | 8 | 03-13-2006 07:33 AM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |