Overloading the + operator with two arguments?

I'm trying to append a string literal to the end of a "vector" (I'm not using STLs, I'm essentially imitating a vector).

My Sample.h:
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
  template <typename Object>
class Sample {
public:
        //assignment operator, copy constructor, etc. have been omitted but they work
	Sample(size_t num) : array_{new Object[num]}, size_{num} { } //used for temp

	void print(ostream & out) const {
		for (unsigned int i = 0; i < size_; i++) {
			out << array_[i] << " ";
		}
	}

	void ReadSample() {
		int count = 0;

		cout << "Enter a size: ";
		cin >> size_;

		array_ = new Object[size_];

		for (unsigned int i = 0; i < size_; i++) {
			cout << "Enter element " << count + 1 << ": ";
			cin >> array_[i];
			count++;
		}
	}

	size_t Size() const {
		return size_;
	}

	Sample & operator+=(const Sample & rhs) {
		Sample temp(size_ + rhs.size_);

		for (unsigned int i = 0; i < size_; i++) {
			temp.array_[i] = array_[i];
		}

		int j = 0;
		for (unsigned int k = size_; k < size_ + rhs.size_; k++, j++) {
			temp.array_[k] = rhs.array_[j];
		}

		*this = temp;
		return *this;
	}

	Sample operator+(const Sample & rhs) const {
		Sample temp(*this);
		temp += rhs;
		return temp;
	}

	~Sample() { //destructor
		delete[] array_;
	}

private:
	size_t size_; 
	Object *array_;
};

template <typename Object>
ostream & operator<<(ostream & out, const Sample<Object> & rhs) {
	rhs.print(out);
	return out;
}


My main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
Sample<string> a, b;

	a.ReadSample(); 
	cout << a << endl;

	b.ReadSample(); 
	cout << b << endl; 

	cout << a + b << endl; 

	Sample<string> d = a + b;
	cout << d << endl;

	cout << d + "adding this" << endl;

        return 0;
}


My attempt of another overload for +:

1
2
3
4
5
6
7
8
9
Sample operator+(const Sample & lhs, const string theString) const {
		Sample temp(lhs.size_ + 1); 
		for (unsigned int i = 0; i < lhs.size_ + 1; i++) {
			temp.array_[i] = lhs.array_[i];
		}

		temp.array_[lhs.size_] = theString;
		return temp;
	}


... which produces the error in my compiler:
...operator+ const must take either zero or one argument


Is there any way to bypass this? I'm a little confused on how to proceed.
Last edited on
I tried playing around with this. I needed to make use of some code which was not supplied, so my code may have diverged from the original a little.

This was my version:
1
2
3
4
5
6
7
8
9
    Sample operator+(const Object & rhs) const {    
        Sample temp(size_ + 1);
        
        unsigned int i=0;
        for (  ; i<size_; i++)
            temp.array_[i] = array_[i];
        temp.array_[i] = rhs;
        return temp;
    }
Last edited on
Awesome man, the code worked. I see what I did wrong in my attempt.
Topic archived. No new replies allowed.