Operator overloading
Nov 15, 2013 at 7:17pm UTC
Hi, I'm getting this from Visual Studios 2010
error C2582: 'operator =' function is unavailable in 'ArrayList<TYPE>'
Basically, I want to create a new object called Restaurant and store it in an array that is being managed by the class RArrayList.
In my restaurant class header file.
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
class Restaurant
{
private :
string name;
int rating;
RTime openingTime;
RTime closingTime;
public :
void setName(string n);
void setRating(int r);
void setOpeningTime(RTime oTime);
void setClosingTime(RTime cTime);
string getName() const ;
int getRating() const ;
RTime getOpeningTime() const ;
RTime getClosingTime() const ;
const Restaurant& operator =(Restaurant& obj)
{
name = obj.name;
rating = obj.rating;
openingTime = obj.openingTime;
closingTime = obj.closingTime;
return *this ;
}
public :
Restaurant();
~Restaurant();
};
In my RArrayList header file
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
#pragma once
#include "RArray.h"
#include"Restaurant.h"
template <typename TYPE>
class ArrayList : public Array<TYPE>
{
private :
int count;
public :
bool isEmpty() const { return (count ==0); }
bool isFull() const { return (count >= SIZE); }
int getCount() const { return count; }
void addToBack(const TYPE& element);
public :
ArrayList(int size);
};
template <typename TYPE>
ArrayList<TYPE>::ArrayList(int size) : Array(size)
{
count = 0;
}
template <typename TYPE>
void ArrayList<TYPE>::addToBack(const TYPE& element)
{
if (isFull())
throw runtime_error("List is Full" );
ar[count] = element;
count++;
}
In main. That equals sign gets underlined with a red squiggle.
restaurant[restaurant->getCount()] = new Restaurant;
Last edited on Nov 15, 2013 at 7:25pm UTC
Nov 15, 2013 at 7:47pm UTC
Without knowing the definition of restaurant
, we can't possibly know what's going on.
Is there any particular reason you decided not to show us the information that would actually help us help you?
Nov 15, 2013 at 7:54pm UTC
No reason at all. Sorry about that.
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
#include "Restaurant.h"
void Restaurant::setName(string n)
{
name = n;
}
void Restaurant::setRating(int r)
{
if (r < 1)
rating = 1;
else if (r > 5)
rating = 5;
else
rating = r;
}
void Restaurant::setOpeningTime(RTime oTime)
{
openingTime = oTime;
}
void Restaurant::setClosingTime(RTime cTime)
{
closingTime = cTime;
}
string Restaurant::getName() const
{
return name;
}
int Restaurant::getRating() const
{
return rating;
}
RTime Restaurant::getOpeningTime() const
{
return openingTime;
}
RTime Restaurant::getClosingTime() const
{
return closingTime;
}
Restaurant::Restaurant()
{
rating = 0;
}
Nov 15, 2013 at 7:57pm UTC
I was talking about restaurant
, the variable in that line from main that you showed us. How have you defined it? We can't know why you're getting the error if we don't know what types the variables are.
Nov 15, 2013 at 8:00pm UTC
I think I've answered your question now.
ArrayList<Restaurant*> restaurant(MAX)
Topic archived. No new replies allowed.