C++ ';testArray未在此范围内声明';

C++ ';testArray未在此范围内声明';,c++,C++,这是我正在为班级做的作业。长话短说,我试图调用main中的allocate()函数来执行。我一直收到一个错误,即testArray未在此范围内声明。我有点困惑,如果我的返回语句是通过我的函数调用的,它怎么可能不在范围内?我希望这是有意义的,我对这些指针和函数感到困惑。任何建议都非常感谢,谢谢 #include <iostream> using namespace std; int* allocate(int&); int* allocate(int &numOfS

这是我正在为班级做的作业。长话短说,我试图调用main中的
allocate()
函数来执行。我一直收到一个错误,即
testArray
未在此范围内声明。我有点困惑,如果我的返回语句是通过我的函数调用的,它怎么可能不在范围内?我希望这是有意义的,我对这些指针和函数感到困惑。任何建议都非常感谢,谢谢

#include <iostream>
using namespace std;

int* allocate(int&);

int* allocate(int &numOfScores)
{

    int *testArray;

    //prompt user for scores
    cout << "How many test scores would\n";
    cout << "you like to process: " << endl;
    cin >> numOfScores;

    //dynammically allocate an arrray to hold the scores
    testArray = new int[numOfScores];

    //get the scores from user
    for(int count = 0; count < numOfScores; count++)
    {
        cout << "Enter Score: " << endl;
        cin >> testArray[count];
    }


    //release the memory that was allocated for *ptr 
    delete [] testArray;
    testArray = 0;

    return testArray;
}

int main()
{   

    allocate(testArray);

    return 0;

}
#包括
使用名称空间std;
int*分配(int&);
整数*分配(整数和整数)
{
int*testArray;
//提示用户输入分数

cout这是因为您正在引用testArray,就好像它是在当前函数中定义的一样,您实际需要的是

#include <iostream>
using namespace std;

int* allocate(int&);

int* allocate(int &numOfScores)
{

    int *testArray;

    //prompt user for scores
    cout << "How many test scores would\n";
    cout << "you like to process: " << endl;
    cin >> numOfScores;

    //dynammically allocate an arrray to hold the scores
    testArray = new int[numOfScores];

    //get the scores from user
    for(int count = 0; count < numOfScores; count++)
    {
        cout << "Enter Score: " << endl;
        cin >> testArray[count];
    }


    //release the memory that was allocated for *ptr 
    delete [] testArray;
    testArray = 0;

    return testArray;
}

int main()
{   
    int* testArray;
    int numberOfScores;
    testArray=allocate(numberOfScores);
    delete[] testArray;
    return 0;
}
#包括
使用名称空间std;
int*分配(int&);
整数*分配(整数和整数)
{
int*testArray;
//提示用户输入分数
cout>nun_分数;
标准::向量分数(num_分数);
//诸如此类

这是因为您引用testArray时,就好像它是在当前函数中定义的一样,您实际需要的是

#include <iostream>
using namespace std;

int* allocate(int&);

int* allocate(int &numOfScores)
{

    int *testArray;

    //prompt user for scores
    cout << "How many test scores would\n";
    cout << "you like to process: " << endl;
    cin >> numOfScores;

    //dynammically allocate an arrray to hold the scores
    testArray = new int[numOfScores];

    //get the scores from user
    for(int count = 0; count < numOfScores; count++)
    {
        cout << "Enter Score: " << endl;
        cin >> testArray[count];
    }


    //release the memory that was allocated for *ptr 
    delete [] testArray;
    testArray = 0;

    return testArray;
}

int main()
{   
    int* testArray;
    int numberOfScores;
    testArray=allocate(numberOfScores);
    delete[] testArray;
    return 0;
}
#包括
使用名称空间std;
int*分配(int&);
整数*分配(整数和整数)
{
int*testArray;
//提示用户输入分数
cout>nun_分数;
标准::向量分数(num_分数);
//诸如此类

testArray
allocate
中的一个局部变量,在
main
中不存在且不可见。如果要在
main
中创建一个局部变量,该变量将被分配
allocate
的返回值,您可以这样做:

int numberOfScores;
int* testArray = allocate(numberOfScores);
但是要意识到,由于您已
delete[]
allocate
中编辑了数组并将其设置为0,因此在
main
中创建的
testArray
将指向
NULL
。最好不要在
allocate
delete[]中将其设置为0
main
的末尾,而不是在
allocate
的内部,或者如果可以的话,使用
std::vector
来避免手动内存管理


此外,您可能希望确保
cin>>numOfScores
没有失败,如果成功,
numOfScores
大于0。

testArray
allocate
中的局部变量,并且不存在,在
main
中不可见。如果您想在中创建局部变量de>main
将分配
allocate
的返回值,您可以这样做:

int numberOfScores;
int* testArray = allocate(numberOfScores);
但是要意识到,由于您已
delete[]
allocate
中编辑了数组并将其设置为0,因此在
main
中创建的
testArray
将指向
NULL
。最好不要在
allocate
delete[]中将其设置为0
main
的末尾,而不是在
allocate
的内部,或者如果可以的话,使用
std::vector
来避免手动内存管理


此外,您可能希望确保
cin>>numOfScores
没有失败,并且如果成功,
numOfScores
大于0。

您正在使用从未创建过的变量testArray调用allocate()函数。另一个函数(即作用域)中可能存在同名变量的事实不重要--它不存在于main()中,也不存在于全局。

您正在使用从未创建过的变量testArray调用allocate()函数。另一个函数(即作用域)中可能存在同名变量这一事实并不重要--它不存在于main()中,也不是全局性的。

我明白了!谢谢大家的建议,我想既然我调用了allocate()函数,它就会返回,不管作用域如何,我现在明白了,谢谢。我明白了!谢谢大家的建议,我想既然我调用了allocate()函数不管作用域如何,它都会返回函数,我现在明白了,谢谢。不要调用
delete[]testArray
分配中
为数组释放内存?然后返回一个指向未分配空间的指针,该空间具有未定义的行为…对吗?@Mosby:只有当指针被取消引用时,它才会变为未定义的行为。不调用
delete[]testArray
分配中
释放数组的内存?然后返回一个指向未分配空间的指针,该空间具有未定义的行为…对吗?@Mosby:只有当指针被取消引用时,它才会变为未定义的行为。