"不允许使用不完整类型"

时间:2016-06-17 13:35:58

标签: c++ debugging

为我的最后一年项目写一个klonkide程序。

但现在有一个让我震惊的错误。

这是我的克朗代克计划的草稿;

// ConsoleApplication18.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cctype>

using namespace std;
// Removed part

class card {
    char *rank[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    char *suit[] = {"S", "D", "H", "C"};
    char *show[] = { "Up", "Down" };

};

然而,在&#34; * rank []&#34;是一个错误,说:&#34;不允许不完整类型&#34;。我运行它时也会收到C2011错误。此外,当我尝试编写类时,上面的字符将开始获取不完整类型错误。帮助

现在我删除了&#34; struct&#34;,但不完整的类型错误仍然存​​在,现在显示:

错误C2229 class&#39; card&#39;有一个非法的零大小阵列
错误C2997&#39; card :: show&#39;:数组绑定无法从类内初始化程序中推断出

实际上结构只是因为这个错误而存在。

编辑:好的。我现在通过像下面的答案之一那样指示数组来解决这个问题。我还发现了另一个问题,很快就会有另一个问题。

3 个答案:

答案 0 :(得分:0)

您不能同时拥有struct cardclass card

答案 1 :(得分:0)

您声明了两个具有相同名称的类型,违反了一个定义规则。

考虑到在C ++中,字符串文字具有常量字符数组的类型。因此,例如,第二类应该被定义为

class card {
    const char *rank[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    const char *suit[4] = {"S", "D", "H", "C"};
    const char *show[2] = { "Up", "Down" };

};

对于错误消息,应明确指定类定义中数组的大小。

答案 2 :(得分:-1)

Visual Studio 2013并不完全符合C ++ 11标准,请看这个答案 Error: cannot specify explicit initializer for array

你可以在ctor中初始化vector。

class card_class {
    std::vector<std::string> rank;
    std::vector<std::string> suit;
    std::vector<std::string> show;
public:
    card_class()
    {
        rank = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        suit = { "S", "D", "H", "C" };
        show = { "Up", "Down" };
    }
};
相关问题