Problem with operator<<

This is my code

project.h
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
#ifndef PROJECT_H
#define PROJECT_H

#include <iostream>
#include <string>
#include "staff.h"

class Division;


class Project {
public:

  typedef const Staff* iterator;

  Project(int maxStaff = DEFAULT_MAXSTAFF);


  // Attributes of Project

  // Project title
  std::string getTitle() const     {return theTitle;}
  void setTitle(std::string title) {theTitle = title;}

  // To which division does this project belong? Each project belongs
  // to one division, but a division may have many projects.
  Division* getDivision() const    {return theDivision;}
  void setDivision(Division* publ)  {theDivision = publ;}

  // The project ID is a unique identifier for the project.
  std::string getProjectID() const     {return theProjectID;}
  void setProjectID(std::string projectID)  {theProjectID = projectID;}

  // Budget codes indicate to whome this project is charged.
  std::string getBudgetCode() const    {return theBudgetCode;}
  void setBudgetCode(std::string projectID) {theBudgetCode = projectID;}


  // Operations on Project

  // Add a staff member to the project. Staff should be kept in alphabetic
  // order by name.
  void addStaff(const Staff staffMember);
  int numberOfStaff() const;

  // Access to list of staff assigned to this project
  iterator begin() const;
  iterator end() const;


  bool operator< (const Project& bk) const;
  //bool operator<< (const Project& p) const;
//  bool operator<< (const Project& i) const;


private:
  std::string theTitle;
  std::string theProjectID;
  std::string theBudgetCode;
  Division* theDivision;

  int numStaff;
  int theMaxStaff;
  Staff* staff;  // array of Staff

  static const int DEFAULT_MAXSTAFF = 12;
};


#endif 



project.cpp
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
#include "division.h"
#include "project.h"

#include <algorithm>
#include <cassert>
#include <cstdlib>

using namespace std;

Project::Project(int maxStaff)
  : theProjectID(""), theBudgetCode(""), theDivision(0), numStaff(0),
    theMaxStaff(maxStaff)
{
  staff = new Staff[maxStaff];
}

Project::iterator Project::begin() const
{
  return staff;
}


Project::iterator Project::end() const
{
  return staff + numStaff;
}


/**** insert your operations for Projects here  ***/

std::ostream& operator<< (std::ostream& out, Division::const_iterator p)
{
	  out << p.getTitle() << "\t"
	      << p.getDivision() << "\t"
	      << p.getProjectID() << "\t"
	      << p.getBudgetCode() << "\t"
	      << "\n";
	  return out;
}


void Project::addStaff(Staff staffMember)
{
  staff[numStaff+1]=staffMember;
  numStaff++;
}



And these are the errors I am getting.


