C2440 error in BitArray template class

Ok so I've spent quite a bit on this and I'm at a loss as to what to do. I've gone over my code and everything looks to be fine. This is currently incomplete. Anyways does anyone have any clues as to how to fix these two errors:

'return' : cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
'initialize' : cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'

This error pops up in my each of my shift operations and in my slice function (depending on what test function is being called)Any ideas would be appreciated
The lines that end with an E are the ones that get the error
tbitarray.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
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
163
164
165
166
167
168
169
// tbitarray.cpp: A cursory test for the BitArray class
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include "bitarray.h"
#include "test.h"
using namespace std;

// Test program
int main() {
	// Test exceptions
	BitArray<> b;
	//throw_(b[0], logic_error);
	//throw_(b.toggle(0), logic_error);
	//const BitArray<> b1{ b }; // Test copy constructor
	//throw_(b1[0], logic_error);

	// Test empty Bitarray properties
	test_(b.size() == 0);
	test_(b.count() == 0);
	test_(b.capacity() == 0);
	test_(!b.any());

	// Validate construction and to_string()
	BitArray<> b2{ 5 };
	test_(b2.size() == 5);
	for (size_t i = 0; i < 5; ++i) {
		test_(!b2[i]);
	}
	test_(b2.to_string() == "00000");

	// Test copy, assign, equality, and from_string
	BitArray<> b3{ b2 };
	test_(b2 == b3);
	test_(b != b2);
	test_(!b3[2]);
	b3[2] = 1;
	test_(b3[2]);
	test_(b2 != b3);
	test_(b2.to_string() == "00000");
	b = b2;
	test_(b.to_string() == "00000");

	// Test move operations
	BitArray<> b4{ move(b3) };
	test_(b4[2]);
	BitArray<> b4b;
	b4b = move(b4);
	test_(b4b[2]);

	// Test bit ops
	BitArray<> x{ "011010110" }; // Also tests string constructor
	test_(x.count() == 5);
	test_(x.any());
	//test_((x << 6).to_string() == "110000000");	E
	//test_((x >> 6).to_string() == "000000011");	E
	//test_((x <<= 3).to_string() == "010110000");	E
	//test_((x >>= 3).to_string() == "000010110");	E
	//BitArray<> y{ ~x };							E
	nothrow_(x.toggle());
	//test_(x == y);
	//test_(x.to_string() == "111101001");

	b = BitArray<>{};
	test_(!b.any());
	b += 1;
	b += 0;
	b += 1;
	test_(b.to_string() == "101");
	test_(b.any());

	b2 = BitArray<>{ "10101011" };
	test_(b2.count() == 5);
	b2.toggle();
	test_(b2.to_string() == "01010100");
	//b2.erase(3);
	//test_(b2.to_string() == "0100100");
	//b2.erase(b2.size() - 1);
	//test_(b2.to_string() == "010010");
	//b2.erase(1, 4);
	//test_(b2.to_string() == "00");
	//b2.insert(1, 1);
	//test_(b2.to_string() == "010");
	//b2.insert(1, 0);
	//test_(b2.to_string() == "0010");
	//b2 += b;
	//test_(b2.to_string() == "0010101");
	//b2.insert(3, b);
	//test_(b2.to_string() == "0011010101");

	ostringstream os;
	os << "101" << 'a' << "0101";
	istringstream is{ os.str() };
	b3 = BitArray<>{};
	is >> b3;
	test_(b3.to_string() == "101");
	is.get();
	is >> b3;
	test_(b3.to_string() == "0101");
	os.str("");
	os << 'a';
	istringstream is2{ os.str() };
	is2 >> b3;
	test_(!is2);
	test_(b3.to_string() == "0101");

	BitArray<> b5{ "11111111111111111111111111000000000000000000000000000011" };
	//test_(b5.slice(23, 10) == BitArray<>("1110000000"));	E
	//size_t n = b2.size();
	//b2.insert(3, b5);
	//test_(n + b5.size() == b2.size());
	//b2.erase(3, b5.size());
	b2.shrink_to_fit();

	//// Test comparisons
	BitArray<> b6{ "10101" };
	BitArray<> b7{ "101010" };
	BitArray<> b8{ b7 };
	test_(b6 < b7);
	test_(b6 <= b7);
	test_(b6 <= b6);
	test_(b7 > b6);
	test_(b7 >= b6);
	test_(b7 >= b7);
	test_(BitArray<>("111") > BitArray<>("10111"));

	BitArray<> b9{ "11111111111111111111111111000000000000000000000000000011" };
	ostringstream ostr;
	ostr << b9;
	test_(ostr.str() == "11111111111111111111111111000000000000000000000000000011");
	test_(b9.count() == 28);
	//nothrow_(b9 <<= 2);									E
	//test_(b9.count() == 26);
	//nothrow_(b9 >>= 33);									E
	//test_(b9.count() == 23);
	BitArray<> b10{ "01" };
	//b9[0] = b10[0] = b10[1];
	//test_(b10[0]);
	//test_(b9[0]);
	const BitArray<> b11{ b10 };
	//test_(b11[0]);

	//BitArray<> b12("11011111101");
	//b12.erase(1, 8);
	//test_(b12.to_string() == "101");
	//b12 += b12;
	//test_(b12.to_string() == "101101");

	BitArray<> b13("");
	test_(b13.size() == 0);

	report_();
	cin.get();
}

