将类对象放在向量内? [英] Placing a class object inside a vector?

查看:184
本文介绍了将类对象放在向量内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我可以在一个向量中放置一个类;这里是我的程序,其中我收到以下错误:

I noticed that I can place a Class inside a vector; here is my program, where I am receiving the following error:

/out:blackjack.exe
blackjack.obj
blackjack.obj : error LNK2019: unresolved external symbol "private: static class
 std::vector<class Card,class std::allocator<class Card> > Card::Cards" (?Cards@
Card@@0V?$vector@VCard@@V?$allocator@VCard@@@std@@@std@@A) referenced in functio
n "public: static void __cdecl Card::blankCard(void)" (?blankCard@Card@@SAXXZ)
blackjack.exe : fatal error LNK1120: 1 unresolved externals

问题出现在`blankcard()函数中,我试图创建一个新的类实例,并将其放入向量。

The issue is inside the `blankcard() function, where I'm trying to create a new class instance, and put it into the vector.

有人可以阅读代码告诉我我的问题,给我一个很好的例子,如何做到这一点?谢谢。

Can someone please read the code tell me my issue and give me a good example in how to do this? Thank you.

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Card
{
  private:
    string suit;
    int number;
    static vector<Card> Cards;

  public:
    Card::Card();
    Card::Card(string cardS, int cardV);
    static void createCards(); 
    static void blankCard();    
};


int main()
{
  Card::blankCard();
  return 0;
}

Card::Card(){};
Card::Card(string cardS, int cardV){};

void Card::blankCard()
{
   int temp = 1;
   int cardValue; 
   string suit;
   int suitCount = 1;

   for(int i = 1; i < 52; i++)
   {
     if(temp == 1 || temp > 13)
     {
       temp = 1;
       cardValue = temp;
       temp ++;
     }
     else if(temp > 1)
     { 
       cardValue = temp;
       temp ++;
     }

     if(suitCount <= 13)
     {
       suit = "spade";
       suitCount++;
     }
     else if( suitCount >= 14 && suitCount <= 26)
     {
       suit = "club";
       suitCount++;
     }
     else if(suitCount >= 27 && suitCount <= 39)
     {
       suit = "heart";
       suitCount++;
     }
     else if(suitCount >= 40 && suitCount <= 52)  
     {
       suit = "diamonds";
       suitCount++;
     }     

     Card a = Card(suit, cardValue);
     Cards.push_back(a);
   }
}


推荐答案

正在声明类定义中的静态成员 Cards ,但是你没有在任何地方定义它。在类定义之后添加一个定义:

You are declaring the static member Cards inside the class definition, but you aren't defining it anywhere. Add a definition after the class definition:

vector<Card> Card::Cards;

这篇关于将类对象放在向量内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