我正在做一项填充空白数据类型的任务。我已经重读了无数次“学习游戏开发的C++”中的小章节,并尝试了各种类型,但我的问题是,我的问题是从枚举中调用绿色。
以下是原始代码:
// 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;
}以下是我到目前为止所完成的工作:
#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;
}我知道我不明白如何将这种颜色称为绿色。我尝试了书中建议的各种组合,但似乎都不起作用。
请帮助我,这样我就可以解决这个问题并了解原因。
提前谢谢你。
发布于 2017-05-10 23:13:31
正如Ceros在注释中所说的,您需要附加枚举类型:
colorCode gradebookColor = colorCode::Green;发布于 2017-05-10 23:14:25
更改变量的类型。
enum colorCode
{
Green = 1
};
colorCode gradebookColor = colorCode::Green;
std::cout << gradebookColor << std::endl;将输出"1“
发布于 2017-05-10 23:18:09
当您定义一个enum class时,您必须在enum的前面加上名称才能访问它的值:colorCode::Green。您也可以在不使用class说明符的情况下定义enum,这样名称就是可选的。但是,请记住,enum可以在int之间进行隐式转换。
https://stackoverflow.com/questions/43896405
复制相似问题