please help me to solve this problem, thanks. I am newcomer to C++

Discussion in 'C++' started by Simon, Jan 29, 2007.

  1. Simon

    Simon Guest

    platform: Borland C++ 5.5 free Winxp

    C++ source from: C++ primer Plus 5 Edition.

    Complie Error: "operator >>" not implemented in type 'istream' for
    arguments of type "STRING" in function main()


    //mystring.h
    #ifndef MYSTRING_H_
    #define MYSTRING_H_
    #include <iostream.h>

    using std::eek:stream;
    using std::istream;

    class STRING
    {
    private:
    char *str;
    int len;
    static int num_strings;
    static const int CINLIM=80;
    public:
    STRING(const char *s); //constructor
    STRING(); //default constructor
    STRING(const STRING &);// copy constructor
    ~STRING();
    int length()const { return len; }
    //overload operator methods
    STRING &operator=(const STRING &);
    STRING &operator=(const char *);
    char &operator[] (int i);
    const char &operator[] (int i)const;
    //overload operator friends
    friend bool operator<(const STRING &st, const STRING &st2);
    friend bool operator>(const STRING &st1, const STRING &st2);
    friend bool operator==(const STRING &st, const STRING &st2);
    friend ostream &operator<<(ostream &os, const STRING &st);
    friend istream &operator>>(istream *is, STRING &st);
    //static function
    static int HowMany();
    };
    #endif

    //mystring.cpp

    #include <cstring.h>
    #include <string.h>
    #include "mystring.h"

    using std::cin;
    using std::cout;

    int STRING::num_strings=0;

    int STRING::HowMany()
    {
    return num_strings;
    }
    //class methods

    STRING::STRING(const char *s)
    {
    len=std::strlen(s);
    str=new char[len+1];
    std::strcpy(str,s);
    num_strings++;
    }

    STRING::STRING()
    {
    len=4;
    str=new char[1];
    str[0]='\0';
    num_strings++;
    }
    STRING::STRING(const STRING &st)
    {
    num_strings++;
    len=st.len;
    str=new char[len+1];
    std::strcpy(str,st.str);
    }

    STRING::~STRING()
    {
    --num_strings;
    delete [] str;
    }
    //overload operator methods
    //assign a STRING to a STRING
    STRING & STRING::eek:perator=(const STRING &st)
    {
    if(this==&st)
    return *this;
    delete [] str;
    len=st.len;
    str=new char[len+1];
    std::strcpy(str, st.str);
    return *this;
    }
    // assign a C string to a string
    STRING &STRING::eek:perator=(const char *s)
    {
    delete [] str;
    len=std::strlen(s);
    str=new char[len+1];
    std::strcpy(str,s);
    return *this;
    }
    char &STRING::eek:perator[] (int i)
    {
    return str;
    }
    const char &STRING::eek:perator[] (int i)const
    {
    return str;
    }
    bool operator<(const STRING &st1, const STRING &st2)
    {
    return (std::strcmp(st1.str, st2.str)<0);
    }
    bool operator>(const STRING &st1, const STRING &st2)
    {
    return st2.str<st1.str;
    }
    bool operator==(const STRING &st1, const STRING &st2)
    {
    return (std::strcmp(st1.str, st2.str)==0);
    }
    ostream &operator<<(ostream &os, const STRING &st)
    {
    os<<st.str;
    return os;
    }

    istream & operator>> (istream & is, STRING &st)
    {
    char temp[STRING::CINLIM];

    is.get(temp, STRING::CINLIM);

    if(is)
    st=temp;
    while(is && is.get()!='\n')
    continue;
    return is;

    }


    //mysaying.cpp

    #include <iostream.h>
    #include "mystring.h"
    const int ArSize=10;
    const int MaxLen=81;


    int main()
    {
    using std::cout;
    using std::cin;
    using std::endl;
    STRING name;
    cout<<"Hi, what's your name?\n>>";
    cin >> name;

    cout<<name<<", please enter up to "<<ArSize
    <<"short sayings<empty line to quit>:\n";
    STRING sayings[ArSize];
    char temp[MaxLen];
    int i;
    for(i=0;i<ArSize;i++)
    {
    cout<<i+1<<": ";
    cin.get(temp,MaxLen);
    while(cin &&cin.get()!='\n')
    continue;
    if(!cin||temp[0]=='\n') //empty line
    break;
    else
    sayings=temp; //overload assignment
    }
    int total=i;
    cout<<"Here are you sayings:\n";
    for(i=0;i<total;i++)
    cout<<sayings[0]<<": "<<sayings<<endl;
    int shortest=0;
    int first=0;
    for(i=1;i<total;i++)
    {
    if(sayings.length()<sayings[shortest].length())
    shortest=i;
    if(sayings<sayings[first])
    first=i;
    }
    cout<<"Shortest saying:\n"<<sayings[shortest]<<endl;
    cout<<"First aphabetically:\n"<<sayings[first]<<endl;
    cout<<"This program used "<<STRING::HowMany()
    <<"STRING objects/Bye.\n";
    return 0;


    }
    Simon, Jan 29, 2007
    #1
    1. Advertising

  2. Simon

    Ian Collins Guest

    Re: please help me to solve this problem, thanks. I am newcomer toC++

    Simon wrote:
    > platform: Borland C++ 5.5 free Winxp
    >
    > C++ source from: C++ primer Plus 5 Edition.
    >
    > Complie Error: "operator >>" not implemented in type 'istream' for
    > arguments of type "STRING" in function main()
    >
    >
    > //mystring.h
    > #ifndef MYSTRING_H_
    > #define MYSTRING_H_
    > #include <iostream.h>
    >
    > using std::eek:stream;
    > using std::istream;
    >

    Putting 'using' in headers is no-no, it gets dragged into any file that
    includes the header.

    > class STRING


    Avoid all caps for class names, the idiomatic use for all caps is for
    macros.

    <snip>

    > friend istream &operator>>(istream *is, STRING &st);

    ^^
    There's your problem, should be istream&.

    > //static function


    Silly comment!

    > static int HowMany();
    > };


    --
    Ian Collins.
    Ian Collins, Jan 29, 2007
    #2
    1. Advertising

  3. Simon

    red floyd Guest

    Re: please help me to solve this problem, thanks. I am newcomer toC++

    Simon wrote:
    > platform: Borland C++ 5.5 free Winxp
    >
    > C++ source from: C++ primer Plus 5 Edition.
    >
    > Complie Error: "operator >>" not implemented in type 'istream' for
    > arguments of type "STRING" in function main()
    >
    >
    > //mystring.h
    > #ifndef MYSTRING_H_
    > #define MYSTRING_H_
    > #include <iostream.h>

    Non-standard include. You should use
    #include <iostream>

    Actually, since you never use a full iostream, just references, you
    should use

    #include <iosfwd>

    >
    > using std::eek:stream;
    > using std::istream;
    >
    > class STRING
    > {
    > private:
    > char *str;
    > int len;
    > static int num_strings;
    > static const int CINLIM=80;
    > public:
    > STRING(const char *s); //constructor
    > STRING(); //default constructor
    > STRING(const STRING &);// copy constructor
    > ~STRING();
    > int length()const { return len; }
    > //overload operator methods
    > STRING &operator=(const STRING &);
    > STRING &operator=(const char *);
    > char &operator[] (int i);
    > const char &operator[] (int i)const;
    > //overload operator friends
    > friend bool operator<(const STRING &st, const STRING &st2);
    > friend bool operator>(const STRING &st1, const STRING &st2);
    > friend bool operator==(const STRING &st, const STRING &st2);
    > friend ostream &operator<<(ostream &os, const STRING &st);
    > friend istream &operator>>(istream *is, STRING &st);
    > //static function
    > static int HowMany();
    > };
    > #endif
    >
    > //mystring.cpp
    >
    > #include <cstring.h>
    > #include <string.h>
    > #include "mystring.h"
    >
    > using std::cin;
    > using std::cout;
    >
    > int STRING::num_strings=0;
    >
    > int STRING::HowMany()
    > {
    > return num_strings;
    > }
    > //class methods
    >
    > STRING::STRING(const char *s)
    > {
    > len=std::strlen(s);
    > str=new char[len+1];
    > std::strcpy(str,s);
    > num_strings++;
    > }
    >
    > STRING::STRING()
    > {
    > len=4;
    > str=new char[1];
    > str[0]='\0';
    > num_strings++;
    > }
    > STRING::STRING(const STRING &st)
    > {
    > num_strings++;
    > len=st.len;
    > str=new char[len+1];
    > std::strcpy(str,st.str);
    > }
    >
    > STRING::~STRING()
    > {
    > --num_strings;
    > delete [] str;
    > }
    > //overload operator methods
    > //assign a STRING to a STRING
    > STRING & STRING::eek:perator=(const STRING &st)
    > {
    > if(this==&st)
    > return *this;
    > delete [] str;
    > len=st.len;
    > str=new char[len+1];
    > std::strcpy(str, st.str);
    > return *this;
    > }
    > // assign a C string to a string
    > STRING &STRING::eek:perator=(const char *s)
    > {
    > delete [] str;
    > len=std::strlen(s);
    > str=new char[len+1];
    > std::strcpy(str,s);
    > return *this;
    > }
    > char &STRING::eek:perator[] (int i)
    > {
    > return str;
    > }
    > const char &STRING::eek:perator[] (int i)const
    > {
    > return str;
    > }
    > bool operator<(const STRING &st1, const STRING &st2)
    > {
    > return (std::strcmp(st1.str, st2.str)<0);
    > }
    > bool operator>(const STRING &st1, const STRING &st2)
    > {
    > return st2.str<st1.str;
    > }
    > bool operator==(const STRING &st1, const STRING &st2)
    > {
    > return (std::strcmp(st1.str, st2.str)==0);
    > }
    > ostream &operator<<(ostream &os, const STRING &st)
    > {
    > os<<st.str;
    > return os;
    > }
    >
    > istream & operator>> (istream & is, STRING &st)
    > {
    > char temp[STRING::CINLIM];
    >
    > is.get(temp, STRING::CINLIM);
    >
    > if(is)
    > st=temp;
    > while(is && is.get()!='\n')
    > continue;
    > return is;
    >
    > }
    >
    >
    > //mysaying.cpp
    >
    > #include <iostream.h>
    > #include "mystring.h"
    > const int ArSize=10;
    > const int MaxLen=81;
    >
    >
    > int main()
    > {
    > using std::cout;
    > using std::cin;
    > using std::endl;
    > STRING name;
    > cout<<"Hi, what's your name?\n>>";
    > cin >> name;
    >
    > cout<<name<<", please enter up to "<<ArSize
    > <<"short sayings<empty line to quit>:\n";
    > STRING sayings[ArSize];
    > char temp[MaxLen];
    > int i;
    > for(i=0;i<ArSize;i++)
    > {
    > cout<<i+1<<": ";
    > cin.get(temp,MaxLen);
    > while(cin &&cin.get()!='\n')
    > continue;
    > if(!cin||temp[0]=='\n') //empty line
    > break;
    > else
    > sayings=temp; //overload assignment
    > }
    > int total=i;
    > cout<<"Here are you sayings:\n";
    > for(i=0;i<total;i++)
    > cout<<sayings[0]<<": "<<sayings<<endl;
    > int shortest=0;
    > int first=0;
    > for(i=1;i<total;i++)
    > {
    > if(sayings.length()<sayings[shortest].length())
    > shortest=i;
    > if(sayings<sayings[first])
    > first=i;
    > }
    > cout<<"Shortest saying:\n"<<sayings[shortest]<<endl;
    > cout<<"First aphabetically:\n"<<sayings[first]<<endl;
    > cout<<"This program used "<<STRING::HowMany()
    > <<"STRING objects/Bye.\n";
    > return 0;
    >
    >
    > }
    >
    red floyd, Jan 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shapper
    Replies:
    0
    Views:
    328
    shapper
    Dec 18, 2007
  2. Rustom Mody

    Re: Newcomer Help

    Rustom Mody, Feb 10, 2014, in forum: Python
    Replies:
    0
    Views:
    83
    Rustom Mody
    Feb 10, 2014
  3. Walter Hughey

    Re: Newcomer Help

    Walter Hughey, Feb 10, 2014, in forum: Python
    Replies:
    1
    Views:
    71
    Grant Edwards
    Feb 10, 2014
  4. Gisle Vanem

    Re: Newcomer Help

    Gisle Vanem, Feb 10, 2014, in forum: Python
    Replies:
    0
    Views:
    83
    Gisle Vanem
    Feb 10, 2014
  5. Ned Deily

    Re: Fwd: Newcomer Help

    Ned Deily, Feb 11, 2014, in forum: Python
    Replies:
    0
    Views:
    81
    Ned Deily
    Feb 11, 2014
Loading...

Share This Page