2017-05-10 9 views
0

空のデータ型を入力するための作業を行っています。 私は「ゲーム開発のためのC++の学習」で小さな章を何度も読んできましたが、さまざまなタイプを試しましたが、 私の問題は 列挙型の緑色を呼び出しています。ここでC++でのデータ型

は元のコードです:

#include "stdafx.h" 
#include <cstdlib> 
#include <iostream> 
using namespace std; 

int main(int atgc, const char * arg[]) 
{ 
float classAverage = 90.7f; //Decimal number 
char letterScore = 'A'; //Single letter 
int testScore = 95; //Whole number value 
float classTestAverage = 88.4f; //Decimal number, notice the 'f' at the end 
enum class colorCode { 
    Green = 1, 
    Yellow = 5, 
    Red = 10 
}; 

unsigned int gradebookColor; //Stores list of values 
colorCode gradebookColor = colorCode::Green; //This line errors out  bool isStudentPassing = true; //Could be true or false 
cout << "The class average is currently " 
    << classAverage 
    << endl; 
cout << "The class test average was " 
    << classTestAverage 
    << endl; 
cout << "Your test score was " 
    << testScore 
    << endl; 
cout << "Your current letter score is " 
    << letterScore 
    << endl; 
cout << "The color of your gradebook entry is " 
    << gradebookColor 
    << endl; 
cout << "Are you passing? " 
    << boolalpha  //This line allows the word 'true' or 'false' to be printed instead of '0' or '1' 
    << isStudentPassing 
    << endl; 
return 0; 
} 

を私は色のグリーンを呼び出す方法を理解していないのです知っている:ここで

// DataTypes.cpp : The data types to declare each of the variables is missing. 
// Based on the value being stored in the variable and the comments beside it, 
// fill in the data type at the beginning of each line. Then compile and run 
// program to make sure you selected the correct types. 
// 
// After you submit your answers, try changing the values stored in the 
// variables. What can you learn about the different data types? 
// 

#include "stdafx.h" 
#include <cstdlib> 
#include <iostream> 
using namespace std; 

int main(int atgc, const char * arg[]) 
{ 
classAverage = 90.7f; //Decimal number 
letterScore = 'A'; //Single letter 
testScore = 95; //Whole number value 
classTestAverage = 88.4f; //Decimal number, notice the 'f' at the end 
colorCode{ 
    Green = 1, 
    Yellow = 5, 
    Red = 10 
} gradebookColor; //Stores list of values 
gradebookColor = Green; //This line does not need a declaration, it was declared in the line above 
isStudentPassing = true; //Could be true or false 

cout << "The class average is currently " 
    << classAverage 
    << endl; 
cout << "The class test average was " 
    << classTestAverage 
    << endl; 
cout << "Your test score was " 
    << testScore 
    << endl; 
cout << "Your current letter score is " 
    << letterScore 
    << endl; 
cout << "The color of your gradebook entry is " 
    << gradebookColor 
    << endl; 
cout << "Are you passing? " 
    << boolalpha  //This line allows the word 'true' or 'false' to be printed instead of '0' or '1' 
    << isStudentPassing 
    << endl; 
return 0; 
} 

は、私がこれまでに完了しているものです。私は本の中で示唆されている様々な組み合わせを試みたが、何も動作していないようだ。

私はこれを修正して理由を理解できるように手伝ってください。

ありがとうございます。

colorCode gradebookColor = colorCode::Green; 
+3

'colorCode gradebookColor = colorCode :: Green' – Ceros

答えて

1

は、あなたが列挙型を追加する必要があります。 colorCode::Green:あなたはenum classを定義するとウィル出力「1」

+0

ありがとうございます。私はそれを試みました。私はまだ同じエラーを受け取ります:緑:宣言されていない識別子。 – rmac

1

変更あなたの変数の種類:同様Cerosコメントで述べている

+0

enumクラスをenumに変更して、int gradebookColorが受け入れられるようにしましたが、 "green"は表示されません。 – rmac

+0

列挙カラーコード{ \t \t緑= 1、 \t \t、5 =黄\t \t赤= 10 \t}。 \t \t unsigned int gradebookColor; //値のリストを格納します \t colorCode gradebookColor = colorCode :: Green; //値のリストを格納します。 //この行は宣言を必要とせず、上の行で宣言されています \t bool isStudentPassing = true; // trueまたはfalseである可能性があります – rmac

+0

@rmacこの 'unsigned int gradebookColor = colorCode :: Green;'が動作します。また、コンソールへの印刷では、列挙型の文字列表現は決して印刷されません。悲しいことに数値だけです。 – Ceros

1

は、あなたがその値にアクセスするenumの名前を付加する必要が

enum colorCode 
{ 
    Green = 1 
}; 

colorCode gradebookColor = colorCode::Green; 

std::cout << gradebookColor << std::endl; 

class指定子なしでenumを定義することもできます。名前はオプションです。ただし、enumは暗黙的にintに変換可能であることに注意してください。

0

データ型: - データ型は、データ型とそれを処理するための関連する操作を識別する手段です。 基本的に2種類のデータ型 1.基本的なデータ型(int、char、bool、float、double、void) 2.単純な言語の詳細の詳細については、 を参照してください。下記のリンクにあります。 https://simplifiedtutorial4u.blogspot.in/2017/08/data-types-in-c.html