/* Output (should have at least 4 move assignments):
move constructor
move assignment
move assignment
move assignment
move assignment
move assignment
move assignment
shrinking from 2 to 1 words   (numbers may vary)
Test Report:
Number of Passes = 69
Number of Failures = 0
*/
Last edited on
bitarray.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef BIT_ARRAY_H
#define BIT_ARRAY_H

#include <vector>
#include <climits>
#include <stdexcept>
#include <string>
#include <math.h>
#include <iostream>
#include <utility>

using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
using std::logic_error;

template<class IType = size_t>
class BitArray
{
private:
	vector<IType> Bit_Vector;
	size_t _count;
	enum { BITS_PER_WORD = CHAR_BIT* sizeof(IType) };
	bool test(size_t pos) const
	{
		return Bit_Vector[pos / BITS_PER_WORD] & (1u << (pos%BITS_PER_WORD));
	}
	void reset(size_t pos) 
	{
		Bit_Vector[pos / BITS_PER_WORD] &= ~(1u << (pos%BITS_PER_WORD));
	}		
	void set(size_t pos)
	{
		Bit_Vector[pos / BITS_PER_WORD] |= (1u << (pos % BITS_PER_WORD));
	}		
	void copy(size_t pos, bool value) 
	{
		if (value)
			set(pos);
		else
			reset(pos);
	}
	bool readBit(size_t pos)const
	{
		size_t block = pos / BITS_PER_WORD;
		size_t offset = pos % BITS_PER_WORD;
		if (pos >= _count)
			throw logic_error("Index given is out of range.");
		else
			return Bit_Vector[block] & (1u << offset);
	}
	bool assignBit(size_t pos, bool value)
	{
		size_t block = pos / BITS_PER_WORD;
		size_t offset = pos % BITS_PER_WORD;
		if (value)
			Bit_Vector[block] |= (1u << offset);
		else
			Bit_Vector[block] &= ~(1u << offset);
	}
public:
	// Object Management
	explicit BitArray(size_t bitCount = 0)
	{
		_count = bitCount;
		if (bitCount)
			Bit_Vector.resize((bitCount - 1) / BITS_PER_WORD + 1);
	};
	explicit BitArray(const string& bit_string)
	{		
		_count = bit_string.length();
		if (_count)
			Bit_Vector.resize((_count - 1) / BITS_PER_WORD + 1);
		for (size_t i = 0; i < _count; ++i)
		{
			if (bit_string[i] == '1')
			{
				set(i);
			}
			else if (bit_string[i] == '0')
			{
				reset(i);
			}
			else
				throw runtime_error("0 or 1 not found");
		}
	};
	explicit BitArray(const BitArray& b) = default;		// Copy constructor
	BitArray& operator=(const BitArray& b) = default;	// Copy assignment
	explicit BitArray(BitArray&& b) noexcept : Bit_Vector(b.Bit_Vector)	// Move assignment
	{
		cout << "move constructor" << endl;
		_count = b._count;
		//Bit_Vector = b.Bit_Vector;
		b._count = 0;
		b.Bit_Vector.clear();
	};	
	BitArray& operator=(BitArray&& b) noexcept
	{
		cout << "move assignment" << endl;
		swap(_count, b._count);
		swap(Bit_Vector, b.Bit_Vector);
		return *this;
	};
	size_t capacity() const			
	{
		return (BITS_PER_WORD * Bit_Vector.size());
	};			
	// Append a bit
	BitArray& operator+=(bool bit)
	{
		//2 options space/no space
		if (capacity() == _count)
			Bit_Vector.resize(Bit_Vector.size() + 1);
		_count++;
		//put bit in last spot
		copy(_count - 1, bit);
		return *this;
	}
	//** Append a BitArray
	BitArray& operator+=(const BitArray& b)
	{
		// Add BitArray obj starting from the last spot of the current BitArray
		if (capacity() == _count)
			Bit_Vector.resize(Bit_Vector.size() + Bit_Vector.size());
		//Append BitArray obj to the end
		for (int i = 0; i < _count; i++)
		{
			copy(_count-1, b[i]);
			_count++;
		}
		return *this;
	}	
	//** Remove “nbits” bits at a position
	void erase(size_t pos, size_t nbits = 1)
	{
		????Bit_Vector[pos/BITS_PER_WORD] = nbits/BITS_PER_WORD;
	}	
	//** Insert a bit at a position (slide "right")
	void insert(size_t pos, bool value)
	{
		
	}	
	//** Insert an entire BitArray object
	void insert(size_t pos, const BitArray& bitArray)
	{

	}
	// Discard unused, trailing vector cells
	void shrink_to_fit()
	{
		if (!Bit_Vector[Bit_Vector.size() - 1])	//ask if it's a 0
		{
			Bit_Vector.resize((_count - 1) / BITS_PER_WORD + 1);
		}
	}	
	// Bitwise ops
	class bitproxy
	{
		BitArray* b;
		int pos;
	public:
		bitproxy(BitArray* bs, int p) : b(bs) {
			pos = p;
		}
		bitproxy& operator=(bool bit) {
			if (bit)
				b->Bit_Vector[pos/BITS_PER_WORD] |= (1u << (pos%BITS_PER_WORD));
			else
				b->Bit_Vector[pos / BITS_PER_WORD] &= ~(1u << (pos%BITS_PER_WORD));
			return *this;
		}
		operator bool() const {
			return b->Bit_Vector[pos / BITS_PER_WORD] & (1u << (pos%BITS_PER_WORD));
		}
	};
	friend class bitproxy;
	bitproxy operator[](size_t pos)
	{
		if (pos >= _count)
			throw logic_error("Invalid Index.");
		return bitproxy(this, pos);
	}
	bool operator[](size_t pos) const
	{
		if (pos>=_count)
			throw logic_error("Invalid Index.");
		else
			return test(pos);
	}
	// Toggles all bits
	void toggle(size_t pos)
	{
		if (test(pos))
			reset(pos);
		else
			set(pos);
	}	
	void toggle()	// toggles all bits
	{
		for (size_t i = 0; i < _count; ++i)
		{
			if (test(i))
				reset(i);
			else
				set(i);
		}
	}
	BitArray operator~() const
	{
		BitArray<> bitArray(*this);
		bitArray.toggle();
		return bitArray; // //ERROR :C2440	'return': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
	}
	// Shift operators…
	BitArray operator<<(unsigned int sLeft) const
	{
		BitArray<> bitArray(*this);
		return bitArray <<= sLeft;	//ERROR :C2440	'return': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'

	}
	BitArray operator>>(unsigned int sRight) const
	{
		BitArray<> bitArray(*this);
		return bitArray >>= sRight;	//ERROR :C2440	'return': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
	}
	BitArray& operator<<=(unsigned int n) 
	{
		BitArray<> temp1 = slice(n, _count - n); //ERROR :C2440	'initializing': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
		BitArray<> temp2(n);
		temp1 += temp2; 
		*this = temp1;
		return *this;
	}	
	BitArray& operator>>=(unsigned int n)
	{
		BitArray<> temp1(n); 
		BitArray<> temp2 = slice(n, _count - n); //Error C2440	'initializing': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
		temp1 += temp2;
		*this = temp1;
		return *this;
	}
	
