0

I have an assignment that I am stuck on and I have researched all day and come up empty handed:

I am working with two classes: ClassA must have a unique overloaded assignment and overloaded insertion. ClassB must have a unique overloaded assignment, overloaded extraction, and overloaded insertion.

ClassA has data members: int altitude, char code, string area

ClassB has only an array of classA: classA* data[30]

All of the overloaded operators must be unique in each class. When I overload insertion or extraction for ClassB I get an error that the datamember is private which I assume is because the array I am reading into is of data type ClassA. I am not allowed to replicate friend functions from ClassB into ClassA. Is this even possible?

  class classB;

  class classA
  {
      friend class classB;

      friend ostream & operator<<(ostream&, const classA&);      
      void operator=(const classA&);

      int altitude;
      char code;
      string area;
  };

  class classB
  {
      friend ostream& operator<<(ostream&, const classB&);
      friend istream& operator>>(istream&, classB&);

      public:      
      void operator=(const classB&);      
      void sort();      

      private:
      classA* dataVals[30];      
  };

I am certain my overload functions are written correctly. We were given the basics of overloaded operators and the directions I just provided and that is all.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
DATcat
  • 1
  • 1
  • What do your insertion and extraction operators for class B look like? – 1201ProgramAlarm Nov 01 '15 at 06:59
  • Parsing this a bit more thoroughly this time. Some other function is going to use B's >> operator. B's >> is going to `new` a batch of As and stuff them into `dataVals`. Why not skip the friend relationship and give A a constructor that initializes A with values read in by B? – user4581301 Nov 01 '15 at 07:07
  • If you forget about `ClassB` for a moment, how do you create variables of `ClassA` in general? All its members are private and you haven't declared any constructors. Start with the test case `int main() { ClassA a(1, 'x', "Hello"); }` and make that compile first. Then continue with `ClassB`. – Bo Persson Nov 01 '15 at 08:24
  • Friendship is not associative, so `istream& operator>>(istream&, classB&);` is not a friend of `classA`. – Jarod42 Nov 01 '15 at 08:24
  • marking `classB` as friend can access private members of `classA` but insertion and extraction operator from `classB` cannot access `classA` private members in `classA` because those functions not mark as friend in `classA` my method is i would declared each of the two class an insertion and extraction operator – Kryssel Tillada Nov 01 '15 at 09:31
  • Can you post the code that doesn't compile? – Christian Hackl Nov 01 '15 at 11:10

0 Answers0