如何在c++中定义类中的常量数组?

时间:2021-06-15 19:51:24

I wish to define an array in a class, and set the variables of the class to the elements of the array. The implementation below results in segmentation fault:

我希望在类中定义一个数组,并将类的变量设置为数组的元素。下面的实现导致分割错误:

class Grade {
    char MAP[];
    char *letter;
public:
    Grade();
    ~Grade();
    void set(int);
};
 Grade::Grade(){
    letter = new char;
    *letter = '\0';

    MAP[0] = 'A';
    MAP[1] = 'B';
    MAP[2] = 'C'; // result in segmentation fault

    MAP = { 'A', 'B', 'C'}; // result in segmentation fault
    }

Grade::~Grade(){
    delete letter;
    delete percent;
}

void Grade::set(int a){
    *letter = MAP[a];
}

How should I fix it?

我该怎么修理?

3 个解决方案

#1


3  

Quickest way is to change char MAP[]; to char MAP[3];

最快的方法是更改char MAP[];字符映射[3];

There are other interesting things in the code.

代码中还有其他有趣的东西。

1) It does not compile as given (you never define what a percent is).
2) What happens if someone sends an "int a" to your set function that is outside of the range of your map? (IE: 56 instead of 0, 1, or 2)?

1)它不会按给定的方式进行编译(您永远不会定义百分数)。2)如果有人向您的set函数发送一个“int a”,而该函数超出了映射的范围,会发生什么情况?(例如:56而不是0 1或2)?

#2


2  

Making some assumptions about what you intend the code to do, it appears that the easiest fix is to replace the current

对代码要做什么做了一些假设之后,看起来最简单的解决方法是替换当前代码

char MAP[];
char *letter;

...

void Grade::set(int a){
    *letter = MAP[a];
}

with

char letter_;

...

void Grade::set( int const grade )
{
    // Assuming grade in range 1 through 5 inclusive.
    letter_ = "ABCDEF"[grade - 1];
}

By the way, it's a good idea to reserve ALL UPPERCASE identifiers for macros. That way you minimize the probability of unintended text substitutions. Also, it's easier on the eyes.

顺便说一下,最好将所有大写标识符保留为宏。这样你就可以最小化非预期文本替换的可能性。而且,它对眼睛更容易。

#3


1  

In case of Arrays the compiler requires you to explicitly declare the number of elements in your array. This is to ensure that the size of the array is determined before the execution.

对于数组,编译器要求您显式地声明数组中的元素数量。这是为了确保在执行之前确定数组的大小。

Hence declaring an array as follows char MAP[]; will always result in a compilation error.

因此声明数组如下:char MAP[];将始终导致编译错误。

A probable solution for your problem will be to [as mentioned by David D] to declare it as char MAP[3];

您的问题的一个可能的解决方案将是(如David D所提到的)将其声明为char映射[3];

But, in case your requirement is to have dynamic allocation you can declare it as

但是,如果您的需求是动态分配,您可以声明为。

char *MAP;

and inside the constructor allocate the required memory as follows:

在构造函数内部分配所需内存如下:

MAP = new char[ n ];

新char[n];

where n denotes the number of elements you require inside the array.

其中n表示数组中需要的元素数量。

Note: If you declare the array dynamically you need to use

注意:如果动态声明需要使用的数组

  delete[] MAP;

during destruction to complete free the memory.

在毁灭期间完成释放记忆。

#1


3  

Quickest way is to change char MAP[]; to char MAP[3];

最快的方法是更改char MAP[];字符映射[3];

There are other interesting things in the code.

代码中还有其他有趣的东西。

1) It does not compile as given (you never define what a percent is).
2) What happens if someone sends an "int a" to your set function that is outside of the range of your map? (IE: 56 instead of 0, 1, or 2)?

1)它不会按给定的方式进行编译(您永远不会定义百分数)。2)如果有人向您的set函数发送一个“int a”,而该函数超出了映射的范围,会发生什么情况?(例如:56而不是0 1或2)?

#2


2  

Making some assumptions about what you intend the code to do, it appears that the easiest fix is to replace the current

对代码要做什么做了一些假设之后,看起来最简单的解决方法是替换当前代码

char MAP[];
char *letter;

...

void Grade::set(int a){
    *letter = MAP[a];
}

with

char letter_;

...

void Grade::set( int const grade )
{
    // Assuming grade in range 1 through 5 inclusive.
    letter_ = "ABCDEF"[grade - 1];
}

By the way, it's a good idea to reserve ALL UPPERCASE identifiers for macros. That way you minimize the probability of unintended text substitutions. Also, it's easier on the eyes.

顺便说一下,最好将所有大写标识符保留为宏。这样你就可以最小化非预期文本替换的可能性。而且,它对眼睛更容易。

#3


1  

In case of Arrays the compiler requires you to explicitly declare the number of elements in your array. This is to ensure that the size of the array is determined before the execution.

对于数组,编译器要求您显式地声明数组中的元素数量。这是为了确保在执行之前确定数组的大小。

Hence declaring an array as follows char MAP[]; will always result in a compilation error.

因此声明数组如下:char MAP[];将始终导致编译错误。

A probable solution for your problem will be to [as mentioned by David D] to declare it as char MAP[3];

您的问题的一个可能的解决方案将是(如David D所提到的)将其声明为char映射[3];

But, in case your requirement is to have dynamic allocation you can declare it as

但是,如果您的需求是动态分配,您可以声明为。

char *MAP;

and inside the constructor allocate the required memory as follows:

在构造函数内部分配所需内存如下:

MAP = new char[ n ];

新char[n];

where n denotes the number of elements you require inside the array.

其中n表示数组中需要的元素数量。

Note: If you declare the array dynamically you need to use

注意:如果动态声明需要使用的数组

  delete[] MAP;

during destruction to complete free the memory.

在毁灭期间完成释放记忆。