有没有办法在c ++中创建一个可变长度数组全局?

[英]Is there any way to make a variable length array global in c++?


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.

我在一个函数中创建了一个可变长度数组,但是我需要在第二个函数中引用这个数组。当我将声明置于main()之上时,问题就出现了,因为它的长度尚未定义,我的编译器生气了。

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().

我需要使数组的名称[] midterm []和final []全局。他们都在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;
}

3 个解决方案

#1


13  

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

C ++不支持可变长度数组;要么您没有使用C ++,要么使用特定于实现的语言扩展。

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

在C ++中,您应该使用std :: vector作为动态大小的数组。

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.
  • 使vector成为一个类成员变量,并使所有需要访问它的函数成为该类的成员函数。

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

哪一个更有意义取决于您正在尝试做什么。

#2


3  

What's wrong with std::vector? You cannot have a VLA in C++ (g++ provides it as an extension though).

std :: vector有什么问题?你不能在C ++中拥有VLA(g ++虽然提供了它作为扩展)。

#3


1  

Look into vectors, because although you can in c, in standard c++ theres no way to use variable length arrays.

查看向量,因为虽然你可以在c中,但在标准c ++中,没有办法使用可变长度数组。

Also, please look into a new IDE other than bloodshed, such as Code::Blocks, or if necessary, at least a more updated version of Dev c++ than bloodshed.

另外,请查看除了流血之外的新IDE,例如Code :: Blocks,或者如果需要,至少比流血更新版本的Dev c ++。

  1. Dev-C++ has not been updated since 2005, and is not currently maintained. The software is very buggy. At the time of my writing there are 340 known bugs that will never be fixed.

    Dev-C ++自2005年以来一直没有更新,目前尚未维护。该软件非常多。在我写作的时候,有340个已知的错误永远无法修复。

  2. It’s hard to get help, because the programming community have moved on to newer software.

    很难获得帮助,因为编程社区已经转向更新的软件。

  3. Dev-C++ lacks features that are present in more modern solutions. Code completion, intellisense, and proper debugging facilities (among others) are not provided. These tools can greatly improve the workflow and efficiency of an experienced programmer, and may aid the learning of beginners.

    Dev-C ++缺乏更现代化解决方案中的功能。不提供代码完成,智能感知和适当的调试工具(等)。这些工具可以极大地改善有经验的程序员的工作流程和效率,并可能有助于初学者的学习。

  4. Error messages and the steps required to solve them are poorly documented compared to more modern solutions, and because most programmers have moved on from Dev-C++ it can be difficult (if not impossible) to find anyone who is able to help you. Some problems may not be able to be solved at all. The compiler included with Dev-C++ is very out-dated, and buggy. An out-dated compiler can result in buggy and inefficient code, and may be damaging to the learning process for a beginner.

    与更现代的解决方案相比,错误消息和解决它们所需的步骤记录很少,而且由于大多数程序员已经从Dev-C ++转移,因此找到能够帮助您的人很困难(如果不是不可能)。有些问题可能根本无法解决。 Dev-C ++中包含的编译器已经过时了,并且有错误。过时的编译器可能导致错误和低效的代码,并且可能对初学者的学习过程造成损害。

  5. The provided “devpack” system is no longer supported by modern libraries. Using external libraries in Dev-C++ can be a confusing and difficult process for beginners who are expecting this simple system to handle it for them.

    现代图书馆不再支持所提供的“devpack”系统。在Dev-C ++中使用外部库对于期望这个简单系统为它们处理它的初学者来说可能是一个令人困惑和困难的过程。

Note: the beginner things aren't directly pointed at you, this was taken from here: http://clicktobegin.net/programming/why-you-shouldnt-use-dev-c/

注意:初学者没有直接指向你,这是从这里获取的:http://clicktobegin.net/programming/why-you-shouldnt-use-dev-c/


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/10/11/5117113593254cbd52c366d3b541f69b.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号