c ++ 为什么当我声明变量时,变量"file"未声明?
c++ Why is variable "file" undeclared when I have declared it?
我正在使用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();
}
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
。此外,row
和col
的float
参数类型是不合理的;使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);
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- Seg Fault Issue C++ (file IO / getline)
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 程序在尝试将 file.read() 文件() 到新变量时停止工作
- 是否可以将"Read from file"函数分配给变量?
- 错误:运行时检查失败 #2 - 变量周围的堆栈'file'已损坏
- c ++ 为什么当我声明变量时,变量"file"未声明?