2011-10-17 43 views
0

堆棧溢出的用戶!文件輸入和編譯錯誤

我遇到了一個我試圖用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 *)。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(); 

整個程序如下:

/* 
* 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(); 
    } 
} 

感謝您的幫助

+0

現在好了,我覺得很傻,只要我公佈這方面,我想改變YM的方法來輸入,並將其改爲如下(與它修復了這個問題):ifstream userfile(fileName.c_str()); – meriley

回答

4

以下行應該是

ifstream userfile; 

,而不是

ifstream userfile(); 
+0

夠簡單了,現在當我與我的代碼戰鬥時,我總覺得自己總是很傻。謝謝! – meriley

+4

@ user959799:要清楚,原因是因爲'ifstream userfile();'是一個函數聲明而不是變量聲明。這被稱爲[最煩人的解析](http://en.wikipedia.org/wiki/Most_vexing_parse)。 – ildjarn