Trouble with overloading << operator

Hello,

I am trying to get a better understanding of overloading operators because it is the topic of discussion in my class currently. I cannot seem to get it to work when I am putting it into practice. For the code I have displayed below, I am getting errors such as:

'std::ostream& StringVar::operator<<(std::ostream&, const StringVar&)' must have exactly one argument

I am also getting other errors saying that 'outs' was not declared in the scope. I do not know where to begin. If someone could provide a better explanation of overloading operators that would be wonderful. My book doesn't do a good job at explaining this topic.


#include <iostream>
#include <cstring>
#include <ostream>
#include <istream>

using std::cout;
using std::cin;
using std::string;
using std::ostream;
using std::istream;
using std::endl;

class StringVar
{
public:
//default constructor will create an array of size 100.
StringVar();
//constructor will create an array of size (mySize).
StringVar(size_t);
//construtor will create an c-string.
StringVar(const char[]);
//outputer
char* getValue() { return value; }
//
void input_line(istream& ins)
{
ins.getline(value, max_size + 1);
}
ostream& operator <<(ostream& outs, const StringVar& theString)
{
outs << theString.value;
return outs;
}
private:
char *value;
size_t max_size;
};

int main()
{
StringVar hello("hello");

cout << hello.getValue();
cout (<< hello);
return 0;
}

StringVar::StringVar()
{
max_size = 100;
value = new char[max_size + 1];
value[0] = '\0';
}

StringVar::StringVar(size_t mySize)
{
max_size = mySize;
value = new char[max_size + 1];
value[0] = '\0';
}

StringVar::StringVar(const char array[]) : max_size(strlen(array))
{
value = new char[max_size + 1];
strcpy(value, array);
}


Thanks!!
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
#include <iostream>
#include <cstring>
#include <ostream>
#include <istream>

using std::cout;
using std::cin;
using std::string;
using std::ostream;
using std::istream;
using std::endl;

class StringVar
{
public:
    //default constructor will create an array of size 100.
    StringVar();
    //constructor will create an array of size (mySize).
    StringVar(size_t);
    //construtor will create an c-string.
    StringVar(const char[]);
    //outputer
    char* getValue() { return value; }
    //
    void input_line(istream& ins)
    {
        ins.getline(value, max_size + 1);
    }
    friend ostream& operator <<(ostream& outs, const StringVar& theString) // <--
    {
        outs << theString.value;
        return outs;
    }
private:
    char *value;
    size_t max_size;
};

int main()
{
    StringVar hello("hello");
    
    cout << hello.getValue();
    cout << hello; // <--
    return 0;
}

StringVar::StringVar()
{
    max_size = 100;
    value = new char[max_size + 1];
    value[0] = '\0';
}

StringVar::StringVar(size_t mySize)
{
    max_size = mySize;
    value = new char[max_size + 1];
    value[0] = '\0';
}

StringVar::StringVar(const char array[]) : max_size(strlen(array))
{
    value = new char[max_size + 1];
    strcpy(value, array);
}
Ah..okay thanks.
Topic archived. No new replies allowed.