Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2012
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2012 by Ivor Horton; ISBN: 978-1-118-36808-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2012 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2013, 10:52 PM
Registered User
 
Join Date: Mar 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch 2 Example 9 Compile Error

I noticed that this compiles fine with Visual Studio 2012, but not with Visual Studio 2010? 2010 seems to have an issue with using "class" in the enum declaration.

Most of the problems seem to lie in line 23:
enum class Color : char {Red, Orange, Yellow, Green, Blue, Indigo, Violet};

Code:
// Ex2_09.cpp
// Demonstrating type-safe and non-type-safe enumerations
#include <iostream>

using std::cout;
using std::endl;

// You can define enumerations at global scope
//enum Jewels {Diamonds, Emeralds, Rubies}; // Uncomment this for an error
enum Suit : long {Clubs, Diamonds, Hearts, Spades};

int main()
{
	// Using the old enumeration type...
	Suit suit = Clubs;							// You can use enumerator names directly
	Suit another = Suit::Diamonds;				// or you can qualify them

	// Automatic conversion from enumeration type to integer
	cout << "suit value: " << suit << endl;
	cout << "Add 10 to another: " << another + 10 << endl;

	// Using type-safe enumerations
	enum class Color : char {Red, Orange, Yellow, Green, Blue, Indigo, Violet};
	Color skyColor(Color::Blue);				// You must qualify enumerator names
	// Color grassColor(Green);					// Uncomment for an error

	// No auto conversion to numeric type
	cout << endl
		<< "Sky color value: "
		<< static_cast<long>(skyColor) << endl;

	//cout << skyColor + 10L << endl;			// Uncomment for an error
	cout << "Incremented sky color: "
		<< static_cast<long>(skyColor) + 10L	// OK with explicit cast
		<< endl;

	return 0;
}
Visual Studio 2010 gives the following compilation errors
Code:
1>e:\my documents\dropbox\dropbox\programming\ebook projects\horton cpp\ex2_09\ex2_09\ex2_09.cpp(23): error C2332: 'enum' : missing tag name
1>e:\ex2_09.cpp(23): error C2236: unexpected 'class' 'main::Color'. Did you forget a ';'?
1>e:\ex2_09.cpp(23): error C3381: 'main::Color' : assembly access specifiers are only available in code compiled with a /clr option
1>e:\ex2_09.cpp(23): error C2062: type 'char' unexpected
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C2143: syntax error : missing ';' before '}'
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(24): error C2039: 'Blue' : is not a member of 'main::Color'
1>          e:\ex2_09.cpp(23) : see declaration of 'main::Color'
1>e:\ex2_09.cpp(24): error C2065: 'Blue' : undeclared identifier
1>e:\ex2_09.cpp(30): error C2440: 'static_cast' : cannot convert from 'main::Color' to 'long'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>e:\ex2_09.cpp(34): error C2440: 'static_cast' : cannot convert from 'main::Color' to 'long'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I'm really confused as to what is going on here.
 
Old March 6th, 2013, 10:57 PM
Registered User
 
Join Date: Mar 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Going back and reading chapter 2, page 87 (Type Safe Enums) it seems that this must be a new addition to C++ that was introduced in 2011. So VS2010 doesn't recognize it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch.7, Compile Download Code for first part - Many Hours! jem18rc BOOK: Professional Unified Communications Development with Microsoft Lync Server 2010 0 October 15th, 2011 08:54 PM
Compile warning. Ch.3, p.53. PEHowland BOOK: Beginning iPad Application Development 1 September 13th, 2010 10:31 PM
CH 14 Pages 749-753 Compile Errors Razzy The Pug BOOK: Ivor Horton's Beginning Visual C++ 2005 1 March 28th, 2008 10:20 AM
Compile error: Syntax error: & Else without HELP Corey VB How-To 2 April 21st, 2006 03:25 PM
Ch 16 -- compile error w/ components adidaswood BOOK: Beginning ASP.NET 1.0 0 September 22nd, 2004 08:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.