Vector Bag ADT problem

I have to write a program (proj2.cpp) designed to overload the + operator in order to come up with the union of two vector-based ADT bags. Here is the part of code that generates the error:
1
2
3
4
5
6
template<class ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag){
    VectorBag<ItemType> newBag;
    int Size = sizeof(secondItems)/sizeof(int);
    for (int i = 0; i < Size; i++)
        firstBag.add(secondItems[i]);
}

I seem to have the main part down, but I keep getting the same syntax error:
1
2
3
4
5
6
7
8
9
10
proj2.cpp:136:87: error: ISO C++ forbids declaration of ‘operator+’ with no type [-fpermissive]
 template<class ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag){
                                                                                       ^
proj2.cpp:136:26: error: prototype forint VectorBag<ItemType>::operator+(VectorBag<ItemType>)’ 
 does not match any in class ‘VectorBag<ItemType>’
 template<class ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag){
                          ^
In file included from proj2.cpp:28:0:
VectorBag.h:22:28: error: candidate is: VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType>)
        VectorBag<ItemType> operator+(VectorBag<ItemType> anotherBag);


The VectorBag.h is the interface file containing the basic operations of an ADT bag. But I can't even figure out the source of the problem here. Any help? Thanks!
First error: what is the type of a function? What is the type that you get when the function has been evaluated?

Second error: Compiler made a guess that the type is int. Too bad the class has no such member.

Third message notes that the class does have a member function with that name, whose type is VectorBag<ItemType>.

Lets repeat the error:
1
2
3
4
5
6
7
8
9
struct Foo
{
  double bar();
};

Foo::bar() // error
{
  return 0.7;
}

Can you spot it now?
Last edited on
Okay, so I changed it to this now :
1
2
3
4
5
6
7
8
template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType> operator+(VectorBag<ItemType> anotherBag){
    VectorBag<ItemType> newBag;
    int Size = sizeof(secondItems)/sizeof(int);
    for (int i = 0; i < Size; i++)
        firstBag.add(secondItems[i]);
}


That error is gone, but now the compiler prints this error:
proj2.cpp:138:21: error: invalid declarator before ‘operator
Put simply, what is the difference between these:
1
2
Foo::bar()
Foo  bar()
Foo::bar() means that it is a function or method in the scope of Foo, which might be a class, struct, or namespace. Foo bar() is a function that returns Foo, which might be a typedef, a template, a macro, a class, a struct, or an enum
The first one is a member function and the second is an instance. But either way I get an error here?
1
2
3
4
5
6
7
proj2.cpp: In member function ‘VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType>)’:
proj2.cpp:140:23: error: ‘secondItems’ was not declared in this scope
     int Size = sizeof(secondItems)/sizeof(int);
                       ^
proj2.cpp:142:9: error: ‘firstBag’ was not declared in this scope
         firstBag.add(secondItems[i]);
         ^


It looks like the compiler doesn't read the template<class ItemType> part so it doesn't recognize 'firstBag' and 'secondItems', both of which are declared in a function outside of operator+
Last edited on
What are the 'firstBag' and 'secondItems' supposed to be?
firstBag is the ADT bag and here it's trying to add the array secondItems with i items into the bag.
Where are firstBag and secondItems defined? We can see that i is defined in the for loop: int i = 0. Where is the equivalent for firstBag and secondItems?
they are both declared in the void function, here:
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <algorithm>
#include <iostream>
#include <string>
#include "VectorBag.h"

using namespace std;

template<class ItemType> void displayBag(VectorBag<ItemType>& bag);
template<class ItemType> void bagTester(VectorBag<ItemType>& bag);
void newOpsTester();

/**
 * The usual <code>main()</code> function.
 */
int main()
{
   VectorBag<string> bag;
   cout << "Testing the Vector-Based Bag:" << endl;
   cout << "The initial bag is empty." << endl;
   bagTester(bag);
   newOpsTester();
   cout << "All done!" << endl;
   
   return 0;
}

/**
 * Display the contents of a <code>VectorBag</code>, which can hold
 * any kind of element.  The elements are displayed in sorted order.
 * (This means that the <code>ItemType</code> must be one for which
 * the&nbsp;"&lt;" operation is defined.)
 * @param bag The bag to be displayed.
 */
template<class ItemType>
void displayBag(VectorBag<ItemType>& bag)
{
   cout << "The bag contains " << bag.getCurrentSize()
        << " items:" << endl;
   vector<ItemType> bagItems = bag.toVector();
   
   int numberOfEntries = static_cast<int>(bagItems.size());
   sort(bagItems.begin(), bagItems.end());

   for (int i = 0; i < numberOfEntries; i++) 
      cout << bagItems[i] << " ";
   cout << endl << endl;
}  // end displayBag

