templates help

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.


Driver:
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
97
98
#include "Derived.h"
#include <sstream>

using namespace std;

const int Error = 1; // Error return

//-----------------------------------------------------------------------------

int main(int argc, char *argv[])
{

if(argc != 2)	// Usage statement
 {
  cout << "\nUsage: " << argv[0] << " 'File Name 1'" <<  endl;
  cin.ignore();
  cin.get();
  return(Error);
 }

 ifstream input_File(argv[1] , ios::in); // Open file
 
 if(input_File)
  {
   cout << "Input File is open" << endl;
  }
 else 
  {
   cout << "\n\nInput File not open... Exiting..." << endl;
   return(Error);
  }

//-----------------------------------------------------------------------------

 if(argv[1] == "data5s.dat") 
  {
   string line;
   float num;
   vector<float> numbers;

   Collection<float> * Record_1; // Pointer for access
   Record_1 = new Derived<float>;

   while(getline(input_File, line)) // Getting lines from file
    {
     istringstream(line) >> num; // Converting string
     Record_1-> add(num); 
    }

   Record_1-> print(); // Outputs Class Vector
   cout << "\nSum of list: " << Record_1-> sum();
   cout << "\nSize of list: " << Record_1-> size();
  }
 else if(argv[1] == "data5i.dat")
  {
   string line;
   int num;
   vector<int> numbers;

   Collection<int> * Record_1; // Pointer for access
   Record_1 = new Derived<int>;

   while(getline(input_File, line)) // Getting lines from file
    {
     istringstream(line) >> num; // Converting string
     Record_1-> add(num); 
    }

   Record_1-> print(); // Outputs Class Vector
   cout << "\nSum of list: " << Record_1-> sum();
   cout << "\nSize of list: " << Record_1-> size();
  }
 else if(argv[1] == "data5d.dat")
  {
   string line;
   double num;
   vector<double> numbers;

   Collection<double> * Record_1; // Pointer for access
   Record_1 = new Derived<double>;

   while(getline(input_File, line)) // Getting lines from file
    {
     istringstream(line) >> num; // Converting string
     Record_1-> add(num); 
    }

   Record_1-> print(); // Outputs Class Vector
   cout << "\nSum of list: " << Record_1-> sum();
   cout << "\nSize of list: " << Record_1-> size();
  }

//-----------------------------------------------------------------------------

cin.ignore(); // Pause
cin.get();
return 0; // Success
}
Last edited on
collections.h:

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
#include <vector>
#include <iostream>
#include <time.h>
#include <fstream>
#include <string>

using namespace std;

template <class T> 
class Collection
{
 private:
   vector<T> myVector; // Dynamic vector for ints

 public:
   Collection(); // Constructor
	virtual void add(T value) = 0; //adds the value to the array
	virtual void remove(T index) = 0; //removes and item from the appropriate index location
	virtual void 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();
}

//-----------------------------------------------------------------------------   

};

[/code]
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
   ^
Last edited on
collection.h
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
#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
        virtual void add( T value ) = 0; //adds the value to the array
        virtual void 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
        virtual void print() const = 0; //prTs item of the array comma seperated.
};

#endif // COLLECTION_H_WAS_ALREADY_INCLUDED 



derived.h
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
#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:
        virtual void add( T value ) override; //adds the value to the array
        virtual void remove( std::size_t index ) override; //removes and item from the appropriate index location
        virtual void 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 
Topic archived. No new replies allowed.