	BitArray slice(size_t pos, size_t count) const
	{
		string bitstr = (*this).to_string();
		BitArray b{ bitstr.substr(pos, count) };
		return b;		//Error C2440	'initializing': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
	}
	
	bool operator==(const BitArray& checkBits) const
	{
		return !((*this) < checkBits || checkBits > (*this));
	}	
	bool operator!=(const BitArray& checkBits) const
	{
		return ((*this) < checkBits || checkBits > (*this));
	}	
	bool operator<(const BitArray& checkBits) const
	{
		if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) < 0)
			return true;
		else
			return false;
	}
	bool operator<=(const BitArray& checkBits) const
	{
		if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) <= 0)
			return true;
		else
			return false;
	}	
	bool operator>(const BitArray& checkBits) const
	{
		if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) > 0)
			return true;
		else
			return false;
	}	
	bool operator>=(const BitArray& checkBits) const
	{
		if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) >= 0)
			return true;
		else
			return false;
	}
	size_t size() const 
	{
		return _count;
	}
	size_t count() const
	{
		size_t one_bits = 0;
		for (size_t i = 0; i < _count; ++i)
		{
			if (test(i))
				one_bits++;
		}
		return one_bits;
	}	
	bool any() const
	{
		//for loop through vector
		for (size_t i = 0; i < Bit_Vector.size(); i++)
		{
			if (Bit_Vector[i])
				return true;
		}
		return false;
	}
	friend ostream& operator<<(ostream& ostrm, const BitArray& _bitarray)
	{
		ostrm << _bitarray.to_string();
		return ostrm;
	}
	friend istream& operator>>(istream& istrm, BitArray& _bitarray)
	{
		string bitStr = "";
		char charChk;
		char charNext = 0;
		while (istrm >>charChk)
		{
			if (!isdigit(charChk))
			{
				charNext = -1;
				charNext = istrm.peek();
				istrm.unget();
				istrm.setstate(std::ios::failbit);
				break;
			}
			else
			{
				bitStr.push_back(charChk);
			}
		}
		if (charNext == -1)
			return istrm;
		istrm.clear();
		BitArray<> ba(bitStr);
		_bitarray = ba;
		return istrm;
	}
	string to_string() const 
	{
		string charStr = "";
		for (size_t i = 0; i < _count; i++)
		{
			if(test(i))
				charStr += '1'; //adding a 1
			else
				charStr += '0';	//adding a 0
		}
		return charStr;
	}
};
1
2
BitArray<> temp1 = slice(n, _count - n); 
return bitArray <<= sLeft;
You disabled ability to perform copy initialization and to do implicit conversion yourself by declaring copy constructor explicit. No wonder that those lines are rejected.

Hint: never declare copy constructor explicit. There is literally no reason to do so.
Topic archived. No new replies allowed.