表达式在C ++ VS中未求出常量 [英] expression did not evaluate to a constant in C++ VS

查看:120
本文介绍了表达式在C ++ VS中未求出常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误,其中在VLA的声明中给出了表达式未求值为常量"的错误-我试图声明int l,m,r,n1,n2常量,但仍然无法正常工作.虽然我知道编译器希望在编译时知道固定大小的数组的概念,但是我很少看到在线实现的实现,人们已经实现了以下实现.

I am running into error where in declaration of VLA is giving error of "expression did not evaluate to a constant" - I tried to declare int l,m,r,n1,n2 constant but still it doesn't work. While I am aware of concept that compiler would like to know fixed size array at compile time, however I have seen few implementation online where folks have implemented as below.

其他Wiki搜索在此处输入链接描述显示并非所有版本的C ++都支持它-

Additional wiki search enter link description here have shown not all version of C++ support it -

问题-如何在不创建动态内存分配的情况下使其工作?

Question - how to make it work without creating dynamic memory allocation ?

template<typename T>
void mergesort<T>::_merge_array(int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    /* create temp arrays */
    int L[n1], R[n2];  // error -
}

推荐答案

严格符合要求的C ++实现没有可变长度数组.使用 std::vector 代替:

A strictly-conforming C++ implementation doesn't have variable-length arrays. Use std::vector instead:

#include <vector>

...

std::vector<int> L(n1), R(n2);

这篇关于表达式在C ++ VS中未求出常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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