文件输入和编译错误

mer*_*ley 0 c++ file-io compiler-errors ifstream

Stack Overflow 的用户您好!

我在尝试用 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()'
Run Code Online (Sandbox Code Playgroud)

作为 C++ 的新手,我不太确定这个错误试图告诉我的错误是我使用 ifstream。我确定这一定是非常简单的事情,但我无法弄清楚:( 我尝试了一些绝望的事情,例如取消引用 userFile 将其称为 (userFile*).open 和 userGFile->open。两者都会导致相同的错误。

受此影响的代码行如下:

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();
Run Code Online (Sandbox Code Playgroud)

整个程序如下:

/* 
 * 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: " << "\n\tArray #1: ";
    for (int i = 0; i < arrSize; i++) {
        cout << a[i] << "\t";
    }
    cout << "\n\tArray #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();
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢您的任何帮助!

tun*_*2fs 5

以下行应该是

 ifstream userfile;
Run Code Online (Sandbox Code Playgroud)

代替

 ifstream userfile();
Run Code Online (Sandbox Code Playgroud)

  • @user959799 :要清楚,原因是因为`ifstream userfile();` 是一个函数声明而不是一个变量声明。这被称为 [最令人烦恼的解析](http://en.wikipedia.org/wiki/Most_vexing_parse)。 (4认同)