Array error using template class

So, my only issue is I can't figure out how to assign the array in object Q to the 12 months, Dec through Jan. I then have to sort the array and display the strings from Jan to Dec. Any help or links would be appreciated. I have spent an ample amount of time searching the internet and have not found a solution to my problem. Thank you!


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
//
//  main.cpp
//  Question1_Project9
//
//  Created by Tyler Reymer on 11/5/14.
//  Copyright (c) 2014 Tyler Reymer. All rights reserved.
//

#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <algorithm>

using namespace std;

// Declare template class
template <class T, int n>
class TWO
{
private: T a[n];
public:
    void ReadData();
    void ReadData(string months[12]);// Read data into array a
    void DisplayData();
    void SortArray();      // Display array a
    ~TWO();
};

// Reads data into arrays for objects P and Q
template <class T, int n>
void TWO<T, n>::ReadData()
{
           int random = 0;
            for (int i = 0; i < n; i++)
            {
                // Generates 10 random numbers below 20
                random = rand() % 20;
                a[i] = random;
            }
    
}
template <class T, int n>
void TWO<T, n>::ReadData(string months[12])
{
        for(int i = 0; i < n; ++i)
        {
            a[i] = months[i];
            
        }
    
}

// Displays both objects
template <class T, int n>
void TWO<T, n>::DisplayData()
{
    for (int i = 0; i < n; ++i)
    {
        cout << a[i] << ' ';
    }
    cout << endl;
    
}

// Sorts both arrays
template <class T, int n>
void TWO<T, n>::SortArray()
{
    sort(a, a + n);
}

template <class T, int n>
TWO<T, n>::~TWO()
{}

int main()
{
    string b[12] = {"Dec", "Nov", "Oct", "Sept", "Aug", "Jul", "Jun", "May", "Apr", "Mar", "Feb", "Jan"};
    // Create objects
    TWO <string, 12> Q;
    TWO <int, 10> P;
    
    
    // Seed time
    srand(time(NULL));
    
    // Call read data function
    P.ReadData();
    Q.ReadData(b);
    
    // Output arrays
    cout << "Array's P and Q: " << endl;
    P.DisplayData();
    Q.DisplayData();
    
    // Sort arrays
    P.SortArray();
    Q.SortArray();
    cout << endl;
    
    // Display sorted arrays
    cout << "Sorted array's P and Q: " << endl;
    P.DisplayData();
    Q.DisplayData();
    
    // Pause cmd window
    system("PAUSE");
    
    // Terminate
    return 0;
}
Last edited on
I don't think you understand the point of templates. Why do you have int n as the second template parameter? Do you understand that the way you are using it completely circumvents the point of templates?
That is how my professor wants the project. I know it defeats the purpose of a template, but it seems like he wants us to use int n for the size of the array.

He gives us this declaration:
1
2
3
4
5
6
7
8
9
10
template <class T, int n>
class TWO
{
private: T a[n];
public:
	void ReadData();   // Read data into array a
	void DisplayData();   // Display array a
	void SortArray();     // Sort array a
	~TWO();
};


He then tells us to create two objects, Q and P. One for 10 ints and one for 12 strings. I am simply following his instructions. If I don't, I won't get credit.

I am only having a problem assigning months to my array and getting it to sort properly.
Last edited on
Using n for the size of the array is perfectly fine. Lines 24 and 34 are not fine.
That's the problem I am having. I don't know how I can logically have a template function that can read in random numbers and the months... I thought by using lines 24 and 34 the complier will make sure to enter the correct data into the arrays. Any hints?
What does your assignment say about the ReadData function?
It says to create two objects, P and Q, in object P the array a is type int size 10, in Q array a is string type and size 12. Generate 10 random numbers <20 and store them in array a of object P. Store the name of the month from Dec to Jan in array a of object Q. We then have to display both arrays, sort them, and re-display them using those three functions.
My code works now, I added a second read data function... I guess it still overloads it. Does this look fine?
I don't think that the code that generates the random numbers or stores the months is supposed to be inside one of the template class member functions - I think that's what it wants you to do in the main function.
Last edited on
Topic archived. No new replies allowed.