Question: #ifndef ARRAY_H_INCLUDED #define ARRAY_H_INCLUDED #include #include using namespace std; /* error codes 0 -- no error 1 -- non-positive size for constructor 2 -- invalid
#ifndef ARRAY_H_INCLUDED #define ARRAY_H_INCLUDED
#include #include
using namespace std;
/* error codes 0 -- no error 1 -- non-positive size for constructor 2 -- invalid index was used 4 -- non-postitive newSize for changeSize */
template class Array { public: Array(int size); ~Array(); Array(const Array & a); Array & operator = (const Array & a);
T & operator [] (int index); void changeSize (int newSize); int length(); string err();
private: T * els; int capacity; T dud; int errorCode; void deepCopy(const Array & a); };
template Array::Array(int size) { cout << "constructor" << endl; if (size < 1) { size = 1; errorCode = 1; } else { errorCode = 0; } els = new T[size]; capacity = size; }
template Array::~Array() { delete [] els; }
template Array::Array(const Array & a) { deepCopy(a); }
template Array & Array::operator = (const Array & a) { if (this == & a) return *this; delete [] els; deepCopy (a); return * this; }
template void Array::deepCopy (const Array & a) { capacity = a.capacity; errorCode = a.errorCode; els = new T[capacity]; for (int i = 0; i < capacity; i++) { els[i] = a.els[i]; } }
template T & Array::operator [](int index) { if (index < 0 || index >= capacity) { errorCode |= 2; return dud; } return els[index]; }
template void Array::changeSize(int newSize) { if (newSize < 0) { errorCode |= 4; return; } int numEls = (capacity > newSize ? newSize : capacity); T * temp = new T [newSize]; for (int i = 0; i < numEls; i++) { temp[i] = els[i]; } delete [] els; els = temp; capacity = newSize; }
template int Array::length() { return capacity; }
template string Array::err() { if (errorCode == 0) return "No error. "; string s = ""; if (errorCode & 1) s = s + "non-positive size for constructor. Size set to 1. "; if (errorCode & 2) s = s + "index out of range. "; if (errorCode & 4) s = s + "non-positive newSize for changeSize. Size not changed. "; return s; }
#endif // ARRAY_H_INCLUDED
#ifndef TWITTERARRAYHEADER_H_INCLUDED #define TWITTERARRAYHEADER_H_INCLUDED
using namespace std;
template class TwitterArray { public: TwitterArray(DT u); void AddFollower(DT p); void RemoveFollower(DT p); void PrintFollowers(); private: DT user; int numFollowers; //int capacity; TwitterArray followers; };
template TwitterArray::TwitterArray(DT u):followers(2) { user = u; numFollowers = 0; //capacity = 2; followers = new DT[2]; }
template void TwitterArray::AddFollower(DT p) { if (followers.Length() <= numFollowers) { followers.changeSize(followers.Length() * 2); return; } } template
void TwitterArray
::RemoveFollower(DT p) { int index = -1; for (int i = 0; i < numFollowers; i++) { if (followers[i] == p) { index = i; } } for (int i = index; i < numFollowers - 1; i++) { followers[i] = followers[i + 1]; } numFollowers --; }
template void TwitterArray::PrintFollowers() { for (int i = 0; i < numFollowers; i++) { cout << followers[i] << ", "; } cout << endl; }
#endif // TWITTERARRAYHEADER_H_INCLUDED
#include #include #include "TwitterArrayHeader.h" #include "Array.h"
using namespace std;
struct Profile { string userName; int age; string state;
bool operator == (Profile p2) { if (p2.userName == userName) return true; else return false; } };
ostream& operator << (ostream & output, Profile p) { output << p.userName; return output; }
int main() { TwitterArray user1 ("P1"); user1.AddFollower("P2"); user1.PrintFollowers(); user1.AddFollower("P3"); user1.PrintFollowers(); user1.AddFollower("P4"); user1.PrintFollowers(); user1.RemoveFollower("P3"); user1.PrintFollowers();
Profile up; up.userName = "UP1"; TwitterArray user2 (up); up.userName = "UP2"; user2.AddFollower(up); user2.PrintFollowers(); up.userName = "UP3"; user2.AddFollower(up); user2.PrintFollowers(); up.userName = "UP4"; user2.AddFollower(up); user2.PrintFollowers(); up.userName = "UP3"; user2.RemoveFollower(up); user2.PrintFollowers(); return 0; }
Trying to modify the AddFollower, RemoveFollower and PrintFollowers of the TwitterArrayHeader to use the ArrayHeader and run the functions, but keep getting an error. Any help would be greatly appreciated.