有没有什么办法让一个可变长度的数组在C + +全球性的? [英] Is there any way to make a variable length array global in c++?

查看:112
本文介绍了有没有什么办法让一个可变长度的数组在C + +全球性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个函数创建一个可变长度的数组,但我需要参考这个阵列中的第二功能。当我把上面的声明出现该问题的main()看到,因为它的长度还没有确定,我的编译器生气。

一个人如何通常去呢?

编辑:

下面是我的code为止。

我需要使阵列的名称[]中期[]和最后的[] 全球性的。他们都在 student_input()

 的#include<&iostream的GT;
使用命名空间std;无效student_input();
无效student_output();INT I,NS;主要(){    INT宽度,高度,MULT;    COUT<< 输入学生的数量:其中p其中p ENDL;
    CIN>>纳秒;    I = 0;
    而(I< NS){
           I = I + 1;
           student_input();
           }    I = 0;
    而(I< NS){
           I = I + 1;
           student_output();
           }    系统(暂停);
}无效student_input(){
    INT si_i,si_midterm,si_final,中期[NS + 1],最后的[NS + 1];
    字符串si_name,名[NS + 1];    COUT<< ENDL<< ENDL<< \\ t -----学生<< I<< -----<< ENDL<< ENDL<< ENDL;    COUT<< 输入学生姓名<< I<< :\\ t的<< ENDL;
    CIN>> si_name;
    名称由[i] = si_name;    COUT<< 进入中期成绩学生<< I<< :\\ t的<< ENDL;    CIN>> si_midterm;
    中期[I] = si_midterm;    COUT<< 进入最后的考试成绩为学生的<< I<< :\\ t的<< ENDL;
    CIN>> si_final;
    最后的[I] = si_final;    COUT<< ENDL<< ENDL;    si_i = 0;
    而(si_i 7;){
          si_i = si_i + 1;
          COUT<< 进入实验室<< si_i<<学生<< I<< :\\ t的<< ENDL;
          }    COUT<<名称[1] - ;< ENDL<<期中[1] - ;&下; ENDL&所述;&下;终[1] - ;&下; ENDL;
    返回;
}无效student_output(){
    COUT<<你好!<< ENDL;
    返回;
}


解决方案

C ++不支持可变长度数组;要么你不使用C ++或者您使用的是实现特定的语言扩展。

在C ++中,你应该使用的std ::矢量的动态大小的数组。

如果您需要从多个函数来访问它,您可以:


  • 有需要访问载体的功能需要它的一个引用作为参数,或者

  • 请在矢量 A类成员变量,并需要访问它的类成员函数的所有功能。

哪一个更有意义,要看是什么,究竟你正在尝试做的。

I've created a variable length array in one function, however I need to refer to this array in a second function. The problem occurs when I put the declaration above main() seeing as its length hasn't been defined yet, my compiler gets angry.

How does one typically go about this?

EDIT:

Here is my code so far.

I need to make the array's name[] midterm[] and final[] global. They're all in student_input().

#include <iostream>
using namespace std ;

void student_input();
void student_output();

int i , ns ;

main(){

    int width,height,mult;

    cout << "Enter the number of students:" << endl;
    cin >> ns;

    i = 0 ;
    while( i < ns){
           i = i + 1 ;
           student_input();
           }

    i = 0 ;
    while( i < ns){
           i = i + 1 ;
           student_output();
           }

    system("pause");  
}

void student_input() {
    int si_i,si_midterm,si_final, midterm[ns + 1], final[ns + 1];
    string si_name, name[ns + 1]; 

    cout <<  endl <<  endl << "\t----- Student " << i << " -----" << endl << endl << endl;

    cout << "Enter name for student " << i << ":\t"<< endl;
    cin >> si_name;
    name[i] = si_name ; 

    cout << "Enter midterm score for student " << i << ":\t"<<  endl;

    cin >> si_midterm;
    midterm[i] = si_midterm ;

    cout << "Enter final exam score for student " << i << ":\t"<<  endl ;
    cin >> si_final;
    final[i] = si_final ;

    cout <<  endl <<  endl;

    si_i = 0 ;
    while (si_i < 7){
          si_i = si_i + 1; 
          cout << "Enter lab " << si_i <<" for student " << i << ":\t"<<  endl;
          }

    cout << name[i] <<  endl << midterm[i] <<  endl<<final[i] <<  endl;      
    return;
}

void student_output() {
    cout <<"hello! "<<  endl;
    return;
}

解决方案

C++ does not support variable length arrays; either you are not using C++ or you are using an implementation-specific language extension.

In C++ you should use std::vector for a dynamically sized array.

If you need to access it from multiple functions you can:

  • have the functions that need access to the vector take a reference to it as an argument, or
  • make the vector a class member variable and make all the functions that need to access it member functions of the class.

Which one makes more sense depends on what, exactly, you are trying to do.

这篇关于有没有什么办法让一个可变长度的数组在C + +全球性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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