question about method

This is a discussion on question about method within the C++ Programming forums, part of the General Programming Boards category; I have the following class that I can't compile in g++. It comes from the book C++ Primer Plus Code: ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455

    question about method

    I have the following class that I can't compile in g++. It comes from the book C++ Primer Plus
    Code:
    #ifndef STUDENT_H_
    #define STUDENT_H_
    
    #include <iostream>
    #include <valarray>
    #include <string>
    
    class Student: private std::string, private std::valarray<double>
    {
    private:
      typedef std::valarray<double> ArrayDb;
      std::ostream & arr_out(std::ostream & os) const;
      
    public:
      Student()
        : std::string("Null Student"),ArrayDb() {};
      Student( const std::string & s)
        : std::string(s), ArrayDb() {};
      Student( int n)
        : std::string(), ArrayDb(n) {};
      Student(const std::string & s, int n)
        : std::string(s), ArrayDb(n) {};
      Student ( const std:: string & s, const ArrayDb & a)
        : std::string(s), ArrayDb(a) {};
      Student( const std:: string & s, const double * pd, int n)
        : std::string(s), ArrayDb(pd,n) {};
      ~Student() {};
      
      double average() const;
      double & operator[](int i);
      double operator[](int i) const;
      const std::string & Name() const;
      
      // Friends
      friend std::istream & operator>>(std::istream & is, const Student & stu);
      friend std::istream & getline(std::istream & is, const Student & stu);
      friend std::ostream & operator<<(std::ostream & os, const Student & stu);
    };
    
    #endif
    Code:
    #include "student.h"
    using std::ostream;
    using std::endl;
    using std::istream;
    using std::string;
    
    // public mehods
    double Student::average() const
    {
      if ( ArrayDb::size() > 0)
        return ArrayDb::sum() / ArrayDb::size();
      else
        return 0;
    }
    
    const string & Student::Name() const
    {
      return ( const string & ) * this;
    }
    
    double & Student::operator[](int i)
    {
      return ArrayDb::operator[](i);
    }
    
    // private method
    ostream & Student::arr_out(ostream & os) const
    {
      int i;
      int lim = ArrayDb::size();
      if( lim > 0 )
        {
          for (i = 0; i < lim ; i++)
    	{
    	  os << ArrayDb::operator[](i) << " ";
    	  if( i % 5 == 4)
    	    os << endl;
    	}
          if( i % 5 != 0)
    	os << endl;
        }
      else
        os << "empty array";
      return os;
    }
    
    // friends
    
    istream & operator>>(istream & is, const Student & stu)
    {
      is >> (string &) stu;
      return is;
    }
    
    istream & getline(istream & is, const Student & stu)
    {
      getline(is, (string &) stu);
      return is;
    }
    
    ostream & operator<<(ostream & os, const Student stu)
    {
      os << "Scores for " << (const string &) stu << ":\n";
      stu.arr_out(os);
      return os;
    }
    Code:
    #include <iostream>
    #include "student.h"
    using std::cin;
    using std::cout;
    using std::endl;
    
    void set(Student  & sa , int n);
    
    const int pupils = 3;
    const int quizzes = 3;
    
    int main()
    {
      Student ada[pupils] = { Student(quizzes), Student(quizzes), Student(quizzes) };
      int i;
      for ( i=0 ; i<pupils ; i++)
        set (ada[i] , quizzes);
      cout << "\nStudent List: \n";
      for( i = 0 ; i < pupils ; ++i)
        cout << ada[i].Name() << endl;
      cout << "\nResults: ";
      for ( i = 0; i < pupils ; i++)
        {
          cout << endl << ada[i];
          cout << "average: " << ada[i].average() << endl;
        }
      cout << "Done. \n";
      return 0;
    }
    
    void set(Student & sa, int n)
    {
      cout << "Enter Student name: ";
      getline(cin, sa);
      cout << "Enter " << n << "quiz scores: \n";
      for ( int i = 0; i < n ; i++)
        cin >> sa[i];
      while ( cin.get() != '\n')
        continue;
    }
    When I tried to compile, i get
    Code:
    student.cpp: In function ‘std::ostream& operator<<(std::ostream&, Student)’:
    student.cpp:27: error: ‘std::ostream& Student::arr_out(std::ostream&) const’ is private
    student.cpp:64: error: within this context
    the "student.cpp:64" is the line:
    Code:
    ostream & operator<<(ostream & os, const Student stu)
    {
      os << "Scores for " << (const string &) stu << ":\n";
      stu.arr_out(os);
      return os;
    }
    Why didn't it compile? Should i make the method public instead?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    friend std::ostream & operator<<(std::ostream & os, const Student & stu);
    Code:
    ostream & operator<<(ostream & os, const Student stu);
    Compare and contrast.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    Quote Originally Posted by tabstop View Post
    Code:
    friend std::ostream & operator<<(std::ostream & os, const Student & stu);
    Code:
    ostream & operator<<(ostream & os, const Student stu);
    Compare and contrast.
    Thank you for your response. It works now :-D.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  3. Question about this method...
    By Raigne in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2008, 06:43 PM
  4. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  5. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21