Syntax error 'constant'

This is a discussion on Syntax error 'constant' within the C++ Programming forums, part of the General Programming Boards category; I just started a class for a deck of cards. I made four vectors but the compiler gives me this ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    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.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    That unfortunately only works on PODs, ie non-class types.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    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.
    Last edited by whiteflags; 10-22-2009 at 03:21 AM. Reason: wrote a declaration by mistake

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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