Ok so I have this project. I have got the coding part down, but I am having a compiling Error that I do not understand. Please help!
Here is what I have to do:
Write the definition of an EZArray class to the following specification:
An EZArray contains strings.
All arrays are dynamic (do not declare arrays, but pointers to strings).
A function allocates memory space to the array.
A function deallocates the memory space.
A function assigns the string value in its parameter to all cells of the array.
A function assigns the string value in its first parameter to the cell indexed by its second parameter. If the index is out of bounds, the function should return 1. If it is in bounds, the function should return 0.
A function returns the string value of the cell indexed by its parameter. If the index is out of bounds, the function should return NULL.
A function copies the array passed as the parameter to the array field. If the array field is the same size or larger, pad with "" strings and return 0. If the array field is smaller, copy what fits and return 1. If the array field is NULL, return 1.
Create your class with .h and .cpp files.
Write a main.cpp file with your main function to test your class thoroughly.
Here are my Codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
/** EZArray.h file
**/
#include<iostream>
#include <string>
using namespace std;
typedef string* strPtr;
class EZArray {
public:
void printArray();
void setArraySize(int);//To set array size
int getArraySize();//to get array size
void allocate();
void deallocate();
void assignToAll(string);//To assign the string value
int assignByIndex(string, int);//Assign by index
string searchByIndex(int);
int arrayCopy(string arr[], int);
private:
strPtr array;
int arraySize;
};// end of class
|
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
|
#include <iostream>
#include <string>
using namespace std;
#include "EZArray.h"
typedef string* strPtr;
//printArray
void EZArray::printArray() {
for(int i=0; i<arraySize; i++) {
cout << array[i] << endl;
}// End of for
cout << "-----end-----" << endl;
}// End of printArray
/** set and get method for ArraySize
* */
void EZArray::setArraySize(int as) {
arraySize = as;
}// End of setArraySize
int EZArray::getArraySize() {
int gs = arraySize;
return gs;
}//End of getArraySize
/** To allocate and delocate memory
* */
void EZArray::allocate() {
array = new string[arraySize];
}// End of allocate
void EZArray::deallocate() {
delete [] array;
arraySize = 0;
}// End of deallocate
/** To assign the string value
* */
void EZArray::assignToAll(string as) {
for(int i=0; i<arraySize; i++) {
array[i] = as;
}// End of for
}// End assignToAll
/** To assign and search by index
* */
int EZArray::assignByIndex(string as, int i) {
int gs;
if(i>=arraySize || i<0) {
gs = 1;
}// End of if
else if(i<arraySize && i >=0) {
array[i] = as;
gs = 0;
}// End of if
return gs;
}// End of assignByIndex
//SearchByIndex
string EZArray::searchByIndex(int i) {
string gs;
if(i<arraySize && i>=0) {
gs = array[i];
}// End of if
else if(i<0 || i>=arraySize) {
return NULL;
}// End of else-if
return gs;
}// End of searchByIndex
//Array copy
int EZArray::arrayCopy(string arr[], int length) {
int gs;
if(length <= arraySize) {
for(int i=0; i<length; i++) {
array[i] = arr[i];
}// End of for
for(int i=length; i<arraySize; i++) {
array[i] = "";
}// End of for
gs = 0;
}// End of if
if(length > arraySize) {
for(int i=0; i<arraySize; i++) {
array[i] = arr[i];
}// End of for
gs = 1;
}// End of if
if(arr == NULL) {
gs = 1;
}// End of if
return gs;
}// End of arrayCopy
|
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
|
//main method
#include <iostream>
#include <string>
using namespace std;
#include "EZArray.h"
int main() {
int size;
EZArray EZ;
cout << "Enter the size of the array" << endl;
cin >> size;
EZ.setArraySize(size);
EZ.allocate();
cout << "size of array is: " << EZ.getArraySize() << endl;
EZ.assignToAll("Hello");
EZ.printArray();
EZ.assignByIndex("goodBye", 4);
EZ.printArray();
cout << "index 4 is: " << EZ.searchByIndex(4) << endl;
cout << "index 2 is: " << EZ.searchByIndex(2) << endl;
string* test;
test = new string[3];
for(int i = 0; i<3; i++) {
test[i] = "test";
}// End of for
int r = EZ.arrayCopy(test, 3);
cout << "return value: " << r << endl;
EZ.printArray();
EZ.deallocate();
EZ.printArray();
}// End of main method
|
And finally Here are Errors: I am pretty sure they are easy ones..but I don't understand them.
/tmp/ccuH5XOx.o: In function `main':
hw5.cpp:(.text+0xc9): undefined reference to `EZArray::setArraySize(int)'
hw5.cpp:(.text+0xd4): undefined reference to `EZArray::allocate()'
hw5.cpp:(.text+0xdf): undefined reference to `EZArray::getArraySize()'
hw5.cpp:(.text+0x148): undefined reference to `EZArray::assignToAll(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
hw5.cpp:(.text+0x181): undefined reference to `EZArray::printArray()'
hw5.cpp:(.text+0x1e1): undefined reference to `EZArray::assignByIndex(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
hw5.cpp:(.text+0x21a): undefined reference to `EZArray::printArray()'
hw5.cpp:(.text+0x234): undefined reference to `EZArray::searchByIndex(int)'
hw5.cpp:(.text+0x2b6): undefined reference to `EZArray::searchByIndex(int)'
hw5.cpp:(.text+0x438): undefined reference to `EZArray::arrayCopy(std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)'
hw5.cpp:(.text+0x47b): undefined reference to `EZArray::printArray()'
hw5.cpp:(.text+0x486): undefined reference to `EZArray::deallocate()'
hw5.cpp:(.text+0x491): undefined reference to `EZArray::printArray()'
collect2: ld returned 1 exit status
|
Please Help!