Why is ostream not working?

Why is ostream not working?, I included string.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <iostream>
#include <string.h>

class String
{
public:
    //constructors
    String();
    String(const char *const);
    String(const String &);
    ~String();
    
    //overloaded operators
    char & operator[](int offset);
    char operator[](int offset) const;
    String operator+(const String&);
    void operator+=(const String&);
    String & operator=(const String &);
    friend ostream& operator<<(ostream& theStream, String& theString);
    // General accessors
    int GetLen()const { return itsString; }
    const char * GetString() const { return itsString; }
    // static int ConstructorCount++;
    
private:
    String (int);
    char * itsString;
    unsigned short itsLen;
};

ostream& operator<<(ostream& theStream, String& theString)
{
    theStream << theString.GetString();
    return theStream;
}

int main()
{
	String theString("Hello world.");
    std::cout <<theString;
}
Because you haven't pulled in std::

Either (ahem!) have
using namespace std;
or put std:: in the places it is needed. (e.g. std::ostream)


... After which you have to fix line 21 as well (but I don't have a clue what you intended here; maybe use strlen() ).
Last edited on
It is working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstring>

class String
{
    public:
        String( const char* cstr = nullptr )
        {
            if( cstr == nullptr ) cstr = "" ;
            
            itsLen = std::strlen(cstr) ;
            itsString = new char[itsLen+1] ;
            std::strcpy( itsString, cstr ) ;
        }

        // destructor etc.

        const char* GetString() const { return itsString; }

    private:
        char* itsString;
        std::size_t itsLen;
};

std::ostream& operator << ( std::ostream& theStream, const String& theString )
{ return theStream << theString.GetString(); }

int main()
{
    const String theString("Hello world!");
    std::cout << theString << '\n' ;
}

echo && echo && g++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out 
echo && echo =========== && echo && clang++ -std=c++17 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out


Hello world!

===========

Hello world!

http://coliru.stacked-crooked.com/a/a1bd5326a0e45f08
http://rextester.com/MGOM12607
Hello can you show me how you would add an extraction operator >> to this? Thanks.
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstring>
#include <cctype>

class String
{
    public:
        String( const char* cstr = nullptr )
        {
            if( cstr == nullptr ) cstr = "" ;

            itsLen = std::strlen(cstr) ;
            itsString = new char[itsLen+1] ;
            std::strcpy( itsString, cstr ) ;
        }

        // destructor etc.

        const char* GetString() const { return itsString; }

        void push_back( char c )
        {
            // ressize the buffer
            char* temp = new char[itsLen+2] ;
            std::memcpy( temp, itsString, itsLen ) ;
            delete[] itsString ;
            itsString = temp ;
            
            itsString[itsLen] = c ; // append the charcter c
            ++itsLen ;
            itsString[itsLen] = 0 ; // null terminate
        }

        std::istream& read_from_istream( std::istream& stm )
        {
            char c ;

            while( stm.get(c) && std::isspace(c) ) ; // skip leading white space
            if( !stm ) return stm ; // extraction failed            

            // append characters till next white space
            do push_back(c) ; while( stm.get(c) && !std::isspace(c) ) ;

            // if extraction ended because of white space, put the white space back
            if( std::isspace(c) ) stm.unget() ;

            return stm ;
        }

    private:
        char* itsString;
        std::size_t itsLen;
};

std::ostream& operator << ( std::ostream& theStream, const String& theString )
{ return theStream << theString.GetString(); }

std::istream& operator >> ( std::istream& theStream, String& theString )
{ return theString.read_from_istream(theStream) ; }

int main()
{
    String theString ;
    int i ;
    std::cin >> theString >> i ;
    std::cout << theString << '\n' << i << '\n' ;
}

http://coliru.stacked-crooked.com/a/a2ce146b9aa581f7

Note that this is an inefficient implementation, which relocates the buffer for each character that is read.
(A crude fix would be to use malloc/realloc/free instead of new[]/delete[])
Topic archived. No new replies allowed.