Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello. I am trying to implement an associative 2d array. It should be able to do duplication check and count how many of them. However,

Hello. I am trying to implement an associative 2d array. It should be able to do duplication check and count how many of them.

However, it keep giving me an error says "no match for 'operator[]' (operand types are ...)".

Here is my header file and main CPP file so please let me know which part I did wrong.

\

\

\

\

Header:

#ifndef AssociativeArray_h #define AssociativeArray_h

#include #include "Queue.h" using namespace std;

template

class AssociativeArray

{

struct Node{

V VValues = V();

K KValues = K();

bool inUse = false;

};

int siz = 0;

V* values;

int cap = 2;

V dummy = V();

Node* NodeArray;

void capacity(int capa){

Node* temp = new Node[capa];

for (int i = 0; i < cap; i++){

temp[i] = NodeArray[i];

}

for (int i = cap; i < capa; i++){

temp[i].inUse = false;

temp[i].VValues = V();

temp[i].KValues = K();

}

cap = capa;

delete[] NodeArray;

NodeArray = temp;

};

public:

AssociativeArray(){

NodeArray = new Node[cap];

for (int i = 0; i < cap; i++) {

NodeArray[i] = Node();

}

};

AssociativeArray(int cap){

this->cap = cap;

NodeArray = new Node[cap];

for (int i = 0; i < cap; i++) {

NodeArray[i] = Node();

}

};

AssociativeArray& operator=(const AssociativeArray& original){

if (this != &original)

{

delete[] NodeArray;

cap = original.cap;

siz = original.siz;

NodeArray = new Node[cap];

for (int i = 0; i < cap; i++)

NodeArray[i] = original.NodeArray[i];

dummy = original.dummy;

}

return *this;

};

AssociativeArray(const AssociativeArray& original){

cap = original.cap;

siz = original.siz;

NodeArray = new Node[cap];

for (int i = 0; i < cap; i++){

NodeArray[i] = original.NodeArray[i];

}

};

~AssociativeArray() { delete[] NodeArray; } //destructor

int capacity() const { return cap; }

V& operator[](const K& index) const {

if (index < 0) return dummy;

if (index >= cap) return dummy;

return NodeArray[index].VValues;

};

V& operator[](const K& index){

//search for match, + hole

for (int i = 0; i < cap; i++){

if (NodeArray[i].inUse&&NodeArray[i].KValues == index){

return NodeArray[i].VValues;

}

}

for (int i = 0; i < cap; i++){

if (!NodeArray[i].inUse){

//create the node

Node data;

data.KValues = index;

data.inUse = true;

data.VValues = V();

NodeArray[i] = data;

siz++;

return NodeArray[i].VValues;

}

}

//if no match and no hole

capacity(cap * 2);

Node data;

data.KValues = index;

data.inUse = true;

data.VValues = V();

siz++;

return NodeArray[cap].VValues;

};

V& operator[](int index){

if (index < 0) return dummy;

if (index >= cap) capacity(index * 2);

return NodeArray[index].VValues;

};

bool containsKey(const K& key)const {

for (int i = 0; i < cap; i++){

if (NodeArray[i].KValues == key && NodeArray[i].inUse){return true;}

}

return false;

};

void deleteKey(const K& key){

for (int i = 0; i < cap; i++){

if (NodeArray[i].KValues == key){NodeArray[i].inUse = false; siz--;}

}

};

int size()const {return siz;};

Queue keys(){

Queue AllStrings;

for (int i = 0; i < cap; i++){

if (NodeArray[i].inUse){AllStrings.push(NodeArray[i].KValues);};

}

return AllStrings;

};

};

#endif

\

\

\

main CPP

...

AssociativeArray > alreadySeen;

int duplicates;

ifstream fin; fin.open("dvc-sample.txt"); if (!fin.good()) throw "I/O error";

// read the input file while (fin.good()) {

string line; getline(fin, line); strcpy(buf, line.c_str());

if (buf[0] == 0) continue;

const string term(token = strtok(buf, tab)); const string section((token = strtok(0, tab)) ? token : ""); const string course((token = strtok(0, tab)) ? token : ""); const string instructor((token = strtok(0, tab)) ? token : ""); const string whenWhere((token = strtok(0, tab)) ? token : "");

if (course.find('-') == string::npos) continue; // invalid line: no dash in course name const string subjectCode(course.begin(), course.begin() + course.find('-'));

///duplication check if (alreadySeen[term][section]) /// duplicate increment <----- Here is where the error occurs. { duplicates++; } else { alreadySeen[term][section] = true; count[subjectCode][course]++; }

....

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions