CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2015
    Posts
    4

    cannot bind to ostream

    I am writing class templates and testing them out. Everything seemed to be fine when I tested out my integers but when I got to my double and char classes the compiler gave me this error:

    Code:
    In function 'int main()':
    93:9: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
    In file included from /usr/include/c++/4.9/iostream:39:0,
                     from 2:
    /usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = array<double>]'
         operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
         ^
    95:9: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
    In file included from /usr/include/c++/4.9/iostream:39:0,
                     from 2:
    /usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = array<double>]'
         operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
         ^
    101:9: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
    In file included from /usr/include/c++/4.9/iostream:39:0,
                     from 2:
    /usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = array<char>]'
         operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
         ^
    102:11: error: cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'
    In file included from /usr/include/c++/4.9/iostream:40:0,
                     from 2:
    /usr/include/c++/4.9/istream:872:5: note: initializing argument 1 of 'std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = array<char>]'
         operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
         ^
    103:9: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
    In file included from /usr/include/c++/4.9/iostream:39:0,
                     from 2:
    /usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = array<char>]'
         operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
         ^

    This is my code

    Code:
    
    #include <iostream>
    #include <iomanip>
    #include <typeinfo>
    #include <string>
    
    using namespace std;
    
    
    template< typename T > class array {
    private:
    	int size;
    	T *myarray;
    public:
    // constructor 
    	array (int s) {
    	size = s;
    	myarray = new T [size];
    	}
    
    void setArray ( int elem, T val) {
    	myarray[elem] = val;
    	}
    
    // for loop to display all elements of an array
    void getArray () {
         for ( int j = 0; j < size; j++ ) {
            cout << setw( 7 ) << "\n Array placement:  "<< j << setw( 13 ) << "\n Array value: " << myarray[ j ] 
    << " type: " << typeid(myarray[ j ]).name() << endl;
    	}
    
    }
    
    
    void inputArray(){
        cout << "Enter size of array." << endl;
        cin >> size;
        cout << "Enter a value." << endl;
            for ( int j = 0; j < size; j++ ) {
        cin >> myarray[ j ];
            }
    }
    };
    
    int main() 
    {
        
    // test
    array< int > int_array(2);
    
    int_array.setArray(0,3);
    
    int_array.setArray(1,3.4);
    
    int_array.getArray();
    
    array< float > float_array(3);
    
    float_array.setArray(0,3.4);
    
    float_array.setArray(1,2.8);
    
    float_array.getArray();
    
    array< char > char_array(5);
    
    
    char_array.setArray(0,'H');
    char_array.setArray(1,'E');
    char_array.setArray(2,'L');
    char_array.setArray(3,'L');
    char_array.setArray(4,'O');
    
    char_array.getArray(); // test
    
    cout << "TESTING INTEGERS\n";
    
       array < int > integers(3);
       cout << "After instantiation, array: " << endl;
       integers.getArray();
       integers.setArray(0,8);
       integers.setArray(1,4);
       integers.setArray(2,9);
       cout << "After initialization, array: " << endl;
       integers.getArray();
       integers.inputArray();
       integers.getArray();
    
       cout << "\nTESTING DOUBLES\n";
       array < double > dbls(2);
       dbls.setArray(0,0);
       dbls.getArray();
       cout << "After instantiation, array:\n" << dbls << endl;
       dbls.inputArray();
       cout << "After initialization, array:\n" << dbls << endl;
       dbls.getArray();
    
       cout << "\nTESTING CHARS\n";
       array < char > chrs(2); 
       chrs.getArray();
       cout << "After instantiation, array:\n" << chrs << endl;;
       cin >> chrs;
       cout << "After initialization, array:\n" << chrs << endl;;
    /*
       cout << "\nTESTING STRINGS\n";
       array < string > strs; 
       cout << "After instantiation, array:\n" << strs << endl;;
       cin >> strs;
       cout << "After initialization, array:\n" << strs << endl;;
     */
    
    return 0;
    }
    I tried googling it but a lot of the answers said it was the compiler's fault and I needed a work around but I'm not sure how to do that.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: cannot bind to ostream

    Quote Originally Posted by underfor
    Everything seemed to be fine when I tested out my integers but when I got to my double and char classes the compiler gave me this error:
    Your tests were not consistent. Observe carefully what you wrote for integers:
    Code:
    array < int > integers(3);
    cout << "After instantiation, array: " << endl;
    integers.getArray();
    integers.setArray(0,8);
    integers.setArray(1,4);
    integers.setArray(2,9);
    cout << "After initialization, array: " << endl;
    integers.getArray();
    integers.inputArray();
    integers.getArray();
    Great. You called member functions like getArray and inputArray. No problem there. Now, compare with what you wrote for doubles:
    Code:
    cout << "\nTESTING DOUBLES\n";
    array < double > dbls(2);
    dbls.setArray(0,0);
    dbls.getArray();
    cout << "After instantiation, array:\n" << dbls << endl;
    dbls.inputArray();
    cout << "After initialization, array:\n" << dbls << endl;
    dbls.getArray();
    Notice that you attempted to print dbls twice with an overloaded operator<< that you did not declare, much less implement!

    Likewise, observe for chars:
    Code:
    cout << "After instantiation, array:\n" << chrs << endl;;
    cin >> chrs;
    cout << "After initialization, array:\n" << chrs << endl;;
    You attempted to print chrs twice with an overloaded operator<< that you did not declare, much less implement, and in between you attempted to read into chrs using an overloaded operator>> that you likewise did not declare, much less implement!

    Quote Originally Posted by underfor
    I tried googling it but a lot of the answers said it was the compiler's fault and I needed a work around but I'm not sure how to do that.
    No, it is not your compiler's fault. It is your fault for not overloading operator<< and operator>> (or for attempting to use them).
    • Get rid of getArray in favour of overloading operator<< for std::ostream. I think that getArray is not a good name for such a function anyway as usually people would expect a function prefixed with "get" to return rather than print output.
    • Keep inputArray, but turn it into a non-member non-friend function. You might be tempted to overload operator>> instead, but your inputArray function does interactive input, whereas it is more typical for an overloaded operator>> for std::istream to read input as if it comes from a file. If you take this advice, you should get rid of your attempted use of operator>> in main.
    • setArray should be renamed to setElement.
    • You should match new[] with delete[]. In this case, a first step is to define the destructor.


    By the way, since C++11, there is a container class template named array in the standard library. This means that if you want to name your class template array, you need to be careful to avoid a possible name collision, e.g., by only placing using namespace std (or using std::array) in a restricted scope where your own array class template is not used, and perhaps placing your own array class template in your own namespace.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: cannot bind to ostream

    Nice Answer Laserlight. Very professional.
    ahoodin
    To keep the plot moving, that's why.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured