C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-20-2009, 01:56 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 66
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.
SterlingM is offline   Reply With Quote
Old 10-20-2009, 02:01 PM   #2
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 10-21-2009, 09:00 AM   #3
Mysterious C++ User
 
Elysia's Avatar
 
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;
};
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.
__________________
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 10-22-2009, 02:43 AM   #4
MENTAL DETECTOR
 
whiteflags's Avatar
 
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
There is no problem with constant or variable structs being initialized the old fashioned way.
__________________
<Niggawatts> Writing is both mechanical and organic
<Niggawatts> It's like a cyborg dragon.
<Niggawatts> Writing is like a cyborg dragon.
whiteflags is offline   Reply With Quote
Old 10-22-2009, 02:51 AM   #5
Mysterious C++ User
 
Elysia's Avatar
 
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 10-22-2009, 03:14 AM   #6
MENTAL DETECTOR
 
whiteflags's Avatar
 
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
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.
__________________
<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   Reply With Quote
Old 10-22-2009, 03:35 AM   #7
Mysterious C++ User
 
Elysia's Avatar
 
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:03 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22