Class type redefinition error. How can I fix this?

This is 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
#ifndef UArray_H
#define UArray_H

#include <iostream>
using namespace std;

class UArray
{
	public:
		UArray(int _size);
		UArray(int _a[], int _size);
		UArray(const UArray& _a);
		~UArray();

		void push_back(int _value);
		void setValue(int _value, int _index);
		int getValue(int _index);
		int at(int _index);
		int getIndex(int _value);
		void erase(int _index);
		void eraseByValue(int _value);
		int getSize();
		void print();

	private:
	int m_size;
	int* m_data;
};
#endif 


This is the main.cpp 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
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
#include "UArray.h"

class UArray
{
	public:
		UArray(int _size)
		{
			m_size = _size;
			m_data = new int[_size];

			for (int i = 0; i < _size; i++)
			{
				m_data[i] = 0;
			}
		}

		UArray(int _a[], int _size)
		{
			m_size = _size;
			m_data = new int[_size];

			for (int i = 0; i < _size; i++)
			{
				m_data[i] = _a[i];
			}
		}

		UArray(const UArray& _a)
		{
			m_size = _a.m_size;
			m_data = new int[m_size];

			for (int i = 0; i < m_size; ++i)
			{
				m_data[i] = _a.m_data[i];
			}
		}

		~UArray()
		{
			std::cout << "Desructor is called" << std::endl;
		}

		void push_back(int _value)
		{
			int *temp = new int[m_size + 1];

			for (int i = 0; i < m_size; ++i)
			{
				temp[i] = m_data[i];
			}

			delete[] m_data;

			int * m_data = new int[m_size + 1];

			for (int i = 0; i < m_size + 1; ++i)
			{
				m_data[i] = temp[i];
			}

			m_data[m_size] = _value;
			m_size++;
		}

		void setValue(int _value, int _index)
		{
			m_data[_index] = _value;
		}

		int getValue(int _index)
		{
			int temp;
			temp = m_data[_index];
			return temp;
		}

		int at(int _index)
		{
			return getValue(_index);
		}

		int getIndex(int _value)
		{
			for (int i = 0; i < m_size; ++i)
			{
				if (m_data[i] == _value)
				{
					return i;
				}
			}
		}

		void erase(int _index)
		{
			int *temp = new int[m_size - 1];

			for (int i = _index; i < m_size - 1; ++i)
			{
				m_data[i] = m_data[i + 1];
			}
			--m_size;

			for (int i = 0; i < m_size; ++i)
			{
				temp[i] = m_data[i];
			}
			delete[] m_data;

			int *m_data = new int[m_size];
			for (int i = 0; i < m_size; ++i)
			{
				m_data[i] = temp[i];
			}

			delete[]temp;
		}

		void eraseByValue(int _value)
		{
			int _index = 0;
			for (int i = 0; i < m_size; ++i)
			{
				if (m_data[i] == _value)
				{
					_index = i;
					break;
				}
			}

			int *temp = new int[m_size - 1];
			for (int i = _index; i < (m_size - 1); ++i)
			{
				m_data[i] = m_data[i + 1];
			}
			--m_size;

			for (int i = 0; i < m_size; ++i)
			{
				temp[i] = m_data[i];
			}
			delete[] m_data;

			int *m_data = new int[m_size];
			for (int i = 0; i < m_size; ++i)
			{
				m_data[i] = temp[i];
			}
			delete[]temp;
		}

		int getSize()
		{
			return m_size;
		}

		void print()
		{
			for (int i = 0; i < m_size; ++i)
			{
				cout << m_data[i] << endl;
			}
		}
	private:
		int m_size;
		int* m_data;
};


This is the
int main()
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "UArray.h"

int main()
{
	// # 1
	// creating an array with 10 elements
	UArray a(10);
	// add 11 value to the array
	a.push_back(11);
	// add 12 value to the array
	a.push_back(12);
	// add 13 value to the array
	a.push_back(13);
	a.push_back(14);
	a.push_back(15);
	// print out the current array size and the element.
	// Note: array size should be 15 at this point.
	a.print();
	std::cout << "array index of value 4 is " << a.getIndex(4) << std::endl;
	std::cout << "value at index # 5 is " << a.getValue(5) << std::endl;
	// removing 13th element of the array.
	a.erase(13);
	// print out the current array size and the element.
	// Note: array size should be 14 at this point.
	a.print();
	std::cout << "array index of value 4 is " << a.getIndex(4) << std::endl;
	std::cout << "value at index # 5 is " << a.getValue(5) << std::endl;
	// # 2
	// creating a static array of size 6.
	const int arr_size = 6;
	int arr[arr_size] = { 23, 34, 45, 565, 455, 32 };
	// constructing UArray with the already created array.
	UArray b(arr, 6);
	// setting 555 value at index # 2 (3rd element).
	b.setValue(555, 2);
	// print out the current array size and the element.
	// Note: array size should be 6 at this point.
	b.print();
	// # 3
	// copying an right hand sided(rhs) array into left handed size(lhs)
	// UArray copy_array(b);
	// OR
	UArray copy_array = b;
	// print out the current array size and the element.
	// Note: array size should be 6 at this point.
	copy_array.print();
	// removing 2nd element of the array.
	copy_array.eraseByValue(455);
	// print out the current array size and the element.
	// Note: array size should be 5 at this point.
	copy_array.print();
	// print out the current array size and the element.
	// Note: array size should be 6 at this point.
	b.print();
	std::cout << "value at index # 0 is " << b.at(0) << std::endl;
	std::cout << "value at index # 1 is " << b.at(1) << std::endl;
	return 0;
}


Im getting an error in main.cpp file at line 4.
Last edited on
This is the main.cpp file: (snippet 2)

No, that should be your uarray.cpp file.

Second snippet line 3: You're creating another class declaration for UArray, which is why the compiler is complaining. In a separately compiled .cpp file, your functions should look like this:

1
2
3
4
5
6
7
8
9
//delete lines 3-5, 164-167
UArray::UArray(int _size)  //  Note the UArray:: scope qualifier in front of every function in your class
{   m_size = _size;
    m_data = new int[_size];

    for (int i = 0; i < _size; i++)
    { m_data[i] = 0;
    }
}

Last edited on
I fixed the code like you said. But the program still wont run.

Visual Studio 2015 gives an error sayingDebug Assertion Failed.

And Dec-C++ says C:\Users\123\AppData\Local\Temp\cc6EpAio.o task1.cpp:(.text+0x23): undefined reference to `UArray::UArray(int)' for almost every function
Last edited on
Visual Studio 2015 gives an error saying Debug Assertion Failed.

This is actually a segmentation error.
How can it be fixed?
closed account (48T7M4Gy)
http://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault
Topic archived. No new replies allowed.