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;
| |