Error in overloading the istream operator>>

I get this error when compiling:
ccAssignmentOprOverload.cpp:19:9: error: invalid user-defined conversion from 'char [81]' to 'const ccAssignmentOprOverload&' [-fpermissive]
str = temp;
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
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "ccAssignmentOprOverload.h"
#include<iostream>
#include<cstdlib>
#include<cassert>
#include<iomanip>

using namespace std;

ostream& operator<<(ostream& osObject, const ccAssignmentOprOverload& str)
{
   osObject <<str.list;
   return osObject;
}
istream& operator>>(istream& isObject, ccAssignmentOprOverload& str)
{
    char temp[81];
    isObject>>setw(81)>> temp;
    str = temp;
    return isObject;
}
void ccAssignmentOprOverload::print() const
{
    if(length == 0)
        cout << "List is empty." << endl;
    else
    {
        for(int i = 0; i < length; i++)
            cout << list[i] << " ";
        cout << endl;
    }
}
void ccAssignmentOprOverload::insertEnd(int item)
{
    if(length == maxSize)
        cout <<"List is full."<< endl;
    else
        list[length++] = item;
}

void ccAssignmentOprOverload::destroyList()
{
    delete [] list;
    list == NULL;
    length = 0;
}

ccAssignmentOprOverload::ccAssignmentOprOverload(int size)
{
    length = 0;
    if(size <= 0)
        maxSize = 10;
    else
        maxSize = size;
    list = new int[maxSize];
    assert(list != NULL);
}

const ccAssignmentOprOverload& ccAssignmentOprOverload::operator=
                    (const ccAssignmentOprOverload& otherList)
{
    if(this != &otherList)    //avoid self-assignment;          //Line 1
    {
        if(list != NULL)
            destroyList();
        maxSize = otherList.maxSize;                           //Line 4
        length = otherList.length;

        if(maxSize != 0)
        {
            list = new int[maxSize];                          //Line 7
            assert(list != NULL);

            for(int i = 0; i < length; i++)
                list[i] = otherList.list[i];                      //Line 10
        }
        else
                list = NULL;
      }
      return *this;
}

Here is my main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "ccAssignmentOprOverload.h"
#include <iostream>

using namespace std;

int main()
{
    ccAssignmentOprOverload due_date;
    ccAssignmentOprOverload tomorrow;

    cout << endl << " Enter today's date: " << endl;
    cin >> due_date;

    due_date = tomorrow;
    cout << tomorrow;

    return 0;
}
Edit: corrected a sentence.
- - -

error: invalid user-defined conversion from 'char [81]' to 'const ccAssignmentOprOverload&'
1
2
3
char temp[81];
isObject>>setw(81)>> temp;
str = temp;


'temp' is defined as a char array --> char temp[81];
'str' is defined as a ccAssignmentOprOverload --> const ccAssignmentOprOverload& str

When you write
str = temp;
the compiler tells you it doesn't know how to convert an instance of the class 'ccAssignmentOprOverload' into a character array a character array into an instance of the class 'ccAssignmentOprOverload'.

It's hard to guess what you are trying to do, because it seems you want the characters you read from std::cin, and which you store into 'temp', to be copied into ccAssignmentOprOverload::list.

If so:

- on one hand, inside class ccAssignmentOprOverload there should be a property like
char* list;

- on the other hand, inside your constructor ccAssignmentOprOverload(int size) you initialize it as
list = new int[maxSize];
Hence, it looks more like a "int* list;" than a "char* list;"...

To solve your issue, you could add another overloaded operator=, perhaps like so:
ccAssignmentOprOverload& operator= (const char* const input);
which takes care of the conversion from a char* to a ccAssignmentOprOverload instance.

Maybe we could help better if you posted also your header file and an example of the input you plan to provide at runtime.

Happy coding!
Last edited on
Hello Bopaki,

With out the "ccAssignmentOprOverload.h" header file there are more errors than what you have mentioned so far. Also with the "ccAssignmentOprOverload.h" header file there is no way to test the program.

Post the "ccAssignmentOprOverload.h" header file so I can have a better idea what is happening.

I can not say that I have seen anything like:
const ccAssignmentOprOverload& ccAssignmentOprOverload::operator=
I could be wrong, but I do not believe that you can return a "const" qualified variable.

The first example of an overloaded >> I found the function sets the member variables of the class. With out the header file I can tell just what you are trying to do in the function.

Andy
Thank you good people its all sorted out now!!!
Here is the test program to test the assignment operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "cAssignmentOprOverload.h"

using namespace std;

int main()
{
    cAssignmentOprOverload myList;
    cAssignmentOprOverload otherList;

    for (int i = 0; i < 10; i++)
    myList.insertEnd(i);

    cout<< "My original list is:" << endl;
    myList.print();
    otherList = myList;
    cout <<" My list copied to otherlist is: "<< endl;
    otherList.print();

}


I only need to now change it a bit, so that it should be me who enters the data into the original list.
Here is the output:
1
2
3
4
5
6
7
8
9
My original list is:
0 1 2 3 4 5 6 7 8 9
 My list copied to otherlist is:
0 1 2 3 4 5 6 7 8 9

Process returned 0 (0x0)   execution time : 0.052 s
Press any key to continue.

Here is the modified version:
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
#include <iostream>
#include "cAssignmentOprOverload.h"

using namespace std;

int main()
{
    cAssignmentOprOverload myList;
    cAssignmentOprOverload otherList;

    //for (int i = 0; i < 10; i++)
    //myList.insertEnd(i);

    int num;
    cout <<" Enter numbers ending with -999"<< endl;
    cin >> num;
    while (num != -999)
    {
        myList.insertEnd(num);
        cin >> num;
    }
    cout<< "My original list is:" << endl;
    myList.print();
    otherList = myList;
    cout <<" My list copied to otherlist is: "<< endl;
    otherList.print();

}

1
2
3
4
5
6
7
8
9
 Enter numbers ending with -999
78 65 45 23 90 123 67 -999
My original list is:
78 65 45 23 90 123 67
 My list copied to otherlist is:
78 65 45 23 90 123 67

Process returned 0 (0x0)   execution time : 21.720 s
Press any key to continue.


IS COMPUTER PROGRAMMING NOT WONDERFUL IF YOU ONLY GIVE IT SOME THINKING???
Topic archived. No new replies allowed.