#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.
constint 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;
}
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 = newint[_size];
for (int i = 0; i < _size; i++)
{ m_data[i] = 0;
}
}
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