Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

błąd przeciążenia operatora wejścia >> "cannot bind..."

Ostatnio zmodyfikowano 2015-02-13 11:50
Autor Wiadomość
Rigid
Temat założony przez niniejszego użytkownika
błąd przeciążenia operatora wejścia >> "cannot bind..."
» 2015-02-11 22:27:58
Mam problem w prostym programie - uczę się obiektowości cpp i nie mogę sobie poradzić z błędem.

W mainie bład w linii:
cin >> test4;
C/C++
#include <iostream>
#include "stringbad.h"
using namespace std;

int main()
{
   
    StringBad test4;
    cin >> test4;
    cout << endl << test4;
   
   
    return 0;
}

o treści:

error: cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'|


C/C++
#ifndef STRNGBAD_H_
#define STRNGBAD_H_

#include <iostream>
using namespace std;

class StringBad
{
private:
    char * str;
    int len;
    static int num_strings;
    //static const int CINLIM = 80;
public:
    StringBad( const char * s );
    StringBad( const StringBad & );
    StringBad();
    ~StringBad();
   
    StringBad & operator =( const StringBad & );
    StringBad & operator =( const char * ch );
    int lenght() const { return len; }
    static int howmany();
    char & operator []( int i );
    const char & operator []( int i ) const;
   
    friend ostream & operator <<( ostream & os, const StringBad & st );
    friend bool operator <( const StringBad &, const StringBad & );
    friend bool operator >( const StringBad &, const StringBad & );
    friend bool operator ==( const StringBad &, const StringBad & );
    friend istream & operator >>( iostream & is, StringBad & st );
   
};

#endif // STRNGBAD_H_


C/C++
#include <cstring>
#include "stringbad.h"
using namespace std;


int StringBad::num_strings = 0;

StringBad::StringBad( const char * s )
{
    len = strlen( s );
    str = new char[ len + 1 ];
    strcpy( str, s );
    num_strings++;
    cout << num_strings << ": \"" << str << "\" - obiekt utworzony.\n";
}

StringBad::StringBad( const StringBad & old )
{
    len = old.len;
    str = new char[ len + 1 ];
    strcpy( str, old.str );
    num_strings++;
    cout << num_strings << ": \"" << str << "\" - obiekt utworzony.\n";
}

StringBad::StringBad()
{
    len = 4;
    str = new char[ 4 ];
    strcpy( str, "C++" );
    num_strings++;
    cout << num_strings << ": \"" << str << "\" - obiekt domyslny utworzony.\n";
}

StringBad::~StringBad()
{
    cout << "\n\"" << str << "\" - obiekt usuniety\n";
    --num_strings;
    cout << "jest jeszcze " << num_strings << " obiektow.\n";
    delete[] str;
}

StringBad & StringBad::operator =( const StringBad & old )
{
    if( this == & old )
         return * this;
   
    cout << "  Zwolnienie " << str << " i wykonanie ";
    delete[] str;
    len = old.len;
    str = new char[ len + 1 ];
    strcpy( str, old.str );
    num_strings++;
    cout << "Operator przypisania...\n";
}

StringBad & StringBad::operator =( const char * ch )
{
    cout << "  Zwolnienie " << str << " i wykonanie ";
    delete[] str;
    len = strlen( ch );
    str = new char[ len + 1 ];
    strcpy( str, ch );
    cout << "Operator przypisania dla char...\n";
    return * this;
}

ostream & operator <<( ostream & os, const StringBad & st )
{
    os << st.str;
    return os;
}

int StringBad::howmany()
{
    return num_strings;
}

bool operator <( const StringBad & st1, const StringBad & st2 )
{
    /*int len1 = st1.len;
        int len2 = st2.len;
        if (len1 < len2)
            return 1;
        else
            return 0;
        */
    if( strcmp( st1.str, st2.str ) < 0 )
         return true;
    else
         return false;
   
}
bool operator >( const StringBad & st1, const StringBad & st2 )
{
    /*int len1 = st1.len;
        int len2 = st2.len;
        if (len1 > len2)
            return 1;
        else
            return 0;*/
    if( strcmp( st1.str, st2.str ) > 0 )
         return true;
    else
         return false;
   
}

bool operator ==( const StringBad & st1, const StringBad & st2 )
{
    return st2 > st1;
}

istream & operator >>( istream & is, StringBad & st )
{
    char temp[ 80 ];
    is.get( temp, 80 );
    if( is )
         st = temp;
   
    while( is && is.get() != '\n' )
         continue;
   
    return is;
}
char & StringBad::operator []( int i )
{
    return str[ i ];
}
const char & StringBad::operator []( int i ) const
{
    return str[ i ];
}

Edytowalem
to jest wszystko
P-126383
megatron
» 2015-02-11 23:12:52
Sry ze tak napisze ale wlasnie dziewczyna mnie zostawila i zycie mi sie psuje(tak ladnie to powiem) ;) i nie mg myslec
Ale "wydaje mi sie" , ze gdybys dal wiecej wiecej kodu swojego prostego programu napisanego obiektowo - bylo by latwiej w znalezieniu bledu ;)
P-126388
Rigid
Temat założony przez niniejszego użytkownika
» 2015-02-13 00:01:22
Dałem całość kodu
P-126459
megatron
» 2015-02-13 11:07:42
spróbuj tak:
C/C++
StringBad test4;
string str;
cin >> str;
test4 = a.c_str();
cout << endl << test4;


edit:
linijkę z pliku stringbad.h
char * str;
 daj po public i potem w main.cpp:

C/C++
StringBad test4;
cin >> test4.str;
cout << endl << test4;
P-126472
Monika90
» 2015-02-13 11:50:09

C/C++
friend istream & operator >>( iostream & is, StringBad & st );
Napisałeś iostream zamiast istream
P-126480
« 1 »
  Strona 1 z 1