simple class with header file

This is a discussion on simple class with header file within the C++ Programming forums, part of the General Programming Boards category; Hi and thanks in advance for helping me out. I am writing a class in C++ called PolyLine.cc I am ...

  1. #1
    Unregistered
    Guest

    simple class with header file

    Hi and thanks in advance for helping me out. I am writing a class in C++ called PolyLine.cc 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
    {
    public:
    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);



    private:
    double *xArray;
    double *yArray;
    int length;
    };
    #endif

    and my PolyLine.cc 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++ PolyLine.cc
    In file included from PolyLine.cc:1:
    PolyLine.h:8: `PolyLine:perator <<(ostream &, const PolyLine &)' must take exactly one argument
    PolyLine.cc:12: 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
    Posts
    155
    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, 06:23 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM

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