我遇到了一个障碍,成员函数应该从文件中读取内容,并将内容逐行添加到Player对象数组中。最初我通过引用将testList2对象传递到成员函数中,代码工作正常,但是我被告知主文件不能被修改,并且readFile函数只能带一个参数。下面是来自测试文件的代码片段,以及前面提到的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"); // Function call here. Can not add second parameter.
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;
}
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); // HOW TO APPEND TO CONTESTANTS OBJECT WITHOUT A SECOND FUNCTION PARAMETER?
}
}
}
// 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;
}
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;
};
我尝试过各种指针方法,但都抛出错误。
1条答案
按热度按时间sy5wg1nm1#
我想弄清楚这一点,所以如果有人有一些一般性的建议给我指出正确的方向而不给出答案...
我会尽量不给予出答案:)
readFile()
函数是class Contestants
上的成员,因此不必“将testList2对象传递到成员函数中”