I am pretty sure I have this template set up correctly, I am not very good with classes or templates or operator overloading, so any help is greatly appreciated.
My assignment:
Use HW4 and use templates to allow your dynamic array to hold any type.
i will give you 3 files
one file will contain all ints (data5i.dat)
one file will contain all doubles (data5d.dat)
one file will contain all strings (data5s.dat)
you must be able to process the three files without changing your classes.
You will use your templates and pass the types via main.
The output will be the same. The output for + with a string is Concatenation so there should not be an issue.
Also, the last letter of the file name before the extension dictates the data type in the file.
You will need to pass the file to the program via the commandline.
i.e. Program.exe ddata5i.dat
Your program must handle ints, doubles, and floats given the letter before the extension.
#pragma once
#include <vector>
#include <iostream>
#include <time.h>
#include <fstream>
#include <string>
usingnamespace std;
template <class T>
class Collection
{
private:
vector<T> myVector; // Dynamic vector for ints
public:
Collection(); // Constructor
virtualvoid add(T value) = 0; //adds the value to the array
virtualvoid remove(T index) = 0; //removes and item from the appropriate index location
virtualvoid print() = 0; //prTs item of the array comma seperated.
virtual T get(T index) = 0; //gets item at a particular index.
virtual T sum() = 0; //gets the sum of the array
virtual T size() = 0; //gets the size of the array
//-----------------------------------------------------------------------------
Collection::Collection() // Create object
{
myVector.empty();
}
//-----------------------------------------------------------------------------
void Collection::add(T value) // Add value to Vector
{
myVector.push_back(value);
}
//-----------------------------------------------------------------------------
void Collection::remove(T index) // Removes value from Vector @ index
{
myVector.erase(myVector.begin() + index);
}
//-----------------------------------------------------------------------------
void Collection::print()
{
cout << "\n Printing List" << endl;
cout << "---------------" << endl << endl;
for(int i=0; i<myVector.size()-1; i++) // Printing out list til second to last
{
cout << myVector[i] << ", "; // Output
}
cout << myVector.back() << "." << endl; // Prints last item for proper format
}
//-----------------------------------------------------------------------------
T Collection::get(T index) // Returns value at index
{
return myVector[index];
}
//-----------------------------------------------------------------------------
T Collection::sum() // Adds all items in Vector together
{
T result = 0;
for(int i=0; i<myVector.size(); i++)
{
result = result + myVector[i];
}
return result;
}
//-----------------------------------------------------------------------------
T Collection::size() // Returns size of vector
{
return myVector.size();
}
//-----------------------------------------------------------------------------
};
derived.h:
[code]
#pragma once
#include "collections.h"
template <class T>
class Derived : public Collection<T>
{
private:
vector<T> myVector;
public:
Derived();
void add(T value); //adds the value to the array
void remove(T index); //removes and item from the appropriate index location
void print(); //prints item of the array comma seperated.
T get(T index); //gets item at a particular index.
T sum(); //gets the sum of the array
T size(); //gets the size of the array
//-----------------------------------------------------------------------------
Derived::Derived() // Default Constructor
{
}
//-----------------------------------------------------------------------------
void Derived::add(T value) // Add value to Vector
{
myVector.push_back(value);
}
//-----------------------------------------------------------------------------
void Derived::remove(T index) // Removes value from Vector @ index
{
myVector.erase(myVector.begin() + index);
}
//-----------------------------------------------------------------------------
void Derived::print()
{
cout << "\n Printing List" << endl;
cout << "---------------" << endl << endl;
for(int i=0; i<myVector.size()-1; i++) // Printing out list til second to last
{
cout << myVector[i] << ", "; // Output
}
cout << myVector.back() << "." << endl; // Prints last item for proper format
}
//-----------------------------------------------------------------------------
T Derived::get(T index) // Returns value at index
{
return myVector[index];
}
//-----------------------------------------------------------------------------
T Derived::sum() // Adds all items in Vector together
{
T result = 0;
for(int i=0; i<myVector.size(); i++)
{
result = result + myVector[i];
}
return result;
}
//-----------------------------------------------------------------------------
T Derived::size() // Returns size of vector
{
return myVector.size();
}
//-----------------------------------------------------------------------------
};
These are the errors I am getting. For all of the .h functions.
collections.h:69:19: error: explicit specialization of 'T Collection<T>::sum()'
must be introduced by 'template <>'
T Collection::sum() // Adds all items in Vector together
^
collections.h:82:3: error: extra qualification 'Collection<T>::' on member 'size
' [-fpermissive]
T Collection::size() // Returns size of vector
^
#ifndef COLLECTION_H_WAS_ALREADY_INCLUDED
#define COLLECTION_H_WAS_ALREADY_INCLUDED
#include <cstddef> // for std::size_t
// avoid gratituous #includes
template < class T > class Collection // abstract class; so no implementations (pure interface)
{
public:
virtual ~Collection() = default ; // virtual destructor
// setters (modifiers) - not const
virtualvoid add( T value ) = 0; //adds the value to the array
virtualvoid remove( std::size_t index ) = 0; //removes and item from the appropriate index location
// getters (inspectors) - const
virtual T get( std::size_t index ) const = 0; //gets item at a particular index.
virtual T sum() const = 0; //gets the sum of the array
virtual std::size_t size() const = 0; //gets the size of the array
// operations, non-mutating, const
virtualvoid print() const = 0; //prTs item of the array comma seperated.
};
#endif // COLLECTION_H_WAS_ALREADY_INCLUDED
#ifndef DERIVED_H_WAS_ALREADY_INCLUDED
#define DERIVED_H_WAS_ALREADY_INCLUDED
#include "collection.h"
#include <vector>
#include <iostream>
// avoid using namespace directives at global scope in a in a header
template < class T > class Derived : public Collection<T> // implements the interface
{
private: std::vector<T> myVector ;
public:
virtualvoid add( T value ) override; //adds the value to the array
virtualvoid remove( std::size_t index ) override; //removes and item from the appropriate index location
virtualvoid print() const override; //prTs item of the array comma seperated.
virtual T get( std::size_t index ) const override; //gets item at a particular index.
virtual T sum() const override; //gets the sum of the array
virtual std::size_t size() const override; //gets the size of the array
};
template < class T > void Derived<T>::add( T value ) { myVector.push_back(value) ; }
template < class T > void Derived<T>::remove( std::size_t index )
{
if( index < myVector.size() ) myVector.erase( myVector.begin() + index ) ;
}
// ...
template < class T > std::size_t Derived<T>::size() const { return myVector.size() ; }
// etc.
#endif // DERIVED_H_WAS_ALREADY_INCLUDED