我在高级面向对象编程课程中真的需要帮助!以下是该计划的运作方式;我们用C ++编写代码,我们要创建一个创建气象站的程序。如果按'1',则输入温度,风速和风向;如果你按'2',如果你会给你当前的天气读数;如果你按'3'它会给你天气历史;如果按'4'则退出。此外,它从文本文件中读取输出并将它们输出到控制台...我已经完全正常工作了。我遇到的问题是我们要重载流(<<>>)运算符,所以我们不再需要调用print()函数。以前,我打印打印功能打印当前的天气读数以及温度历史记录。我已经排除了我的文件wind.h和wind.cpp,因为我还没有开始尝试在那里实现重载;加上我想我在一篇文章中发布了足够的代码。此外,我应该提一下,如果用户想要使用piglatin.txt中的英语或染色剂,如果他们想要使用piglatin,该程序还会读取english.txt的输出。
我的问题,基本上,我是如何正确实现运算符流的重载?
Source.cpp:
#include <iostream>
#include <string>
#include "temp.h"
#include "Wind.h"
#include "Language.h"
#include <fstream>
#define SIZE 10
using namespace std;
int main()
{
Temperature temp1;
win wind1;
//Temperature p;
string windDir[SIZE];
int windSpeed[SIZE];
int temp[SIZE];
//Tem p1(int temp);
int index=0;
int reply;
int reply2;
int lang;
int count=0;
string STRING1, STRING2, STRING3, STRING4, STRING5, STRING6;
fstream myEngFile;
fstream myPigFile;
ofstream out;
//Language Menu
cout<<"**** Welcome to The Michael Weather Station! \n";
cout<<"**** Elcomeway otay Ethay Ichaelmay Eatherway Ationstay! \n";
cout<<"Please enter 1 for english ";
cout<<"orway 2 orfay iglatinpay \n";
cin>>lang;
cin.clear();
switch(lang){
//English Main Menu
case 1:
//for(int count=0; count<21; count++)
for(int index=0; index<SIZE; index++){
bool menu = true;
while (menu != false){
myEngFile.open ("English.txt");
getline(myEngFile, STRING1);
cout<<STRING1<<"\n";
getline(myEngFile, STRING2);
cout<<STRING2<<"\n";
getline(myEngFile, STRING3);
cout<<STRING3<<"\n";
getline(myEngFile, STRING4);
cout<<STRING4<<"\n";
getline(myEngFile, STRING5);
cout<<STRING5<<"\n";
cin>>reply;
cin.clear();
myEngFile.close();
switch(reply){
//Gets data in English
case 1:
temp[count]=temp1.getTemp();
windSpeed[count] =wind1.getSpeed();
windDir[count] = wind1.getDirection();
cout<<" \n";
count++;
break;
//Prints data in English
case 2:
cout<<"--------------------------------\n";
//temp1.printTemp(temp[count-1]);
Temperature p = temp1.getTemp();
//cout<<temp[count-1];
cout<<p;
wind1.printWindMeasurement(windSpeed[count-1], windDir[count-1]);
cout<<" \n";
cout<<" \n";
break;
//Prints History in English
case 3:
cout<<"---------------------------------- \n";
//for(int index=SIZE%count; index<SIZE; index++){
for(int i=0; i<count; i++){
//temp1.printTemp(temp[i]);
cout<<temp[i];
wind1.printWindMeasurement(windSpeed[i], windDir[i]);
cout<<"\n";
}
break;
//Exits...In English?
case 4:
cout<<"Exiting............. \n";
menu= false;
break;
//Cheacks to see that only 1,2,3, or 4 is entered
default:
cout<<"Please enter 1,2,3, or 4 only!!! \n";
cout<<" \n";
} //Switch(reply) in english
} //While loop in english
return 0;
} //For loop in english
break;
//PigLatin Main Menu
case 2:
for(int index=0; index<SIZE; index++){
bool menu = true;
while (menu != false){
myPigFile.open ("PigLatin.txt");
getline(myPigFile, STRING1);
cout<<STRING1<<"\n";
getline(myPigFile, STRING2);
cout<<STRING2<<"\n";
getline(myPigFile, STRING3);
cout<<STRING3<<"\n";
getline(myPigFile, STRING4);
cout<<STRING4<<"\n";
getline(myPigFile, STRING5);
cout<<STRING5<<"\n";
getline(myPigFile, STRING6);
cout<<STRING6<<"\n";
cin>>reply2;
cin.clear();
myPigFile.close();
switch(reply2){
//Gets data in PigLatin
case 1:
temp[count]=temp1.getTemp2();
windSpeed[count] =wind1.getSpeed2();
windDir[count] = wind1.getDirection2();
cout<<" \n";
count++;
break;
//Prints data in PigLatin
case 2:
cout<<"----------------------------------\n";
//temp1.printTemp2(temp[count-1]);
cout<<temp[count-1];
wind1.printWindMeasurement2(windSpeed[count-1], windDir[count-1]);
cout<<" \n";
cout<<" \n";
break;
//Prints History in PigLatin
case 3:
cout<<"------------------------------------ \n";
//for(int index=SIZE%count; index<SIZE; index++){
for(int i=0; i<count; i++){
//temp1.printTemp2(temp[i]);
cout<<temp[i];
wind1.printWindMeasurement2(windSpeed[i], windDir[i]);
cout<<"\n";
}
break;
//Exits...in PigLatin?
case 4:
cout<<"exitingway............. \n";
menu= false;
break;
//Checks to see only 1,2,3, or 4 is entered
default:
cout<<"Easeplay enterway 1,2,3, orway 4 onlyway!!! \n";
cout<<" \n";
} //Switch (Reply2) in piglatin
} //While loop for PigLatin Main Menu
return 0;
} //For loop for PigLatin Main Menu
break;
} //switch(Lang)
} //Main
Temp.cpp:
#include "temp.h"
#include "Language.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Lang La;
fstream myEngFile5;
string tempLine5;
string tempLine6;
//string tempLine1; // from language file
/**** Keep the getTemp() method, but get rid of the print method
and overload the operators there*******/
ostream &operator<<( ostream &out, const Temperature &p) //&f
{
//int far=0;
fstream myEngFile1;
string tempLine1;
myEngFile1.open ("English.txt");
myEngFile1.seekg(194);
getline(myEngFile1, tempLine1);
//La.getTempLang();
out<<p.tempLine1<<endl; // What is the Temperature?
// cin>>p; //f
return out;
//myEngFile1.close();
}
/*int Temperature::getTemp()
{
int value=0;
fstream myEngFile1;
string tempLine1;
myEngFile1.open ("English.txt");
myEngFile1.seekg(194);
getline(myEngFile1, tempLine1);
cout<<tempLine1<<"\n"; // What is the Temperature?
cin>>value;
return value;
myEngFile1.close();
}*/
int Temperature::getTemp2()
{
int value=0;
fstream myPigFile1;
string tempLine1;
myPigFile1.open ("PigLatin.txt");
myPigFile1.seekg(254);
getline(myPigFile1, tempLine1);
cout<<tempLine1<<"\n"; //What is the Temperature? in piglatin
cin>>value;
return value;
myPigFile1.close();
}
/*void Tem::printTemp(int temper)
{
fstream myEngFile5;
string tempLine5;
string tempLine6;
myEngFile5.open("English.txt");
myEngFile5.seekg(335);
getline(myEngFile5, tempLine5);
cout<<tempLine5<<temper; //The Temperature is
myEngFile5.seekg(356);
getline(myEngFile5, tempLine6);
cout<<tempLine6<<"\n"; //F
}*/
ostream &operator<<( ostream &out, const Temperature p)
{
out<<tempLine5<<p.value; //The Temperature is __
out<<tempLine6<<"\n"; //F
return out;
}
istream &operator>>( istream &in, Temperature &p)
{
//in>>p.value;
myEngFile5.open("English.txt");
myEngFile5.seekg(335);
getline(myEngFile5, tempLine5);
in>>tempLine5;
myEngFile5.seekg(356);
getline(myEngFile5, tempLine6);
in>>tempLine6;
return in;
}
/*void Tem::printTemp2(int temper)
{
fstream myPigFile4;
string tempLine5;
string tempLine6;
myPigFile4.open("PigLatin.txt");
myPigFile4.seekg(445);
getline(myPigFile4, tempLine5);
cout<<tempLine5<<temper; //The temperature is __ (in piglatin)
myPigFile4.seekg(473);
getline(myPigFile4, tempLine6);
cout<<tempLine6<<"\n"; //fay
}*/
Temp.h:
#ifndef TEMP
#define TEMP
#include <fstream>
#include <string>
using namespace std;
//int index=0;
class Temperature
{
int value;
//char unit;
public:
string tempLine1;
int getTemp();
int getTemp2();
Temperature()
{
value=0;
//unit='F';
}
Temperature(int v/*, char u*/)
{
value=v;
//unit=u;
}
friend ostream &operator<<(ostream &out, const Temperature &p); // needs 'friend' i think
friend istream &operator>>(istream &in, Temperature &p);
//friend ostream operator<<(ostream out, const Temperature p);
//friend istream operator>>(istream in, Temperature &p);
//void printTemp(int temper);
//void printTemp2(int temper);
};
#endif
答案 0 :(得分:1)
因此。这是第一点。他的评论完全正确。您的问题中存在吨不相关的代码。
所以,首先,让我们删除它。并将问题的范围缩小到有人可能会阅读的大小。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Temperature
{
private:
int value;
public:
Temperature(): value(0) {
}
explicit Temperature(int v): value(v) {
}
friend ostream &operator<<(ostream &out, const Temperature &p);
friend istream &operator>>(istream &in, Temperature &p);
};
ostream &operator<<(ostream &out, const Temperature &p)
{
fstream myEngFile1;
string tempLine1;
myEngFile1.open ("English.txt");
myEngFile1.seekg(194);
getline(myEngFile1, tempLine1);
out << p.tempLine1 << endl; // What is the Temperature?
out << tempLine5 << p.value; //The Temperature is __
out << tempLine6 << "\n"; //F
return out;
}
istream &operator>>( istream &in, Temperature &p)
{
//in>>p.value;
myEngFile5.open("English.txt");
myEngFile5.seekg(335);
getline(myEngFile5, tempLine5);
in>>tempLine5;
myEngFile5.seekg(356);
getline(myEngFile5, tempLine6);
in>>tempLine6;
return in;
}
int main() {
Temperature x;
std::cin >> x;
std::cout << x << std::endl;
}
这或多或少是您的代码,但删除了您的注释之类的内容(为什么我关心您的温度等级将来会使用F / C比较)。另外,让我们摆脱你所有的菜单结构。我也不在乎,你声称它工作正常。
所以,现在我们需要查看合理数量的代码,所以让我们展示一个合理的ostream和istream重载实现。
#include <iostream>
#include <string>
#include <fstream>
class Temperature
{
private:
int value;
public:
Temperature(): value(0) {
}
explicit Temperature(int v): value(v) {
}
friend std::ostream &operator<<(std::ostream &out, const Temperature &p);
friend std::istream &operator>>(std::istream &in, Temperature &p);
};
std::ostream &operator<<(std::ostream &out, const Temperature & p)
{
out << "Temperature(" << p.value << ")";
return out;
}
std::istream &operator>>( std::istream &in, Temperature &p)
{
in >> p.value;
return in;
}
int main() {
Temperature x;
std::cin >> x;
std::cout << x << std::endl;
}
如果我们执行此....
[2:48pm][wlynch@watermelon /tmp] ./foo
123
Temperature(123)
[2:48pm][wlynch@watermelon /tmp] ./foo
-9000
Temperature(-9000)