Overloading insertion/extraction operators

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//In my main file (main.cc)

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Team.h"
using namespace 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');
}


1
2
3
4
5
6
7
8
9
10
11
12
13
//In my implimentation file (Team.cc).

istream& operator >> (istream& filein, Team& in)
{
  in.input(filein);
  return(filein);
}

ostream& operator << (ostream& fileout, const Team& out)
{
   out.output(fileout);
   return (fileout);
}


If you see where I am wrong, please let me know! Thank you all!

Kit.
Last edited on
Topic archived. No new replies allowed.