我在c ++遇到问题:
#include <iostream>
class Apple{
public:
int price = 100;
enum class color {
red =1, green, yellow
};
};
int main() {
Apple apple;
std::cout << Apple::color::green << std::endl;
}
当我尝试编译此代码时,会出现以下消息:
[错误]&#39; Apple :: color&#39;不是类或命名空间
答案 0 :(得分:4)
使用正确的c ++ 11标志后,您必须重载operator <<
friend std::ostream& operator <<( std::ostream& os, const color& c )
{
/* ... */
return os;
}
答案 1 :(得分:1)
启用c++11
因为enum class
是一个c ++ 11功能,起诉 - std=c++11
编译器标志。
如果您想要<<
cout
Apple::color
运算符
以下内容应该有效:
#include <iostream>
class Apple {
public:
int price = 100;
enum class color { red = 1, green, yellow };
};
std::ostream& operator<<(std::ostream& os, const Apple::color& c) {
if (c == Apple::color::red) std::cout << "Red\n";
if (c == Apple::color::green) std::cout << "Green\n";
if (c == Apple::color::yellow) std::cout << "Yellow\n";
return os;
}
int main() {
Apple apple;
std::cout << Apple::color::green << std::endl;
}
答案 2 :(得分:0)
为了使用enum class
,您的编译器必须支持C ++ 11。例如,如果您使用clang或g ++构建命令,可以通过附加-std=c++11
来实现。新版本的Visual Studio自动启用C ++ 11。
正如@ no operator "<<
所指出的那样,你应该得到的错误是enum class
,因为 let startHour: Int = 6
let endHour: Int = 23
let date1: NSDate = NSDate()
let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
let components: NSDateComponents = gregorian.components(([.day, .month, .year]), from: date1 as Date) as NSDateComponents
components.hour = startHour
components.minute = 0
components.second = 0
let startDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate
components.hour = endHour
components.minute = 0
components.second = 0
let endDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate
timePicker.datePickerMode = .time
timePicker.minimumDate = startDate as Date
timePicker.maximumDate = endDate as Date
timePicker.setDate(startDate as Date, animated: true)
timePicker.reloadInputViews()
timePicker.frame = CGRect(x: 0.0, y: (self.view.frame.height/2 + 60), width: self.view.frame.width, height: 150.0)
timePicker.backgroundColor = UIColor.white
// timePicker.reloadInputViews()
self.view.addSubview(timePicker)
timePicker.addTarget(self, action: #selector(RoundTripViewController.startTimeDiveChanged), for: UIControlEvents.valueChanged)
没有隐式转换为任何内容,因此不会输出整数值。
答案 3 :(得分:-1)
P0W的答案在两个计数上都是正确的,但是如果您只想输出基础值,那么投射可能更简单,而不是使插入运算符过载。
using enum_type = std::underlying_type<Apple::color>::type;
enum_type value = (enum_type)Apple::color::green;
std::cout << value << '\n';