C ++通过枚举作为参数 [英] C++ pass enum as parameter
本文介绍了C ++通过枚举作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个类似这样一个简单的类卡:
If I have a simple class like this one for a card:
class Card {
public:
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };
Card(Suit suit);
};
然后我想在另一个文件中创建一个卡的实例如何传递枚举?
and I then want to create an instance of a card in another file how do I pass the enum?
#include "Card.h"
using namespace std;
int main () {
Suit suit = Card.CLUBS;
Card card(suit);
return 0;
}
错误:'Suit'未在此范围内声明
error: 'Suit' was not declared in this scope
我知道这是有效的:
#include "Card.h"
using namespace std;
int main () {
Card card(Card.CLUBS);
return 0;
}
但是如何在另一个文件中创建一个类型为Suit的变量? p>
but how do I create a variable of type Suit in another file?
推荐答案
使用 Card :: Suit
范围。实际上,你应该参考这样的西装;我有点惊讶, Card.CLUBS
编译,我一直认为你必须做 Card :: CLUBS
。
Use Card::Suit
to reference the type when not inside of Card's scope. ...actually, you should be referencing the suits like that too; I'm a bit surprised that Card.CLUBS
compiles and I always thought you had to do Card::CLUBS
.
这篇关于C ++通过枚举作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文