Default data type for const keyword - int-type?

One book (Object Oriented Programming in C ++ by E. Balagurusamy) says that

 const size = 10;  



 const int size = 10; 


but the g ++ compiler (version-4.6.1 in ubuntu) gives an error like

error: ‘size’ does not name a type 


what should I do based on this?

  • g ++ does not support this feature.
  • This is a new feature. The latest version of g ++ supports it.
  • The statement is incorrect. The data type is required with the const keyword.
  • Something else.

source to share

3 answers

Sounds like a mistake in the book ... you should definitely name the type or alias (i.e. a typedef

) since C ++ is a strongly typed language.

This is what the C ++ 03 specification points to objects, declarations and definitions:

Section 1.8 / 1:

Object properties are determined when the object is created. An object can have a name (section 3). An object has a storage duration (3.7), which affects its lifetime (3.8). The object is of type (3.9). The term object type refers to the type with which the object is created.

Then in section 3.1 / 1:

Declaration is a definition ... [note: the rest of the paragraph is an exception to this rule and is omitted as they do not apply in this case]

Then in section 3.1 / 6:

A program is ill-formed if the definition of any object gives the object an incomplete type

Finally, section 3.9.2 / 1 says:

... the term object type (1.8) includes the cv qualifiers specified when the object was created. The presence of a const in a-specifier-seq declares an object of the object type that matches const; such an object is called a const object ....

So, according to 3.9.2 / 1, const

is a classifier, not a type, and as such, it must qualify a valid unqualified type. Second, in the above example, according to 3.1 / 1, the declaration is size

also a definition, so the object size

must have an associated type, or the program is poorly formed according to 3.1 / 6.



Mr. Balagurus is wrong. Completely wrong. The type is required, with const

or without a keyword .

You must stop reading this book. Nor should you read the books of Yashwant Kanateler. I know that the books of these authors are very popular among students of many universities in India.

I suggest you get the introductory book from this list:



I would go: the expression is wrong. The data type is required with the const keyword.



All Articles