I cannot declare objects of the class

I want to declare objects myList and otherList of the class cAssignmentOprOverload.
I get this error:
cAssignmentOprOverload\main.cpp:10:28: error: call of overloaded 'cAssignmentOprOverload()' is ambiguous
C:\Martin\MalikChapter3\cAssignmentOprOverload\main.cpp:10:28: note: candidates are:
In file included from C:\Martin\MalikChapter3\cAssignmentOprOverload\main.cpp:2:0:
C:\Martin\MalikChapter3\cAssignmentOprOverload\cAssignmentOprOverload.h:30:6: note: cAssignmentOprOverload::cAssignmentOprOverload()
C:\Martin\MalikChapter3\cAssignmentOprOverload\cAssignmentOprOverload.h:26:5: note: cAssignmentOprOverload::cAssignmentOprOverload(int)
C:\Martin\MalikChapter3\cAssignmentOprOverload\main.cpp:11:25: error: call of overloaded 'cAssignmentOprOverload()' is ambiguous
C:\Martin\MalikChapter3\cAssignmentOprOverload\main.cpp:11:25: note: candidates are:
In file included from C:\Martin\MalikChapter3\cAssignmentOprOverload\main.cpp:2:0:
C:\Martin\MalikChapter3\cAssignmentOprOverload\cAssignmentOprOverload.h:30:6: note: cAssignmentOprOverload::cAssignmentOprOverload()
C:\Martin\MalikChapter3\cAssignmentOprOverload\cAssignmentOprOverload.h:26:5: note: cAssignmentOprOverload::cAssignmentOprOverload(int)


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
  #ifndef CASSIGNMENTOPROVERLOAD_H_INCLUDED
#define CASSIGNMENTOPROVERLOAD_H_INCLUDED
#include<iostream>
using namespace std;

class cAssignmentOprOverload
{
    friend ostream& operator<< (ostream&, const cAssignmentOprOverload&);
    friend istream& operator>> (istream&, cAssignmentOprOverload&);
public:
    const cAssignmentOprOverload& operator=
                    (const cAssignmentOprOverload& otherList);
        //Overloads the assignment operator.
    void print() const;
        //Function to print the list.
    void insertEnd(int item);
        //Function to insert an item at the end of the list.
        //Postcondition: If the list is not full, length++;
        //               list[length] = item
        //               If the list is full, outputs an
        //               appropriate message.
    void destroyList();
        //Function to destroy the list
        //Postcondition: length = 0; maxSize = 0;
        //               list = NULL
    cAssignmentOprOverload(int size = 10);
        //constructor
        //Postcondition: length = 0; maxSize = size;
        //               list is an array of size maxSize
     cAssignmentOprOverload();
   ~cAssignmentOprOverload();
private:
        int maxSize;
    int length;
    int *list;
};


#endif // CASSIGNMENTOPROVERLOAD_H_INCLUDED

the implementation
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
81
82
83
84
85
86
87
#include <iostream>
#include <cassert>

#include "cAssignmentOprOverload.h"

using namespace std;

ostream& operator<<(ostream& osObject, const cAssignmentOprOverload& list)
{
    return osObject;
}
istream& operator>>(istream& isObject, cAssignmentOprOverload& list)
{
    return isObject;
}


void cAssignmentOprOverload::print() const
{
	if(length == 0)
		cout<<"List is empty"<<endl;
	else
	{
		for(int i = 0; i < length; i++)
			cout<<list[i]<<" ";
		cout<<endl;
	}
}

void cAssignmentOprOverload::insertEnd(int item)
{
	if(length == maxSize)
		cout<<"List is full"<<endl;
	else
		list[length++] = item;
}

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

cAssignmentOprOverload::cAssignmentOprOverload(int size)
{
	length = 0;

	if(size <= 0)
		maxSize = 10;
	else
	maxSize = size;

	list = new int[maxSize];
	assert(list != NULL);
}
cAssignmentOprOverload::~cAssignmentOprOverload()
{
    delete [] list;
}


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

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

			for(int i = 0; i < length; i++)			//Line 9
				list[i] = otherList.list[i];		//Line 10
		}
		else						  				//Line 11
			list = NULL;				   			//Line 12
	}

	return *this;					   				//Line 13
}


the main
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "cAssignmentOprOverload.h"

using namespace std;