C:\Users\Jacob\Documents\College\Spring 2012\CS330\Assignment 1\project.cpp||In function `std::ostream& operator<<(std::ostream&, std::_List_const_iterator<Project>)':|
C:\Users\Jacob\Documents\College\Spring 2012\CS330\Assignment 1\project.cpp|33|error: 'struct std::_List_const_iterator<Project>' has no member named 'getTitle'|
C:\Users\Jacob\Documents\College\Spring 2012\CS330\Assignment 1\project.cpp|34|error: 'struct std::_List_const_iterator<Project>' has no member named 'getDivision'|
C:\Users\Jacob\Documents\College\Spring 2012\CS330\Assignment 1\project.cpp|35|error: 'struct std::_List_const_iterator<Project>' has no member named 'getProjectID'|
C:\Users\Jacob\Documents\College\Spring 2012\CS330\Assignment 1\project.cpp|36|error: 'struct std::_List_const_iterator<Project>' has no member named 'getBudgetCode'|
||=== Build finished: 4 errors, 0 warnings ===|




All in project.cpp, in the operator<< area. I do not exactly understand what I need to do to fix these problems and would appreciate any help.
What's Division? Any way, Division::const_iterator is definitely not a synonym for Project, so why are you using Project's methods on it? Solution depends on contents of Division. Changing p.getTitle() to (*p).getTitle() might work..
division.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "division.h"
#include "project.h"
#include "split.h"

using namespace std;


Division::Division() {}


void Division::addProject(Project b)
{
  activeProjects.push_back (b);
}


int Division::numberOfProjects() const
{
  return activeProjects.size();
}



istream& operator>> (istream& in, Division& p)
{
  string line;
  if (in)
    {
      getline(in, line);
    }

  string fields[4];
  int fieldCount = split(line, "\t", fields, 4);

  p = Division();
  p.putName (fields[0]);
  p.putRoom (fields[1]);
  p.putBuilding (fields[2]);
  p.putDivisionCode (fields[3]);
}


std::ostream& operator<< (std::ostream& out, const Division& div)
{
  out << div.getName() << "\t"
      << div.getDivisionCode() << "\t"
      << div.getBuilding() << ":" << div.getRoom()
      << "\n";
  if (div.numberOfProjects() > 0)
    {
      int i = 0;
      for (Division::const_iterator p = div.begin();
	   p != div.end(); ++p)
	{
	  ++i;
	  out << i << "/" << div.numberOfProjects() << ": ";
	  out << *p << "\n";
	}
    }
  else
    out << "(no projects)\n";
  return out;
}






Division::iterator Division::begin()
{
  return activeProjects.begin();
}

Division::const_iterator Division::begin() const
{
  return activeProjects.begin();
}

Division::iterator Division::end()
{
  return activeProjects.end();
}

Division::const_iterator Division::end() const
{
  return activeProjects.end();
}

bool Division::operator< (const Division& right) const
{
  return theName < right.theName
    || (theName == right.theName &&
	theDivisionCode < right.theDivisionCode);
}



division.h
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
#ifndef DIVISION_H
#define DIVISION_H

#include <iostream>
#include <string>
#include <list>

#include "project.h"


class Division {
public:
  Division();

  std::string getName() const {return theName;}
  void putName(std::string name) {theName = name;}

  std::string getRoom() const {return theRoom;}
  void putRoom(std::string room) {theRoom = room;}

  std::string getBuilding() const {return theBuilding;}
  void putBuilding(std::string building) {theBuilding = building;}

  std::string getDivisionCode() const {return theDivisionCode;}
  void putDivisionCode(std::string divisionCode) {theDivisionCode = divisionCode;}

  void addProject(Project);
  int numberOfProjects() const;

  typedef std::list<Project> ProjectSequence;
  typedef ProjectSequence::iterator iterator;
  typedef ProjectSequence::const_iterator const_iterator;

  iterator begin();
  iterator end();

  const_iterator begin() const;
  const_iterator end() const;

  bool operator< (const Division& right) const;

private:
  std::string theName;
  std::string theRoom;
  std::string theBuilding;
  std::string theDivisionCode;

  std::list<Project> activeProjects;
};


std::istream& operator>> (std::istream& in, Division& div);
std::ostream& operator<< (std::ostream& out, const Division& div);


#endif 



I just tried changing p to a p* but it didn't fix the problem
Try changing
std::ostream& operator<< (std::ostream& out, Division::const_iterator p)
to
std::ostream& operator<< (std::ostream& out, Division p)

Otherwise, how about:
std::ostream& operator<< (std::ostream& out, Division::const_iterator *p)
and then
p->getTitle()
The first method you suggested did not fix my problem and the second one led me to one error in division.cpp, which I should not have to alter or change at all. Here is the error received with the second method you suggested:

division.cpp

Line 57 error: no match for 'operator<<' in 'out << (&p)->std::_List_const_iterator_<Tp>::operator* [with_Tp=Project]();



EDIT:
And the first method you suggested gave me these errors


C:\Users\Jacob\Documents\College\Spring 2012\CS330\Assignment 1\project.cpp||In function `std::ostream& operator<<(std::ostream&, Division)':|
|33|error: 'class Division' has no member named 'getTitle'|
|34|error: 'class Division' has no member named 'getDivision'|
|35|error: 'class Division' has no member named 'getProjectID'|
|36|error: 'class Division' has no member named 'getBudgetCode'|
||=== Build finished: 4 errors, 0 warnings ===|
Last edited on
So that iterator is std::list::const_iterator. Both (*p).getTitle() and p->getTitle() should work just fine. Keep the << declaration as in the original post. Post the errors if any.
Changing to use the -> operator should fix your problem in project... iterators 'point'

1
2
3
4
5
6
7
8
9
std::ostream& operator<< (std::ostream& out, Division::const_iterator p)
{
	  out << p.getTitle() << "\t" //change to p->getTitle()
	      << p.getDivision() << "\t"
	      << p.getProjectID() << "\t"
	      << p.getBudgetCode() << "\t"
	      << "\n";
	  return out;
}


If this code was given to you, this is not the 'model' approach at encapsulation, returning iterators to your internal data is pretty much the same thing as returning the internal data publicly. (This is just a side note, but try not doing this)

Using these typedefs inside of project makes project confusing.
1
2
3
typedef std::list<Project> ProjectSequence;
typedef ProjectSequence::iterator iterator;
typedef ProjectSequence::const_iterator const_iterator;

When I change it to
1
2
3
4
5
6
7
8
9
std::ostream& operator<< (std::ostream& out, Division::const_iterator p)
{
	  out << p->getTitle() << "\t" //change to p->getTitle()
	      << p->getDivision() << "\t"
	      << p->getProjectID() << "\t"
	      << p->getBudgetCode() << "\t"
	      << "\n";
	  return out;
}


I receive these errors and warnings:


In function `std::istream& operator>>(std::istream&, Division&)':|
division.cpp|33|warning: unused variable 'fieldCount'|
division.cpp|40|warning: control reaches end of non-void function|
division.cpp||In function `std::ostream& operator<<(std::ostream&, const Division&)':|
division.cpp|57|error: no match for 'operator<<' in 'out << (&p)->std::_List_const_iterator<_Tp>::operator* [with _Tp = Project]()'|
|63|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
|74|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
|86|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]|
|121|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]|
|155|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
|98|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]|
|178|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]|
|189|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
|193|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
|204|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
|179|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]|
|214|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
|238|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]|
|219|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]|
|261|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]|
|284|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]|
C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\ostream.tcc|307|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]|
|29|note: std::ostream& operator<<(std::ostream&, const Staff&)|
division.cpp|44|note: std::ostream& operator<<(std::ostream&, const Division&)|
|504|note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]|
|499|note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]|
|612|note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]|
C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\ostream.tcc|567|note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]|
|465|note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]|
|460|note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]|
|505|note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]|
|449|note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]|
||=== Build finished: 1 errors, 2 warnings ===|

Topic archived. No new replies allowed.