c ++ 为什么当我声明变量时,变量"file"未声明?

c++ Why is variable "file" undeclared when I have declared it?

本文关键字:变量 file 未声明 声明 为什么      更新时间:2023-10-16

我正在使用visual studio 2012编写普通的c++,并且我一直在为我声明的变量收到这个错误。

1>c:usersjoeskydrivedocumentsc++consoleapplication2matracies 1.cpp(182): error C2065: 'file' : undeclared identifier
1>c:usersjoeskydrivedocumentsc++consoleapplication2matracies 1.cpp(184): error C2065: 'file' : undeclared identifier

这是我得到错误的函数。

void WriteMatrix(vector<vector<float>> Matrix, float row, float col, int choice)
{
if (choice == 1)
{
    ofstream file("MultiplyMatrix.txt", ios::app);
}
else if (choice == 2)
{
    ofstream file("AddMatrix.txt", ios::app);
}
else if (choice == 3)
{
    ofstream file("AddMatrix.txt", ios::app);
}
for(int i=0; i<row; i++)
{
    for(int j=0; j<col; j++)
    {
        float temp = Matrix[i][j];
        file<<temp<<" ";
    }
    file<<endl;
}
file<<endl;
file.close();
}
正如Oli所说,file只存在于定义它的块中。

您需要在if语句之前定义file。然后使用file.open打开所选文件。

考虑一下如果选择不是1、2或3会发生什么。

您正试图访问条件分支之外的文件变量,在条件分支中,文件变量的生命周期已结束。当您在{…}条件块中声明它们时,它将在右括号处超出范围。

正确的解决方案是在条件分支范围之外的函数开头声明它,然后在分支中打开所需的文件,或者只在条件块之后打开。

在这种情况下,我也会考虑使用switch语句,而不是continuous if/elseif/。。。因为这就是switch语句的作用!

因此,您的代码将是这样的(也有适当的缩进):

void WriteMatrix(vector<vector<float>> Matrix, float row, float col, int choice)
{

这是第一个要演示的替代方案,可能也是最好的C++解决方案:

    static const vector<string> filenameLookUpTable{"MultiplyMatrix.txt",
                                        "AddMatrix.txt", "AddMatrix.txt"};
    ofstream file(filenameLookUpTable.at(choice-1), ios::app);

你也可以做:

    ofstream file;
    switch(choice) {
    case 1:
        file.open("MultiplyMatrix.txt", ios::app);
        break;
    case 2:
        file.open("AddMatrix.txt", ios::app);
        break;
    case 3:
        file.open("AddMatrix.txt", ios::app);
        break;
    }

你也可以这样写:

    string filename;
    switch(choice) {
    case 1:
        filename = "MultiplyMatrix.txt";
        break;
    case 2:
        filename = "AddMatrix.txt";
        break;
    case 3:
        filename = "AddMatrix.txt";
        break;
    }
    ofstream file(filename, ios::app);

然后结束,基本上:

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            float temp = Matrix[i][j];
            file<<temp<<" ";
        }
        file<<endl;
    }
    file<<endl;
    file.close();
}

在C++中,{ }用于表示范围。这意味着file仅在if语句中声明。您可以通过在if语句外部声明file并在内部打开它们来解决此问题

ofstream file; int choice;
if (choice == 1)
{
    file.open("MultiplyMatrix.txt", ios::app);
}
else if (choice == 2)
{
    file.open("AddMatrix.txt", ios::app);
}
else if (choice == 3)
{
    file.open("AddMatrix.txt", ios::app);
}
    return 0;
}
void WriteMatrix(vector<vector<float>> Matrix, float row, float col, int choice)

这通过值传递Matrix,这意味着复制,这对于大型矩阵来说可能很耗时。相反,如果不应该更改,则通过引用将其传递给const。此外,rowcolfloat参数类型是不合理的;使CCD_ 13。

void WriteMatrix(vector<vector<float>> const& Matrix, int row, int col, int choice)

然后这个代码,

if (choice == 1)
{
    ofstream file("MultiplyMatrix.txt", ios::app);
}
else if (choice == 2)
{
    ofstream file("AddMatrix.txt", ios::app);
}
else if (choice == 3)
{
    ofstream file("AddMatrix.txt", ios::app);
}

做两件事:

  • 它选择文件名

  • 它将打开和关闭文件的代码增加了三倍。

在每个分支中,file变量是一个局部自动变量,它在大括号块的末尾不存在(因此,文件关闭)。

三重冗余是不好的,以后不能访问要使用的变量也是不好的。因此,将该逻辑移到文件名选择之后。然后,选择本身可以简化为简单的数组索引:

assert( 1 <= choice && choice <= 3 );
static char const* const names =
{ "MultiplyMatrix.txt", "AddMatrix.txt", "AddMatrix.txt" };
ofstream file( names[choice - 1], ios::app);