I'm trying to have a getData function transfer all the objects it is holding in its array to a new array so that the new array can be used in a friend function to print the objects' information.
1 2 3 4 5 6
void Set::getData(Set& tempSet) const
{
for (int i = 0; i < size; i++)
tempSet[i] = set[i];
}
That code gives me this error: no match for 'operator[]' in 'tempSet[i]'
What am I doing wrong? Thanks for any help in advance.
The compiler asks for operator square brackets "[]" not the assignment operator "=".
If you can post up the whole code, then I can suggest some alternatives. Seeing that fragment of a code allows me to make lots of assumptions, making my suggestions not applicable in all cases.
class Set
{
private:
Time set[10];
int size, place;
int MAXSIZE;
public:
Set();
Set (Time , int);
bool addElement(Time&);
bool isEmpty();
bool isFull();
void printSet() const;
Set getData() const;
int getSize();
friend ostream& operator<< (ostream&, Set&);
Time &operator[](constint index){return set[index];}
Theres the declaration. What I posted gets rid of that error and all others except for this one:
[Linker error] undefined reference to `operator<<(std::ostream&, Set&)'
In my driver I say "cout << set1" to print the set I created and I get that error. The only thing I can think of is that in the overloaded << function, the outSet object doesnt have anything in it to print.
A small suggestion and recommendation: avoid using signed data types at all times. Try to use unsigned digits where appropriate especially for indexes, sizes, etc. This will help you prevent from getting an out of bounds exception. In your case, you have a small program so it is easy to keep track of your values. However, if you write a code for big projects, it's hard to go back and forth to see what your variables contain.
As for your problem, it says "undefined reference" it must be a problem with one of the arguments of operator<<().
Your declaration is:
friend ostream& operator<< (ostream&, Set&);
while your definition is:
ostream& operator<< (ostream& outS, const Set & tempSet)
note that a reference and a constant reference are two different things.