Thread: simple class with header file

  1. #1

    simple class with header file

    Hi and thanks in advance for helping me out. I am writing a class in C++ called I am supposed to include prototypes for all my methods in a header file (PolyLine.h). I am working on a unix server using the GNU compiler.

    I have experience in Java and a little in C but this is my first real C++ program. So far my PolyLine.h file looks like this:

    #ifndef PolyLine_h
    #define PolyLine_h
    #include <iostream.h>

    class PolyLine
    ostream & operator <<(ostream& os, const PolyLine& p1);//operator <<

    PolyLine(double *x, double *y, int len);// c-tor

    PolyLine(const PolyLine& src);//copy c-tor

    PolyLine& operator=( PolyLine& src);// operator =

    ~PolyLine ();//d-tor

    int points();

    double value(double x);

    double max();

    double max(double x1, double x2);

    double *xArray;
    double *yArray;
    int length;

    and my file looks like this:

    #include "PolyLine.h"
    #include <iostream.h>

    ostream& operator <<(ostream& os, const PolyLine& p1)
    {return os;}
    int points(){}
    double value(double x){}
    double max(){}
    double max(double x1, double x2){}
    PolyLine::PolyLine(double *x, double *y, int len){}
    PolyLine::PolyLine(const PolyLine& src){}
    PolyLine PolyLine& :perator=( PolyLine& src)
    {return *this;}
    PolyLine::~PolyLine () {}

    as you can see I have left the methods all empty in hopes of compiling it first and then expanding on it later but the complier returns the following error (borg is the name of the server I am working from):

    borg:~/cs3132/a2$ g++
    In file included from
    PolyLine.h:8: `PolyLine:perator <<(ostream &, const PolyLine &)' must take exactly one argument syntax error before `&'

    I am sure that operator << can take more than one argument, not sure why I am getting this error. If someone out there can help me, please do! =)

    thank you very much for your time in any event. Happy coding

  2. #2
    Registered User
    Join Date
    Aug 2001
    Either make the << operator a friend function or a member function.

    Likewise in the cpp file you need make all the functions (except << if you make it a friend) member functions by adding the class name and scope operator after the return type and before the function name, like you did with the constructors.

    int PolyLine:oints() {}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM