2017-10-15 182 views
0

這是我第一次在C類和頭工作++和我遇到了這些錯誤,放在一起我的代碼多個錯誤與功能的標題,重載函數

  • 功能後,不會採取1個參數//對於所有識別符「fstream的」 //頭文件錯誤

該方案是:4層的功能

  • 重載成員函數不在「動物」 //同樣對於所有4個fncs
  • 語法錯誤發現旨在讀取和寫入文件,同時還進行搜索並能夠操作二進制文件。我很確定這個錯誤在我的聲明中,但我自己弄不清楚;任何幫助表示讚賞。

    的main.cpp

    #include <iostream> 
    #include<iomanip> 
    #include <fstream> 
    
    #include"animals.h" 
    
    using namespace std; 
    int main() 
    { 
        Animals nA; 
    
        fstream animalFile; 
        int choice; 
        cout << setprecision(2) << fixed; 
        do 
        { 
         // Display the menu. 
         cout << "\n1. Add a new animal\n"; 
         cout << "2. Remove an animal\n"; 
         cout << "3. Search and display a animal\n"; 
         cout << "4. Display all animals\n"; 
         cout << "5. Exit\n"; 
         do 
         { 
          cout << "Enter your choice (1-5): "; 
          cin >> choice; 
         } while (choice < 1 || choice > 5); 
         // Process the selection. 
         switch (choice) 
         { 
          // Choice 1 is to add an animal 
         case 1: 
          nA.addAnimal(animalFile); 
          break; 
          // Choice 2 is to remove an animal 
         case 2: 
          nA.removeAnimal(animalFile); 
          break; 
          // Choice 3 is to search and display 1 animal 
         case 3: 
          nA.searchAnimal(animalFile); 
          break; 
          // Choice 4 is to display all animals 
         case 4: 
          nA.displayAnimal(animalFile); 
         } 
        } while (choice != 5); 
        system("pause"); 
        return 0; 
    } 
    

    Animals.h

    #ifndef ANIMALS_H 
    #define ANIMALS_H 
    #include <string> 
    #include <iostream> 
    #include <fstream> 
    
    class Animals 
    { 
    private : 
        std::string name; 
        int age; 
    public : 
        //Default constructor 
        Animals(); 
        //Create an animal object 
        Animals(std::string name, int age); 
        //Add a new animal record 
        void addAnimal(fstream &d); 
        //Remove an animal record 
        void removeAnimal(fstream &d); 
        //Displays an animal through a search 
        void searchAnimal(fstream &d); 
        //Display ALL animals 
        void displayAnimal(fstream &d); 
    }; 
    
    #endif 
    

    Animals.cpp

    #include <iostream> 
    #include <iomanip> 
    #include <string> 
    #include <fstream> 
    
    #include "Animals.h" 
    using namespace std; 
    
    Animals::Animals() 
    { 
        name = "NULL"; 
        age = 0; 
    } 
    
    Animals::Animals(std::string name, int age) 
    { 
        Animals *newAnimal = new Animals; 
    
    } 
    
    void Animals::addAnimal(fstream &d) 
    { 
        string userName; 
        int userAge = 0; 
        int stringRemainder; 
        int record; 
    
        const int RECORD_SIZE = 40; 
    
        //Collecting user input 
        do 
        { 
         std::cout << "Please enter your animal name: "; 
         std::cin >> userName; 
         std::cout << "\n"; 
        } while (sizeof(userName) > 30); 
    
        do 
        { 
         std::cout << "Please enter your animal age: "; 
         std::cin >> userAge; 
         std::cout << "\n"; 
    
        } while (userAge <= 0 || !isdigit(userAge)); 
    
        //Fixing length of string 
        stringRemainder = 30 - sizeof(userName); 
    
        //Finds record number based on position 
        record = (d.tellg() % RECORD_SIZE) + 1; 
    
        //Writing to file 
    
        d.close(); 
    
        d.open("animals.txt", std::ios_base::app | ios::binary); 
    
        d << record << ""; 
    
        d << userName; 
    
        for (int i = 0; i < stringRemainder; i++) 
        { 
         d << ""; 
        } 
    
        d << userAge << "\n"; 
    
        d.close(); 
    
    
    } 
    
    void Animals::removeAnimal(fstream &d) 
    { 
        int recordNumber = 0; 
        const int RECORD_SIZE = 40; 
        char recordBuffer[RECORD_SIZE]; 
    
        d.open("animals.txt", ios::out| ios::in |ios::binary); 
    
        //Collecting user input 
        do 
        { 
         cout << "Enter the record of the animal to be removed: "; 
         cin >> recordNumber; 
        } while (recordNumber <= 0 || !isdigit(recordNumber)); 
    
        // move pointer to desired position, and overwrite! 
        d.seekp((recordNumber-1) * RECORD_SIZE); 
        d.write(recordBuffer, RECORD_SIZE); 
    
        d.close(); 
    
    
    } 
    
    void Animals::searchAnimal(fstream &d) 
    { 
        int userRecord = 0; 
        char displayRecord[2]; 
        const int RECORD_SIZE = 40; 
        char fileOutput[RECORD_SIZE]; 
        string displayInfo; 
        char displayName[RECORD_SIZE]; 
        char displayAge[2]; 
        int i,k; 
        int j = 0; 
    
        d.open("animals.txt", ios::out | ios::binary); 
    
        //Getting user input 
        do 
        { 
         cout << "Enter the record of the animal to be diplayed: "; 
         cin >> userRecord; 
        } while (userRecord <= 0 || !isdigit(userRecord)); 
    
    
        //Moving pointer position to searched value 
        d.seekp((userRecord - 1) * RECORD_SIZE); 
        //Gets data from file, stores into displayInfo 
        getline(d, displayInfo); 
    
        d.close(); 
    
        //Entering file data into diplay variables, 
        //value 'i' increments through entire displayInfo array 
    
        for (i = 0; isdigit(displayInfo[i]); i++) 
        { 
         displayRecord[i] = displayInfo[i]; 
        } 
    
        //New sentinel for ONLY string cap including buffer 
        k = i + 30; 
    
        for (i; i < k; i++) 
        { 
         displayName[j] = displayInfo[i]; 
         j++; 
        } 
    
        //Finishes off last of the displayInfo array 
        for (i; i < sizeof(displayInfo); i++) { 
         displayAge[i] = displayInfo[i]; 
        } 
    
        //Prints the data for user 
        cout << "For record number: " << userRecord << "\n"; 
    
        cout << "Animal: "; 
    
        for (int i = 0; i < strlen(displayName); i++) 
        { 
         cout << displayName[i]; 
        } 
        cout << "\n"; 
    
        cout << "Age: "; 
    
        for (int i = 0; i < strlen(displayAge); i++) 
        { 
         cout << displayAge[i]; 
        } 
        cout << "\n"; 
    } 
    
    
    //Mostly copied from above function, displays ALL animals 
    void Animals::displayAnimal(fstream &d) 
    { 
        int userRecord = 0; 
        char displayRecord[2]; 
        const int RECORD_SIZE = 40; 
        char fileOutput[RECORD_SIZE]; 
        string displayInfo; 
        char displayName[RECORD_SIZE]; 
        char displayAge[2]; 
        int numberOfRecords; 
        int i, k; 
        int j = 0; 
        int q = 0; 
    
        d.open("animals.txt", ios::out | ios::in | ios::binary); 
        d.seekg(0, d.end); 
    
        numberOfRecords = d.tellg() % RECORD_SIZE; 
        d.seekg(0, d.beg); 
    
        for (int q; q < numberOfRecords; q++) { 
    
         d.seekp(q * RECORD_SIZE); 
    
         //Gets data from file, stores into displayInfo 
         getline(d, displayInfo); 
    
         //Entering file data into diplay variables, 
         //value 'i' increments through entire displayInfo array 
    
         for (i = 0; isdigit(displayInfo[i]); i++) 
         { 
          displayRecord[i] = displayInfo[i]; 
         } 
    
         //New sentinel for ONLY string cap including buffer 
         k = i + 30; 
    
         for (i; i < k; i++) 
         { 
          displayName[j] = displayInfo[i]; 
          j++; 
         } 
    
         //Finishes off last of the displayInfo array 
         for (i; i < sizeof(displayInfo); i++) { 
          displayAge[i] = displayInfo[i]; 
         } 
    
         //Prints the data for user 
         cout << "For record number: " << userRecord << "\n"; 
    
         cout << "Animal: "; 
    
         for (int i = 0; i < strlen(displayName); i++) 
         { 
          cout << displayName[i]; 
         } 
         cout << "\n"; 
    
         cout << "Age: "; 
    
         for (int i = 0; i < strlen(displayAge); i++) 
         { 
          cout << displayAge[i]; 
         } 
         cout << "\n\n"; 
        } 
        d.close(); 
    } 
    
  • +0

    在標題中,您必須使用限定名稱std :: fstream。 –

    回答

    1

    的頭<iostream><fstream>把他們的符號進std命名空間,所以任何參考在標題中需要這些符號完全合格;例如std::fstream。另外,您可能希望在頭中包含<iosfwd> - 這樣可以最大限度地減少導入頭的編譯器時間成本,假定頭只使用對iostream頭中提及的符號的引用。

    +1

    感謝您的快速回答和提示!擺脫了錯誤 – user8585343

    1

    您應該使用ifstream來讀取文件,使用ofstream來寫入文件。

    而在頭文件中,您應該在4個函數的參數中使用std::ifstream & dstd::ofstream & d

    +0

    感謝您的快速回答和提示!擺脫了錯誤 – user8585343