如何调用一个函数,该函数在一个单独的类中打印出一组结构?

时间:2015-10-27 09:48:51

标签: c++ arrays class struct

我是新手,所以我希望我做对了。 问题:我和Bill Weinman一起观看了Lynda.com C ++教程,并且有一个示例代码,可以打印出常规扑克牌中的所有牌。该结构定义了一张卡片,该阵列包含每52张卡片。如果所有内容都写在主类中,代码就可以工作。我的问题是分离这些代码并将其放在一个额外的类中。我不知道为什么。

#include <cstdio>
#include <iostream>

//#include "Cards.h"
using namespace std;


struct card{
    unsigned int rank;
    unsigned int suit;
};

enum card_suit { HERZ, KARO, PIK, KREUZ };
enum card_rank { ASS = 1, BUBE = 11, DAME = 12, KOENIG = 13 };

const char * assString = "Ass";
const char * bubeString = "Bube";
const char * dameString = "Dame";
const char * koenigString = "Koenig";
const char * herzString = "Herz";
const char * karoString = "Karo";
const char * pikString = "Pik";
const char * kreuzString = "Kreuz";


card deck[52] = {
        { ASS, HERZ }, { 2, HERZ }, { 3, HERZ }, { 4, HERZ }, { 5, HERZ }, { 6, HERZ },
        { 7, HERZ }, { 8, HERZ }, { 9, HERZ }, { 10, HERZ }, { BUBE, HERZ }, { DAME, HERZ }, { KOENIG, HERZ },
        { 1, KARO }, { 2, KARO }, { 3, KARO }, { 4, KARO }, { 5, KARO }, { 6, KARO },
        { 7, KARO }, { 8, KARO }, { 9, KARO }, { 10, KARO }, { 11, KARO }, { 12, KARO }, { 13, KARO },
        { ASS, PIK }, { 2, PIK }, { 3, PIK }, { 4, PIK }, { 5, PIK }, { 6, PIK },
        { 7, PIK }, { 8, PIK }, { 9, PIK }, { 10, PIK }, { BUBE, PIK }, { DAME, PIK }, { 13, PIK },
        { ASS, KREUZ }, { 2, KREUZ }, { 3, KREUZ }, { 4, KREUZ }, { 5, KREUZ }, { 6, KREUZ },
        { 7, KREUZ }, { 8, KREUZ }, { 9, KREUZ }, { 10, KREUZ }, { BUBE, KREUZ }, { 12, KREUZ }, { 13, KREUZ }
};

void printCards(const card & c){
    switch (c.suit){
    case HERZ:
        printf("%s ", herzString);
        break;
    case KARO:
        printf("%s ", karoString);
        break;
    case PIK:
        printf("%s ", pikString);
        break;
    case KREUZ:
        printf("%s ", kreuzString);
        break;
    }
    if (c.rank >= 2 && c.rank <= 10){
        printf("%d\n", c.rank);
    }
    else {
        switch (c.rank){
        case ASS:
            printf("%s\n", assString);
            break;
        case BUBE:
            printf("%s\n", bubeString);
            break;
        case DAME:
            printf("%s\n", dameString);
            break;
        case KOENIG:
            printf("%s\n", koenigString);
            break;
        }
    }

}


int main(int argc, char ** argv) {

    for (auto & c : deck){
        printCards(c);
    }

    system("PAUSE");

    return 0;
}

所以这就是我所做的:

主要课程:

#include <cstdio>
#include <iostream>

#include "Cards.h"
using namespace std;

int main(int argc, char ** argv) {
    Cards poker;

    for (auto & c : poker.deck){
        poker.printCards(c);
    }
    system("PAUSE");
    return 0;
}

Cards.h

#pragma once
#include <cstdio>
#include <string>

using namespace std;

struct card{
    unsigned int rank;
    unsigned int suit;
};

class Cards
{
public:
    Cards();
    ~Cards();

    enum card_suit { HERZ, KARO, PIK, KREUZ };
    enum card_rank { ASS = 1, BUBE = 11, DAME = 12, KOENIG = 13 };

    const char * assString = "Ass";
    const char * bubeString = "Bube";
    const char * dameString = "Dame";
    const char * koenigString = "Koenig";
    const char * herzString = "Herz";
    const char * karoString = "Karo";
    const char * pikString = "Pik";
    const char * kreuzString = "Kreuz";

    card deck[52] = {
            { ASS, HERZ }, { 2, HERZ }, { 3, HERZ }, { 4, HERZ }, { 5, HERZ }, { 6, HERZ },
            { 7, HERZ }, { 8, HERZ }, { 9, HERZ }, { 10, HERZ }, { BUBE, HERZ }, { DAME, HERZ }, { KOENIG, HERZ },
            { 1, KARO }, { 2, KARO }, { 3, KARO }, { 4, KARO }, { 5, KARO }, { 6, KARO },
            { 7, KARO }, { 8, KARO }, { 9, KARO }, { 10, KARO }, { 11, KARO }, { 12, KARO }, { 13, KARO },
            { ASS, PIK }, { 2, PIK }, { 3, PIK }, { 4, PIK }, { 5, PIK }, { 6, PIK },
            { 7, PIK }, { 8, PIK }, { 9, PIK }, { 10, PIK }, { BUBE, PIK }, { DAME, PIK }, { 13, PIK },
            { ASS, KREUZ }, { 2, KREUZ }, { 3, KREUZ }, { 4, KREUZ }, { 5, KREUZ }, { 6, KREUZ },
            { 7, KREUZ }, { 8, KREUZ }, { 9, KREUZ }, { 10, KREUZ }, { BUBE, KREUZ }, { 12, KREUZ }, { 13, KREUZ }
    };
    void printCards(const card & c);
};

Cards.cpp

#include "Cards.h"

Cards::Cards()
{

}

Cards::~Cards()
{
}

void Cards::printCards(const card & c){
    switch (c.suit){
    case HERZ:
        printf("%s ", herzString);
        break;
    case KARO:
        printf("%s ", karoString);
        break;
    case PIK:
        printf("%s ", pikString);
        break;
    case KREUZ:
        printf("%s ", kreuzString);
        break;
    }
    if (c.rank >= 2 && c.rank <= 10){
        printf("%d\n", c.rank);
    }
    else {
        switch (c.rank){
        case ASS:
            printf("%s\n", assString);
            break;
        case BUBE:
            printf("%s\n", bubeString);
            break;
        case DAME:
            printf("%s\n", dameString);
            break;
        case KOENIG:
            printf("%s\n", koenigString);
            break;
        }
    }
}

我知道,这是一个非常长的代码,但我不知道应该在哪里减少它。 最后有这个错误:

Error   1   error C2536: 'Cards::Cards::deck' : cannot specify explicit initializer for arrays

我锄你可以帮帮我。即使我把静电放在“甲板”前面,我也会收到错误。我展示了这个代码两个人,他们无法帮助我,我在这个网站上搜索,但往往一系列的结构是主要的。所以这很有效,但我把它分开是个问题。

由于

2 个答案:

答案 0 :(得分:0)

与错误消息一样,C样式数组在C ++中不是一等公民,不能像在类中那样初始化。

我会在课堂外声明一个spring.datasource.url = jdbc:mysql://localhost:3306/dsm spring.datasource.username = root spring.datasource.password = admin123 spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 并从课程中复制一份new_deck。构造

答案 1 :(得分:0)

您无法为类定义中的变量赋值(就像使用卡数组,字符串一样)。您需要为您的类创建一个.cpp文件,并在该类的构造函数中初始化这些变量。

即使您使用静态变量,也必须在类之外声明它们,并在那里为它们赋值。