Good evening! I am in an introductory C++ programing class working on a project that I am having some troubles with. I am having trouble overloading the insertion/extraction operators. I can copy and paste code all day long, but that does not help me learn. I am sure that I have made an error in my syntax. This is my greatest weakness in my classes. And as everyone knows, that is a deadly problem to have in programming. I have posted part of my ADT for review and I am leaving it as specific to my question as possible. Please let me know if you see where I have gone astray. Thank you.
I am using Putty SSH on a Linux system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//In my header file (Team.h)
#ifndef TEAM_H
#define TEAM_H
#include <iostream>
#include "Player.h"
class Team
{
//Left body of class out for this post.
};
std::istream& operator >> (std::istream&, Team&);
std::ostream& operator << (std::ostream&, const Team&);
#endif
As you can see, my overloading functions are non-member non-friend functions of the class (as per the assignment).
//In my main file (main.cc)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Team.h"
usingnamespace std;
void Test_1 (Player);
void Test_2 (Player);
int main()
{
Player Test;
//Test_1 (Test); //Tests Default constructor, get functions,
//and set functions.
Test_2 (Test); //Tests input.
return (0);
}
void Test_1 (Player Test)
{
int i;
for (i = 1; i < 6; i++)
{
int I = Test.get_id();
int H = Test.get_hits();
int W = Test.get_walks();
int O = Test.get_outs();
double A = Test.get_average();
cout << "\n";
cout << I << "\t" << H << "\t" << W << "\t" << O << "\t" << A
<< string(2, '\n');
Test.set_id(i);
Test.set_hits(i);
Test.set_walks(i);
Test.set_outs(i);
Test.set_average(i);
}
}
void Test_2 (Player Test)
{
ifstream fin;
int I = Test.get_id();
int H = Test.get_hits();
int W = Test.get_walks();
int O = Test.get_outs();
double A = Test.get_average();
fin.open("bat.dat");
if (fin.fail())
{
cout << string(15, '\n');
cout << "************************ CAUTION ************************\n"
<< "***************** The file failed to open ***************\n"
<< "*********************************************************\n";
cout << string(15, '\n');
exit(1);
}
fin >> Test;
Test.input(std::istream&);
fin.close();
cout << string(2, '\n');
cout << I << "\t" << H << "\t" << W << "\t" << O << "\t" << A
<< string(2, '\n');
}