Joined Vectors

I have written a function whose purpose is to join two vector strings as long as they don't go over a limit. I can't even test the function though because I am getting an error at the function call in main that says:

Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "setUnion" is undefined Project29

Seems like a scope issue, but I don't know why? The prototype is declared public.

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

template<class ItemType>
class ArraySet : public SetInterface<ItemType>
{
private:
	static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
	ItemType items[DEFAULT_CAPACITY];      // Array of bag items
	int itemCount;                         // Current count of bag items 
	int maxItems;                          // Max capacity of the bag

	// Returns either the index of the element in the array items that
	// contains the given target or -1, if the array does not contain 
	// the target.
	int getIndexOf(const ItemType& target) const;

public:
	ArraySet();
	ArraySet& setUnion(bool& Failure, ArraySet<std::string>& set1, ArraySet<std::string>& set2);
	ArraySet& setIntersection();
	ArraySet& setDifference();
	int getCurrentSize() const;
	bool isEmpty() const;
	bool add(const ItemType& newEntry);
	bool remove(const ItemType& anEntry);
	void clear();
	bool contains(const ItemType& anEntry) const;
	std::vector<ItemType> toVector() const;
}; // end ArrayBag

//#include "ArraySet.cpp"

#endif

template<class ItemType>
ArraySet<ItemType>& ArraySet<ItemType>::setUnion(bool& Failure, ArraySet<std::string>&set1, 
	                                                            ArraySet<std::string>& set2)
{
	ArraySet<std::string>& combineSets;
	combineSets.reserve(set1.size() + set2.getsize());
	combineSets.insert(combineSets.end(), set1.begin(), set1.end());

	combineSets.insert(combineSets.end(), set2.begin(), set2.end());

	if (combineSets > itemCount) {
		Failure = false;

		return Failure;
	}
		
}






int main()
{
	ArraySet<std::string> set1;
	std::string items1[] = { "one", "two", "three" };
	cout << "Add 6 items to the set: " << endl;
	for (int i = 0; i < 6; i++)
	{
		set1.add(items1[i]);
	}  // end for
	ArraySet<std::string> set2;
	std::string items2[] = { "one", "two", "three" };
	cout << "Add 6 items to the set: " << endl;
	for (int i = 0; i < 6; i++)
	{
		set2.add(items2[i]);
	}  // end for

	displaySet(set1);
	displaySet(set2);
	setUnion(set1, set2); // Error here *************************************************


	return 0;
} // end main
Doesn't setUnion take three arguments instead of two?

-Albatross
Yes your right. I fixed that but still get the same error.

1
2
3
4
        bool Failure;
	displaySet(set1);
	displaySet(set2);
	setUnion(Failure, set1, set2);
I do not see why you would want this over any of the stl containers, or 3rd party libraries like boost or just something you found on github.

You aren't gonna learn by creating something that doesn't solve a problem, you will only learn how to give yourself more problems. In programming nobody will make fun of you for taking shortcuts by depending on code that you didn't write (especially for wheel-like things like containers), and the less code you write, the less possibility that you will make a mistake (sure you should learn, but do it for a reason, and if there isn't, just read how its done and say "huh so that's how it works").

First problem is that you are returning a reference, 2nd is you created a reference object that doesn't reference anything and you are trying to use it, 3rd you return a bool, 4th you are looping 6 times but there are only 3 elements how is your code getting past this please compile your code in debug when debugging.

I think the "this" variable could help you if you want to return the current array BUT this doesn't make sense because you can just create a function that inserts a range of elements and just forget the setUnion, and your current logic is flawed since how could there be more items inside of an array than itemCount when the items in an array cannot be over itemCount (it could work, just doesn't make sense). Also where is "reserve" or "insert" defined how can it affect ArraySet's memory if it was inside of SetInterface?

I think you should study harder on practical programming and just get the job done in the best way possible focusing on KISS(keep it short and simple), readability, and usability.

nitpicks: you use namespace std but you still use std::string, just make a choice and stay consistant. "ArraySet" is confusing, is it more like a std::set or an std::array? The name "setUnion" doesn't make sense it's just a "merge" there is an <algorithm> function called std::set_union and it does something quite different, and there is also std::merge that works for stl containers. // end ArrayBag is obsolete/typo, also a good reason to avoid superfluous comments.

and what does this mean, why didn't you just delete this if it is dead code?
//#include "ArraySet.cpp"


edit: also from my understanding everything with templates requires all the code to be in headers, so that means it would make more sense for the function to just be defined inside the class if you can.
Last edited on
Everything in the class was already named and given to me as an assignment. My job is to write certain functions for the class, and setUnion is the focus here. I realize that the logic is flawed and that is why I posted so I could get some direction. However, I managed to find some help and it looks like the kinks are getting worked out.
setUnion() is a method of the ArraySet class. You can't just call it as if it were a free function, as you do on line 76; you need to call it on a specific ArraySet object.
Topic archived. No new replies allowed.