int main() {

	using namespace std;

    cAssignmentOprOverload myList;
	cAssignmentOprOverload otherList;
You need to define your constructor cAssignmentOprOverload(); since that's the constructor that will be called according to your instantiation in main.
I have defined the default constructor and I am still getting the same error
1
2
3
4
5
6
cAssignmentOprOverload::cAssignmentOprOverload()
{
    length = 0;
    maxSize = 0;
    list = new int[maxSize];
}
The error states:
call of overloaded 'cAssignmentOprOverload()' is ambiguous
The reason is cAssignmentOprOverload(int size = 10);. Due to the default parameter you have two constructors that can be called without parameter, hence it is ambiguous. I suggest to remove the contstructor cAssignmentOprOverload(). It does not make too much sense to have a list with 0 size.
The program is now working after removing the default constructor.
The function insertEnd & print() are now working.

Here is what I get:
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty

Assigning myList to otherList:
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
I have now fixed the insertEnd function but the print() function is still not working
here is what I get:
1
2
3
4
5
6
7
8
9

Assigning myList to otherList:
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty
List is empty

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





Please show the code that produces the output.
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

#include<iostream>
using namespace std;

class cAssignmentOpr
{
    friend ostream& operator<< (ostream&, const cAssignmentOpr&);
    friend istream& operator>> (istream&, cAssignmentOpr&);
public:
    const cAssignmentOpr& operator=
                    (const cAssignmentOpr& otherList);
        //Overloads the assignment operator.
    void print() const;
        //Function to print the list.
    void insertEnd(int item);
        //Function to insert an item at the end of the list.
        //Postcondition: If the list is not full, length++;
        //               list[length] = item
        //               If the list is full, outputs an
        //               appropriate message.
    void destroyList();
        //Function to destroy the list
        //Postcondition: length = 0; maxSize = 0;
        //               list = NULL
    cAssignmentOpr(int size = 10);
        //constructor
        //Postcondition: length = 0; maxSize = size;
        //               list is an array of size maxSize
    // cAssignmentOprOverload();
   ~cAssignmentOpr();
private:
        int maxSize;
    int length;
    int *list;
};

implementation
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
81
82
83
84
85
86
87
88
#include <iostream>
#include <cassert>

#include "cAssignmentOpr.h"

using namespace std;

ostream& operator<<(ostream& osObject, const cAssignmentOpr& list)
{
    return osObject;
}
istream& operator>>(istream& isObject, cAssignmentOpr& list)
{
    return isObject;
}


void cAssignmentOpr::print() const
{
	if(length == 0)
		cout<<"List is empty"<<endl;
	else
	{
		for(int i = 0; i < length; i++)
			cout<<list[i]<<" ";
		cout<<endl;
	}
}

void cAssignmentOpr::insertEnd(int item)
{
	if(length == maxSize)
		cout<<"List is full"<<endl;
	else
		list[length++] = item;
}

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

cAssignmentOpr::cAssignmentOpr(int size)
{
	length = 0;

	if(size <= 0)
		maxSize = 10;
	else
	maxSize = size;

	list = new int[maxSize];
	assert(list != NULL);
}
cAssignmentOpr::~cAssignmentOpr()
{
    delete [] list;
}


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

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

			for(int i = 0; i < length; i++)			//Line 9
				list[i] = otherList.list[i];		//Line 10
		}
		else						  				//Line 11
			list = NULL;				   			//Line 12
	}

	return *this;					   				//Line 13
}

The driver
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
#include <iostream>
#include "cAssignmentOpr.h"

using namespace std;

int main() {

	using namespace std;

    cAssignmentOpr myList(100);
	cAssignmentOpr otherList;

        //int list[10];

    for(int i; i < 10; i++)
    {
        cin>> i;
        myList.insertEnd(i);
    }
    myList.print();
    //cAssignmentOpr list2(10);
    //for(int i; i < 10; i++)
		//{
		  //  cout << i;
		   //list2.print();
	//}
	// use overloaded assignment (=) operator

   cout <<"\nAssigning myList to otherList:" << endl;

	//	myList = otherList;

	//otherList.print();
	//cout <<endl;
	for(int i = 0; i < 10; i++) {
		myList.print() ;
	}

}
Line 15 in main(): for(int i = 0; i < 10; i++) // Note: i = 0

Line 31 when uncommented should be: otherList = myList
Thanks coder777!!! the program now works fine
Topic archived. No new replies allowed.