C++ 错误:向量未命名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8403468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: vector does not name a type
提问by Chris De Bow
I'm having lots of errors in my final project (a poker and black Hyman sim). I'm using a vector to implement the "hands" in the blackHyman class, and I'm using a structured data type declared in another class, which is publicly inherited. The error I'm worried about is the compiler I'm using telling me that I'm not declaring a type in the vector.
我在我的最终项目(扑克和黑Hyman sim)中有很多错误。我正在使用向量来实现二十一点类中的“手”,并且我正在使用另一个类中声明的结构化数据类型,该类型是公开继承的。我担心的错误是我使用的编译器告诉我我没有在向量中声明类型。
blackHyman header file:
黑Hyman头文件:
#ifndef BLACKHyman_H
#define BLACKHyman_H
#include <vector>
#include "card.h"
class blackHyman: public cards
{
private:
vector <Acard> playerHand;
vector <Acard> dealerHand;
public:
blackHyman();
void dealHands();
void hitOrStay();
void dealerHit();
int handleReward(int);
void printHands();
};
#endif
card header file (this is the class black Hyman inherits from):
卡头文件(这是黑Hyman继承的类):
#ifndef CARD_H
#define CARD_H
const char club[] = "\xe2\x99\xa3";
const char heart[] = "\xe2\x99\xa5";
const char spade[] = "\xe2\x99\xa0";
const char diamond[] = "\xe2\x99\xa6";
//structured data to hold card information
//including:
// a number, representing Ace-King (aces low)
//a character, representing the card's suit
struct Acard
{
int number;
char pic[4];
};
// a class to hold information regarding a deck of cards
//including:
//An array of 52 Acard datatypes, representing our Deck
//an index to the next card in the array
//a constructor initializing the array indices to Ace-king in each suit
//a function using random numbers to shuffle our deck of cards
//13 void functions to print each card
class cards
{
private:
Acard Deck[52];
int NextCard;
public:
cards();
Acard getCard();
void shuffleDeck();
void cardAce(char[]);
void cardTwo(char[]);
void cardThree(char[]);
void cardFour(char[]);
void cardFive(char[]);
void cardSix(char[]);
void cardSeven(char[]);
void cardEight(char[]);
void cardNine(char[]);
void cardTen(char[]);
void cardHyman(char[]);
void cardQueen(char[]);
void cardKing(char[]);
};
#endif
回答by Hauleth
You forgot to add std::
namespace prefix to vector
class name.
您忘记为类名添加std::
命名空间前缀vector
。
回答by Alok Save
use:
用:
std::vector <Acard> playerHand;
everywhere qualify it by std::
无处不在 std::
or do:
或做:
using std::vector;
in your cpp file.
在您的 cpp 文件中。
You have to do this because vector
is defined in the std
namespace and you do not tell your program to find it in std
namespace, you need to tell that.
你必须这样做,因为它vector
是在std
命名空间中定义的,你没有告诉你的程序在std
命名空间中找到它,你需要告诉它。
回答by robin ranjan
Also you can add #include<vector>
in the header. When two of the above solutions don't work.
您也可以#include<vector>
在标题中添加。当上述两种解决方案不起作用时。
回答by dasblinkenlight
You need to either qualify vector
with its namespace (which is std
), or import the namespace at the top of your CPP file:
您需要vector
使用其命名空间(即std
)进行限定,或者在 CPP 文件的顶部导入命名空间:
using namespace std;