文件输入和编译错误
File Input and Errors on Compile
大家好!
我在用c++编写程序时遇到了一个问题。每次我编译我的代码,我得到以下错误:
FCS2Phase.cpp: In function `int main(int, char**)':
FCS2Phase.cpp:47:15: error: request for member `open' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:48:22: error: request for member `eof' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:50:39: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:52:49: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:54:14: error: request for member `close' in `userfile', which is of non-class type `std::ifstream()'
作为c++的新手,我不太确定这个错误试图告诉我的是我使用ifstream不正确。我确信这必须是一些愚蠢的简单,但我不能弄清楚:(我尝试了一些绝望的事情,如解引用userFile称它为(userFile*)。打开,也使用ergfile ->打开。两者都会导致相同的错误。
受此影响的代码行如下:int count = 0;
userfile.open(fileName.c_str(), ios::in | ios::binary);
while (!userfile.eof()) {
if (count < arrSize)
getline(userfile, a[count]);
else if (count - arrSize < arrSize)
getline(userfile, b[count - arrSize]);
}
userfile.close();
整个程序如下:
/*
* File: FCS2Phase.cpp
* Author: Mark
*
* Created on October 17, 2011, 11:28 AM
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
using namespace std;
void randInput(int size, string fileName);
/*
*
*/
int main(int argc, char** argv) {
string fileName;
char randYN;
int arrSize;
cout << "(This program assumes file is in program root directory and arrays are equal size)";
cout << "nPlease insert the file name: ";
getline(cin, fileName);
ifstream userfile();
cout << "How many numbers are stored in the arrays?: ";
cin >> arrSize;
double a[arrSize];
double b[arrSize];
cout << "Create Randomized File? (Y/N): ";
cin >> randYN;
if (toupper(randYN) == 'Y')
randInput(arrSize * 2, fileName);
int count = 0;
userfile.open(fileName.c_str(), ios::in | ios::binary);
while (!userfile.eof()) {
if (count < arrSize)
getline(userfile, a[count]);
else if (count - arrSize < arrSize)
getline(userfile, b[count - arrSize]);
}
userfile.close();
cout << "The values inputted were: " << "ntArray #1: ";
for (int i = 0; i < arrSize; i++) {
cout << a[i] << "t";
}
cout << "ntArray #2: ";
for (int i = 0; i < arrSize; i++) {
cout << b[i] << "t";
}
return 0;
}
void randInput(int size, string fileName) {
const double MIN = 0;
const double MAX = 100;
double num;
ofstream file(fileName.c_str());
if (file.is_open()) {
for (int i = 0; i < size; i++) {
srandom(time(NULL));
num = (double) rand() / RAND_MAX;
file << MIN + num * (MAX - MIN) << "n";
}
file.close();
}
}
谢谢你的帮助!
下面一行应该是
ifstream userfile;
不是 ifstream userfile();
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译