C++中Contestants类readFile单参数写入Player对象数组问题
如何在成员函数中无需额外参数追加对象到当前实例的数组?
我在实现Contestants类的readFile成员函数时遇到了问题——这个函数需要从文件读取内容,逐行解析成Player对象后追加到当前实例的数组里。之前我是通过把testList2对象以引用形式传入函数来实现的,但现在要求主文件不能修改,且readFile只能接收一个文件名参数。
测试代码片段
Contestants testList2; std::cout << "-----BEGIN FILE I/O TEST -----------\n\n"; std::cout << "Test 1 - error opening file\n"; std::cout << "\tEXPECTING: Could not open file.\n"; std::cout << "\tACTUAL: "; testList2.readFile("testFil"); std::cout << "\nTest 2 - append to object from file\n"; testList2.readFile("testFile2"); // 此处不能添加第二个参数 std::cout << "\nPrinting testList2 contents\n"; for(int i = 0; i < testList2.getSize(); i++) { std::cout << "Player " << i + 1 << "\n"; std::cout << testList2.at(i) << std::endl; }
readFile成员函数实现
bool Contestants::readFile(std::string fileName) { // Create object and open file Player tmpPlayer; std::ifstream inFile(fileName); // Check that file is open. if(!inFile) { std::cout << "Could not open file." << std::endl; return false; } std::cout << "File opened.\n" << std::endl; // Read the file line by line std::string line; std::string tmpName; std::string tmpScore; while(std::getline(inFile, line)) { //std::cout << line << std::endl; // This line is for testing purposes // Create a string stream read from input file. std::stringstream ss(line); // Store contents of the file into two seperate value delimited by a comma. if(std::getline(ss, tmpName, ',')) { if(std::getline(ss, tmpScore)) { tmpPlayer = Player(tmpName, stoi(tmpScore)); testList2.append(tmpPlayer); // 如何在无需第二个参数的情况下追加到当前Contestants对象? } } } // Check for errors or end of file if (inFile.bad()) { std::cout << "Error reading file." << std::endl; return false; } else if (inFile.eof()) { std::cout << "\nEnd of file reached." << std::endl; } // Close the file. inFile.close(); std::cout << "\nFile closed." << std::endl; return true; }
Contestants类定义
class Contestants { public: // Static constant size maximum for array size. static const int MAX_SIZE = 3; // Default constructor Contestants(); // Capacity int getSize() const; // Returns the number of objects held within Contestants int getMaxSize() const; // Returns the value of MAX_SIZE bool isEmpty() const; // Checks for empty object array. // Accessors int contains(const Player& player) const; // Determines if Contestants contains specified Player object const Player& at(int index) const; // Returns player object found at index param const Player& operator[](int index) const; // TODO // Modifiers void clearAll(); // Resets object array to 0. bool append(const Player& player); // Adds player objects and increases size. bool removePlayer(const Player& player); // Finds player object and calls removeByIndex() bool removeByIndex(int index); // removes specified player object // Other (file IO) bool readFile(std::string fileName); // TODO bool writeFile(std::string fileName); // TODO private: Player data[MAX_SIZE]; int size; };
我试过多种指针方法但都报错,希望得到方向指引而非直接答案。
内容的提问来源于stack exchange,提问作者MadTragic
相关产品推荐
相关产品推荐

