C ++用const变量定义数组大小 [英] c++ defining array size with a const variable

查看:135
本文介绍了C ++用const变量定义数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int place = determinePlace(input);

const int arraySize = (place + 1);
int decimal[arraySize] = {};

嗨!

我试图使用const int变量来定义十进制[]的数组大小.但是,错误C2057和错误C2466继续出现.

I tried to use a const int variable to define the array size of decimal[]. However, error C2057 and error C2466 keeps on coming up.

有什么建议吗?

推荐答案

Joachim是对的,您正在尝试设置:

Joachim is right, you are trying to set:

const int arraySize = (determinePlace(input) + 1);

这是行不通的,因为您试图获取用户输入或类似内容,而这些内容只能在运行程序时才能访问,而在编译时则无法访问.

this doesn't work, because you are trying to get a user input or something similar which will be only accessible when you run the program not when you compile it.

我会尝试这样的事情:

#include <vector>

using std::vector;

vector<int> decimal;
decimal.resize(determinePlace(input) +1);
decimal = {};

这篇关于C ++用const变量定义数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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