Making Two Arrays Equal Each Other

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.
You'll need to provide and overload your operator[]. The same as overloading other types of operator.
I would need to overload the = operator, right? Whats the alternative as we havent learned how to do that yet.
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.
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Set::Set()
{
    set[0];
    MAXSIZE = 10;
    place = 0;
    size = 1;
}
    

Set::Set (Time timeObjectParameter, int sizeParameter)
{   

    place = 0;
    size = sizeParameter;
    set[place] = timeObjectParameter;

    MAXSIZE = 10;
	place++;

}

bool Set::addElement (Time& setParameter)
{
	bool valid = true;

	if (size < MAXSIZE){
        set[place] = setParameter;
        size++;
        place++;
    }    
	else 
		valid = false;
	
	return valid;
}

bool Set::isEmpty()
{

	bool empty = true;

	if (set > 0)
		empty = false;

	return empty;

}

bool Set::isFull()
{
	
	bool full = true;

	if (size < MAXSIZE)
		full = false;
	
	return full;

}

//For testing purposes until the overloaded << is implemented
void Set::printSet() const
{
     
     for (int i = 0; i < size - 1; i++)
		set[i].printTime();
        
}

Set Set::getData() const
{
    Set returnSet;
     for (int i = 0; i < size; i++)
     {
        Time tempTime = set[i]; 
		returnSet.addElement(tempTime);
     }
	
	return returnSet;	
     
}

int Set::getSize()
{    
     return size;
}         

ostream& operator<< (ostream& outS, const Set & tempSet)
{
	int size, sizeTemp;
	
	Set outSet = tempSet;
	
	size = outSet.getSize();
	
	outSet = outSet.getData();	
 
	for (int i = 0; i < size; i++)
	{
        Time tempTime = outSet[i];
		//outS << tempTime;
		tempTime.printTime();
    }

	return outS;

}


I changed some things since the original post but the error I get is the same except its in outSet not tempSet now. I appreciate the help.
If I can see the class declaration of Set it would much easier.

But reviewing your code for a moment, I am assuming that set is a member of class Set.

If so, then by doing tempSet[] will definitely not work unless you have an array of Sets.

So to solve your problem, you might want to try:
 
tempSet.set[i] = set[i]; //or any statement applicable. 


tempSet should also have a data member called set if set is a data member of class Set.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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[](const int 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.

Thanks again for the help.

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.
Topic archived. No new replies allowed.