C++ ADT&x27;s将对象保存到类中
请帮帮我,我卡住了 请看下面我的代码 我正在尝试为类创建一个ADT文件,因此需要接口文件、植入文件和主文件。我的问题在主文件中 我想创建一个类数组。例如,有9个电话呼叫,因此我想在PhoneCalls类中创建9个对象 每个对象都需要有自己的变量,即:名称、长度和速率 这些对象是从导入文件MyCalls.dat派生的 首先,我不知道我是否正确创建了CallsMade数组 其次,当我尝试使用我在实现和接口文件中创建的>>操作符将值存储到CallsMade时,我的程序只是停止运行 我不知道如何使用friend>>操作符将主文件中的tempName、tempLenght、tempRate的值保存到header类中 **C++ ADT&x27;s将对象保存到类中,c++,C++,请帮帮我,我卡住了 请看下面我的代码 我正在尝试为类创建一个ADT文件,因此需要接口文件、植入文件和主文件。我的问题在主文件中 我想创建一个类数组。例如,有9个电话呼叫,因此我想在PhoneCalls类中创建9个对象 每个对象都需要有自己的变量,即:名称、长度和速率 这些对象是从导入文件MyCalls.dat派生的 首先,我不知道我是否正确创建了CallsMade数组 其次,当我尝试使用我在实现和接口文件中创建的>>操作符将值存储到CallsMade时,我的程序只是停止运行 我不知道如何使用fr
- 接口文件
编辑-将>>操作符更新为只安装3个变量,而不是4个。因此这里有很多东西需要解包,但我认为基本上您对
操作符>
的工作原理感到困惑
您的代码将文件读入一个字符串,将该字符串解析为不同的变量,然后您将无法确定如何将运算符>>与您拥有的变量一起使用。但这是错误的方法,实际上您正在做很多operator>>将为您做的工作。正确的代码要简单得多
您可以看到操作符>>
直接作用于文件,并将数据直接放入对象中。您不需要单独读取和解析文件。下面是代码的外观
PhoneCall callsMade[numOfCalls];
ifstream inStreamMyCalls;
inStreamMyCalls.open("MyCalls.dat");
for (int i = 0; i < numOfCalls; i++)
{
inStreamMyCalls >> callsMade[i];
}
inStreamMyCalls.close();
phonecallsmade[numOfCalls];
ifstream-instreamy调用;
inStreamMyCalls.open(“MyCalls.dat”);
for(int i=0;i>CallsMode[i];
}
instreamyCalls.close();
就这样,你可以摆脱数据数组,所有的临时。。。变量
现在,在完成上述工作之后,还有一些问题需要处理
最重要的是,运算符>>读取三个值,但运算符>应该能够读取什么运算符欢迎使用StackOverflow。如果您希望其他人努力阅读并回答您的问题,您应该努力将代码减少到与您的问题相关的最小示例/相关部分。CallsMode是一个数组,您必须为数组的每个元素执行输入/输出您可能想考虑如何表达您的问题
我不知道如何使用friend>>操作符将tempName、tempLenght、tempRate的值从主文件保存到头类中。
对我来说没有任何意义。@YuriFeldman谢谢您的建议。将立即更新。@john谢谢,我将立即更新我的问题。非常感谢!我需要学习>>和
//Implementation file Question_5_pCall.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "Question_5_pCall.h"
using namespace std;
//Set default constructors values to empty, 0 and 0.
PhoneCall::PhoneCall() : number(""), length(0), rate(0), charged(0)
{
//Body intentionally empty
}
//Set default destructor
PhoneCall::~PhoneCall()
{
//Body intentionally empty
}
//Use overload stream operators
istream& operator >> (istream& ins, PhoneCall& callData)
{
ins >> callData.number;
ins >> callData.length;
ins >> callData.rate;
return ins;
}
ostream& operator <<(ostream& outs, const PhoneCall& callData)
{
outs << callData.number << endl;
outs << callData.length << endl;
outs << callData.rate << endl;
outs << callData.charged << endl;
return outs;
}
//Main file Question_5.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include "Question_5_pCall.h"
using namespace std;
void getTotalCallsMade (int & numOfCalls)
{//First lets see how many calls where made.
//Import the file
ifstream inStreamMyCalls;
inStreamMyCalls.open("MyCalls.dat");
if (inStreamMyCalls.fail()) //Fail safe to check if file is run
{
cout << "Input file opening failed." << endl;
exit(1);
}
string tempInfo;
while (!inStreamMyCalls.eof())
{
getline(inStreamMyCalls, tempInfo);
numOfCalls++;
}
inStreamMyCalls.close();
}
void getReadMyCalls (string data[], int numOfCalls)
{//Import the data and save on each line
ifstream inStreamMyCalls;
inStreamMyCalls.open("MyCalls.dat");
if (inStreamMyCalls.fail()) //Fail safe to check if file is run
{
cout << "Input file opening failed." << endl;
exit(1);
}
string tempInfo;
int k=0;
while (!inStreamMyCalls.eof())
{
getline(inStreamMyCalls, tempInfo);
data[k] = tempInfo;
//cout << data[k] <<endl;
k++;
}
inStreamMyCalls.close();
}
void getInfo (string data, string & tempName, int & tempLength, float & tempRate)
{//Devide the data into Name, Length and rate for each call
//Convert length to int and rate to float
//Import the file
int firstSpace, secondSpace, secondStart, thirdStart, totalLength;
firstSpace = data.find(" ");
secondSpace = data.find(" ", (firstSpace+1));
totalLength = data.length();
secondStart = firstSpace+1;
thirdStart = secondSpace+1;
string tempLe = "";
string tempRa = "";
for (int i = 0; i < firstSpace; i++)
tempName = tempName + data[i];
for (int i = secondStart; i < secondSpace; i++)
{
tempLe = tempLe + data[i];
}
for (int i = thirdStart; i < totalLength; i++)
tempRa = tempRa + data[i];
tempLength = atoi(tempLe.c_str());
tempRate = atof(tempRa.c_str());
}
//THIS IS THE PROBLEM AREA
void storeInfo (int i, string tempName, int tempLenght, float tempRate)
{//Store the data into the class for each call made
cout << tempName << " " << tempLenght << " " << tempRate << endl; //Double check to see if info is correct
cin >> callsMade[i];
}
//PROBLEM AREA
int main()
{
string inputNumber;
int numOfCalls = 0;
getTotalCallsMade(numOfCalls);
string data[numOfCalls];
PhoneCall callsMade[numOfCalls];
getReadMyCalls(data, numOfCalls);
string tempName;
int tempLength;
float tempRate;
string tempData;
for (int i = 0; i < numOfCalls; i++)
{
tempData = data[i];
getInfo(tempData, tempName, tempLength, tempRate);
//PROBLEM AREA
storeInfo(i, tempName, tempLength, tempRate);
//PROBLEM AREA
tempData = "";
tempName = "";
tempLength = 0;
tempRate = 0.0;
}
return 0;
}
0123452347 12 3.50
0337698210 9 3.15
0214672341 2 1.75
0337698210 15 3.15
0442389132 8 1.75
0232189726 5 3.50
0124395623 6 3.50
0337698210 2 3.15
0337698210 5 3.15
PhoneCall callsMade[numOfCalls];
ifstream inStreamMyCalls;
inStreamMyCalls.open("MyCalls.dat");
for (int i = 0; i < numOfCalls; i++)
{
inStreamMyCalls >> callsMade[i];
}
inStreamMyCalls.close();