syntax: vector arguments as inputs to copy constructor

I would like to know how to initialise a copy constructor with vector arguments in general and for the following particular case.

I am currently developing a suite C++ code that:

1. Converts numbers from a file into their binary (or two's complement) equivalent plus a parity bit for error checking.

2. Concatenates the bits representing the individual word into a single array.

Although I have successfully achieved the concatenation by some other means (using a class member function), to help my continued education in C++, I would like to achieve a similar outcome using a copy constructor.

Here is the implementation of my header file:

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
#include <iostream>

using namespace std;

class word
{
public:
	word ();
	word (int);

	int DAC ();
	bool checkParityOK ();
	
	friend class message;

private:
	int firstBit;		// least significant bit (LSB)
	int secondBit;
	int thirdBit;
	int fourthBit;
	int fifthBit;
	int sixthBit;
	int seventhBit;
	int eighthBit;		// most significant bit (MSB)
	int parityBit;		// parity bit for error checking
};

class message: public word
{
public:
	message ();
	message (vector<word>);

	vector<int> binarySequence ();
};


I believe my problem is with the declaration of the copy constructor
message (vector<word>)
; but, I may well be wrong!

Here is the implementation of the copy constructor and a member function:

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
message::message(vector<word> theSampleWord)
{
	// Initialise empty vector for word
	vector<int> vectorSampleWord;
	
	// Define the counter variable
	int index;

	// Define iterator for sample word
	vector<word>::iterator sampleWordIter;

	// For the entire length of the Sample Word
	for (sampleWordIter = theSampleWord.begin(); sampleWordIter != theSampleWord.end(); sampleWordIter++)
	{
		// Show that message has been successfully initialised
		// Output the content all the words (I'd like them printed out on one line)
		cout << sampleWordIter->parityBit << sampleWordIter->eighthBit << sampleWordIter->seventhBit << sampleWordIter->sixthBit << sampleWordIter->fifthBit
			<< sampleWordIter->fourthBit << sampleWordIter->thirdBit << sampleWordIter->secondBit << sampleWordIter->firstBit << endl;

		// Include a line spacer between outputs
		cout << endl;

		// Call upon function that returns the cascaded bits of the message's word
		vectorSampleWord = binarySequence();
	};

	// Show the content of the vector that contains the cascaded bits of words
	cout << "vectorSampleWord contains the following elements:" << endl;  
	for (index = 0; index < vectorSampleWord.size(); ++index)
	{
		// Print out the entire content of vectorSampleWord on one line
		cout << vectorSampleWord.at(index);  
	}
};


vector<int> message::binarySequence ()
{
	// Initialise empty vector for word
	vector<int> vectorWord;
	
	// Use push_back() to append only the bits of word to the vector
	vectorWord.push_back(parityBit);
	vectorWord.push_back(eighthBit);
	vectorWord.push_back(seventhBit);
	vectorWord.push_back(sixthBit);
	vectorWord.push_back(fifthBit);
	vectorWord.push_back(fourthBit);
	vectorWord.push_back(thirdBit);
	vectorWord.push_back(secondBit);
	vectorWord.push_back(firstBit);
	
	return vectorWord;
};


Within the main code, I call the copy constructor using the following line of code:

1
2
3
4
5
6
// Create a new instance of message
	// Currently an error exists due to this line of code
	// it is suppose to call on the copy constructor "message (vector<word>)"
	// My feeling is that I have not declared and/or defined this copy constructor properly
	// But, there may be more problems than this
	message(theWord);


I hope that the details I have provided is sufficient to provide enough insight into what I am doing and, hopefully, why things are going wrong. Please, request for more information if this is not the case.
Last edited on
In the copy constructor i suppose you can only initialize the variabls of the class . you have to declare and define different function for the opration you want to perform .

xxx
Hi,

Thanks for your reply. I guess I'll stick with doing it that way i.e. defining a different function:

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
vector<int> message::binarySequence (vector<word> theSampleWord)
{
	// Define the counter variable
	int count;
	
	// initialise the vector for word storage
	vector<int> vectorWord;

	// Initialise the iterator for the class word
	vector<word>::iterator wordIter;

	// For the entire length of the Sample Word
	for (wordIter = theSampleWord.begin(); wordIter != theSampleWord.end(); wordIter++)
	{
		// Output the content all the words (I'd like them printed out on one line)
		cout << wordIter->parityBit << wordIter->eighthBit << wordIter->seventhBit << wordIter->sixthBit << wordIter->fifthBit
			<< wordIter->fourthBit << wordIter->thirdBit << wordIter->secondBit << wordIter->firstBit << endl;

		// Include a line spacer between outputs
		cout << endl;

		// Use push_back() to append only the bits of word to the vector
		vectorWord.push_back(wordIter->parityBit);
		vectorWord.push_back(wordIter->eighthBit);
		vectorWord.push_back(wordIter->seventhBit);
		vectorWord.push_back(wordIter->sixthBit);
		vectorWord.push_back(wordIter->fifthBit);
		vectorWord.push_back(wordIter->fourthBit);
		vectorWord.push_back(wordIter->thirdBit);
		vectorWord.push_back(wordIter->secondBit);
		vectorWord.push_back(wordIter->firstBit);
	}

	// Show the content of the vector that contains the cascaded bits of words
	cout << "vectorSampleWord contains the following elements:" << endl;  
	for (count = 0; count < vectorWord.size(); ++count)
	{
		// Print out the entire content of vectorSampleWord on one line
		cout << vectorWord.at(count);  
	}
	
	return vectorWord;
};
Topic archived. No new replies allowed.