错误c2065:'filename':未声明的标识符

时间:2015-11-18 00:10:25

标签: c++

#include <iostream>
#include <ostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>    


void GetOutputFileStream(std::ofstream * fout, std::string filename);
void PrintStatistics(std::ostream & fout,
    int numUsed,
    int numNew,
    double newTotalPrice,
    double newTotalMileage,
    double usedTotalPrice,
    double usedTotalMileage);


int main()
{

double newTotalPrice = 33333;
double newTotalMileage = 44444;
double usedTotalPrice = 22222;
double usedTotalMileage = 99999;
int numUsed = 2;
int numNew = 3;
std::ofstream fout; // 'f'ile out - fout
std::string filename = "statistics.txt";
GetOutputFileStream(&fout, filename);
// Print to screen
PrintStatistics(std::cout,
    numUsed,
    numNew,
    newTotalPrice,
    newTotalMileage,
    usedTotalPrice,
    usedTotalMileage);
// Print to file
PrintStatistics(fout,
    numUsed,
    numNew,
    newTotalPrice,
    newTotalMileage,
    usedTotalPrice,
    usedTotalMileage);


std::cout << "Press ENTER to continue";
std::cin.get();

return 0;
}

void GetOutputFileStream(std::ofstream * fout, std::string filename)
{
    fout->open(filename, std::ios::out);
}
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage)
{

}

PrintStatistics为空,因为我想在开始编写函数之前修复此错误。

我一直收到:错误C2065:'filename':未声明的标识符

然而,每当我尝试测试GetOutputFileStream(&amp; fout,filename);使用int main()中的样本函数确保其功能如下所示:

std::ofstream fout; // 'f'ile out - fout
std::string filename = "newFile.txt";
GetOutputFileStream(&fout, filename);
fout << "This is my new file!\n";
fout << "This is on a new line!";
fout.close();

我没有收到任何错误,并且该函数的行为与其假设相同。谁能指出我正确的方向?谢谢。

1 个答案:

答案 0 :(得分:1)

导致您遇到麻烦的不是filename。在使用它们之前,不要定义以下函数:

void GetOutputFileStream(std::ofstream * fout, std::string filename);
void PrintStatistics( ... );

您需要对它们进行原型设计,或者在使用它们之前对其进行定义。 See here for more info.

Here are your actual compiler errors.

And here is the same function with one way of fixing them.