/**
 * This is an adaption of the <code>bagTester</code> function in 
 * Carrano-Henry <code>main.cpp</code>, which tests the
 * <code>ArrayBag</class>.  The changes are as follows:
 * <ol>
 * <li> Whereas the original version assumed that
 * the <code>ArrayBag</code> contained <code>string</code>, our
 * version allows the <code>VectorBag</code> to contain any kind of
 * item.</li>
 * <li> In addition to testing the original operations that are
 * implemented from <code>BagInterface</code>, we test the operations
 * of union, intersection, and difference.</li>
 * </ol>
 * @param bag The bag to be tested.
 */
template<class ItemType> void bagTester(VectorBag<ItemType>& bag)
{
   cout << "isEmpty: returns " << bag.isEmpty() 
        << "; should be 1 (true)" << endl;
   displayBag(bag);

   string items[] = {"one", "two", "three", "four", "five", "one"};
   int itemsSize = sizeof(items)/sizeof(char*);
   cout << "Adding " << itemsSize << " items to the bag: " << endl;
   for (unsigned int i = 0; i < itemsSize; i++)
      bag.add(items[i]);
   
   displayBag(bag);
   
   cout << "isEmpty: returns " << bag.isEmpty() 
        << "; should be 0 (false)" << endl;
	
   cout << "getCurrentSize: returns " << bag.getCurrentSize() 
        << "; should be " << itemsSize << endl;
   
   cout << "Try to add another entry: add(\"extra\") returns " 
        << bag.add("extra") << endl;      
   
   cout << "contains(\"three\"): returns " << bag.contains("three")
        << "; should be 1 (true)" << endl;
   cout << "contains(\"ten\"): returns " << bag.contains("ten")
        << "; should be 0 (false)" << endl;
   cout << "getFrequencyOf(\"one\"): returns "
        << bag.getFrequencyOf("one") << " should be 2" << endl;
   cout << "remove(\"one\"): returns " << bag.remove("one")
        << "; should be 1 (true)" << endl;
   cout << "getFrequencyOf(\"one\"): returns "
        << bag.getFrequencyOf("one") << " should be 1" << endl;
   cout << "remove(\"one\"): returns " << bag.remove("one")
        << "; should be 1 (true)" << endl;
   cout << "remove(\"one\"): returns " << bag.remove("one")
        << "; should be 0 (false)" << endl;
   cout << endl;
   
   displayBag(bag);
   
   cout << "After clearing the bag, ";
   bag.clear();
   
   cout << "isEmpty: returns " << bag.isEmpty()
        << "; should be 1 (true)" << endl;
}  // end bagTester

template<class ItemType> 
VectorBag<ItemType>
VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag){
    VectorBag<ItemType> newBag;
    int Size = sizeof(secondItems)/sizeof(int);
    for (int i = 0; i < Size; i++)
        firstBag.add(secondItems[i]);
}

/**
 * Test the new VectorBag operations (union, intersection, difference)
 */

void newOpsTester()
{
   cout << "\nLet's test the new bag operations.\n\n";
   int firstItems[] = {2, 4, 6, 6, 8, 10, 12, 15, 15};
   VectorBag<int> firstBag;
   int firstSize = sizeof(firstItems)/sizeof(int);
   cout << "Adding " << firstSize << " items to first bag: " << endl;
   for (int i = 0; i < firstSize; i++)
      firstBag.add(firstItems[i]);
   cout << "First bag: ";
   displayBag(firstBag);

   int secondItems[] = {3, 6, 9, 12, 12, 15, 15, 18};
   VectorBag<int> secondBag;
   int secondSize = sizeof(secondItems)/sizeof(int);
   cout << "Adding " << secondSize << " items to second bag: " << endl;
   for (int i = 0; i < secondSize; i++)
      secondBag.add(secondItems[i]);
   cout << "Second bag: ";
   displayBag(secondBag);

   VectorBag<int> unionBag = firstBag + secondBag;
   cout << "firstBag union secondBag: \n";
   displayBag(unionBag);

   VectorBag<int> intersectBag = firstBag * secondBag;
   cout << "firstBag intersect secondBag: \n";
   displayBag(intersectBag);

   VectorBag<int> diffBag = firstBag - secondBag;
   cout << "firstBag minus secondBag: \n";
   displayBag(diffBag);

   diffBag = secondBag - firstBag;
   cout << "secondBag minus firstBag: \n";
   displayBag(diffBag);

}


I realize I have to link them somehow, but it has to be without changing anything in the void function. and that's exactly what I'm having trouble with. suggestions?
> I realize I have to link them somehow
http://www.cplusplus.com/doc/tutorial/functions/


Also, this refer to the object that receives the message
Topic archived. No new replies allowed.