Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help working with my template class in C++. Basically it is returning errors saying that it is unable to convert certain data types

I need help working with my template class in C++. Basically it is returning errors saying that it is unable to convert certain data types to other data types. I have no idea how to fix this. There are comments on the code that will tell you what each function is supposed to do.

main.cc

//////////////////////////////////////////////

#include

#include

#include "tarray.h"

using namespace std;

int main(){

Tarray nums;

Tarray words;

nums.add(rand());

nums.add(rand());

nums.add(rand());

nums.add(rand());

for(nums.start(); nums.is_item(); nums.advance())

cout<

words.add("CS");

words.add("is");

words.add("the");

words.add("bestest");

words.add("major");

words.add("at");

words.add("Ohio");

words.add("University");

words.start();

while(words.is_item() && words.current() != "major")

words.advance();

words.remove_current();

for(words.start(); words.is_item(); words.advance())

cout<

return 0;

}

tarray.h

///////////////////////////////////////////

#include

template

class Tarray{

public:

// Constructor makes an object with a dynamic array of size 5

Tarray();

// This function puts the item into the next available spot in the

//array.

// If the array is full, resize is called.

void add(T item);

// iterator functions

// places the current_index at the beginning of the array

void start();

// returns true if the current index is less than used

bool is_item()const;

// moves current index to the next array location

void advance();

// returns the array item being referenced by cuurent index

T current()const;

// removes the item at the current index spot

void remove_current();

private:

void resize();

T *data; // pointer to the dynamic array

std::size_t capacity;

std::size_t used;

std::size_t current_index;

};

#include "tarray.hpp" // This is how we hook this to the implementation file for templates:

tarray.hpp

//////////////////////////////////////////

#include

using namespace std;

template

Tarray::Tarray(){

size_t capacity = 5;

size_t used = 0;

size_t current_index = 0;

}

template

void Tarray::add(T item){

if (used >= capacity - 1)

resize();

for (int i = used; i >= current_index; i--){

data[i + 1] = data[i];

}

data[current_index] = item;

used++;

}

template

void Tarray::start(){

current_index = 0;

}

template

bool Tarray::is_item()const{

return current_index <= used;

}

template

void Tarray::advance(){

if (current_index == used - 1){

cout << "End of the list ";

return;

}

else

current_index++;

}

template

T Tarray::current()const{

return data[current_index];

}

template

void Tarray::remove_current(){

if (current_index < used){

for (int i = current_index; i < used; i++){

data[i] = data[i+1];

}

used--;

if (current_index != 0)

current_index--;

}

else{

used--;

current_index--;

}

}

template

void Tarray::resize(){

Tarray *tmp;

tmp = new Tarray[capacity + 5];

copy(data, data + used, tmp);

delete [] data;

data = tmp;

capacity += 5;

}

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

Students also viewed these Databases questions

Question

Evaluating Group Performance?

Answered: 1 week ago

Question

Azure Analytics is a suite made up of which three tools?

Answered: 1 week ago

Question

Finding and scheduling appointments with new prospective clients.

Answered: 1 week ago