Issues with templates

I have an assignment which is pretty basic in theory; I just have to receive some information for a person and then output it back out along with some other already inputted data. I believe I'm doing everything my professor taught me, but I'm getting a whole mess of errors. If I could get some help that would be great!

There are only 2 files, main.cpp:
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
#include    <iostream>
#include        <string>
using namespace std;
#include    "cpersoninfo.h"

int     main()
{
    // Create an int class object of CPersonInfo and have the default
    // constructor initialize it.
        CPersonInfo<int> P1;

        CPersonInfo<double>     P2(160.5, 73.5, 21, "Maria Lourdes Rodriguez");

        // Create a double class object of CPersonInfo and invoke the copy
        // constructor to initialize it with the info from P2
        CPersonInfo<double> P3;

        int             weight; // pounds
        int             height; // inches
        int             age;    // years
        string          fName;  // full name

        // print the default value for P1
        cout << "P1 = " << "\t\t\tWeight = " << P1.GetWeight() << endl
         << "\t\t\tHeight = " << P1.GetHeight() << endl
         << "\t\t\tAge = " << P1.GetAge() << endl
         << "\t\t\tName = " << P1.GetName() << endl
         << "\t\t\tTotal Name Length = " << P1.GetNameLength()
         << endl << endl;

    // get info from user to update P1
    cout << "Please enter an integer value of your weight, height, age and "
             << "your full name: ";
    cin >> weight >> height >> age >> fName;

        // update P1
        P1.SetWeight(weight);
        P1.SetHeight(height);
        P1.SetAge(age);
        P1.SetName(fName);

        // print out the results
        cout << "P1 = " << "\t\t\tWeight = " << P1.GetWeight() << endl
                        << "\t\t\tHeight = " << P1.GetHeight() << endl
                        << "\t\t\tAge = " << P1.GetAge() << endl
                        << "\t\t\tName = " << P1.GetName() << endl
                        << "\t\t\tTotal Name Length = " << P1.GetNameLength()
                        << endl << endl;

        cout << "P2 = " << "\t\t\tWeight = " << P2.GetWeight() << endl
                        << "\t\t\tHeight = " << P2.GetHeight() << endl
                        << "\t\t\tAge = " << P2.GetAge() << endl
                        << "\t\t\tName = " << P2.GetName() << endl
                        << "\t\t\tTotal Name Length = " << P2.GetNameLength()
                        << endl << endl;

        cout << "P3 = " << "\t\t\tWeight = " << P3.GetWeight() << endl
                        << "\t\t\tHeight = " << P3.GetHeight() << endl
                        << "\t\t\tAge = " << P3.GetAge() << endl
                        << "\t\t\tName = " << P3.GetName() << endl
                        << "\t\t\tTotal Name Length = " << P3.GetNameLength()
                        << endl << endl;

    return 0;

}  // end of "main" 


And then I have my cpersoninfo.h file:
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
#ifndef CPERSONINFO_H
#define CPERSONINFO_H

#include    <iostream>
#include    <string>
using namespace std;

template <class T>
class   CPersonInfo
{
        public:
        // constructors
        CPersonInfo();
        CPersonInfo(const CPersonInfo  &other);
        CPersonInfo(T weight, T height, int age, string fName);
                ~CPersonInfo(); // Destructor

        // member functions
        T       GetWeight() const;
        T       GetHeight() const;
        int     GetAge() const;
        string  GetName() const;
        int     GetNameLength() const;
        void    SetWeight(T weight);
        void    SetHeight(T height);
        void    SetAge(int  age);
        void    SetName(string fName);

        private:
                T m_weight; // pounds
                T m_height; // inches
                int *m_age; // years
                string m_fullName;
};

#endif  // CPERSONINFO_H

CPersonInfo::CPersonInfo()
{
    m_weight = 0;
    m_height = 0;
    m_age = 0;
    m_fullName = "NONE";
}

CPersonInfo::CPersonInfo(const CPersonInfo &other)
{
    m_weight = other.m_weight;
    m_height = other.m_height;
    m_age = other.m_age;
    m_fullName = other.m_fullName;
}

CPersonInfo::CPersonInfo(T weight, T height, int age, string fName)
{
    m_weight = weight;
    m_height = height;
    m_age = age;
    m_fullName = fName;
}

~ CPersonInfo::CPersonInfo()
{
    delete[] m_weight;
    delete[] m_height;
    delete[] m_age;
    delete[] m_fullName;
}

T CPersonInfo::GetWeight() const
{
    return m_weight;
}

T CPersonInfo::GetHeight() const
{
    return m_height;
}

int CPersonInfo::GetAge() const
{
    return m_age;
}

string CPersonInfo::GetName() const
{
    return m_fullName;
}

int CPersonInfo::GetNameLength() const
{
    int len;
    len = strlen(m_fullName);
    return len;
}

void CPersonInfo::SetWeight(T weight)
{
    m_weight = weight;
}

void CPersonInfo::SetHeight(T height)
{
    m_height = height;
}

void CPersonInfo::SetAge(int age)
{
    m_age = age;
}

void CPersonInfo::SetName(string fName)
{
    m_fullName = fName;
}


When I try to compile, I get a whole bunch of errors, all within the header file:
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
In file included from main.cpp:4:0:
cpersoninfo.h:38:1: error: invalid use of template-name 'CPersonInfo' without an argument list
cpersoninfo.h:46:1: error: invalid use of template-name 'CPersonInfo' without an argument list
cpersoninfo.h:54:1: error: invalid use of template-name 'CPersonInfo' without an argument list
cpersoninfo.h:62:14: error: expected class-name before '::' token
cpersoninfo.h:70:1: error: 'T' does not name a type
cpersoninfo.h:75:1: error: 'T' does not name a type
cpersoninfo.h:80:5: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h:80:27: error: non-member function 'int GetAge()' cannot have cv-qualifier
cpersoninfo.h: In function 'int GetAge()':
cpersoninfo.h:82:12: error: 'm_age' was not declared in this scope
cpersoninfo.h: At global scope:
cpersoninfo.h:85:8: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h:85:31: error: non-member function 'std::string GetName()' cannot have cv-qualifier
cpersoninfo.h: In function 'std::string GetName()':
cpersoninfo.h:87:12: error: 'm_fullName' was not declared in this scope
cpersoninfo.h: At global scope:
cpersoninfo.h:90:5: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h:90:34: error: non-member function 'int GetNameLength()' cannot have cv-qualifier
cpersoninfo.h: In function 'int GetNameLength()':
cpersoninfo.h:93:18: error: 'm_fullName' was not declared in this scope
cpersoninfo.h:93:28: error: 'strlen' was not declared in this scope
cpersoninfo.h: At global scope:
cpersoninfo.h:97:6: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h:97:29: error: variable or field 'SetWeight' declared void
cpersoninfo.h:97:29: error: 'T' was not declared in this scope
cpersoninfo.h:102:6: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h:102:29: error: variable or field 'SetHeight' declared void
cpersoninfo.h:102:29: error: 'T' was not declared in this scope
cpersoninfo.h:107:6: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h: In function 'void SetAge(int)':
cpersoninfo.h:109:5: error: 'm_age' was not declared in this scope
cpersoninfo.h: At global scope:
cpersoninfo.h:112:6: error: 'template<class T> class CPersonInfo' used without template parameters
cpersoninfo.h: In function 'void SetName(std::string)':
cpersoninfo.h:114:5: error: 'm_fullName' was not declared in this scope
cpersoninfo.h: In function 'std::string GetName()':
cpersoninfo.h:88:1: warning: control reaches end of non-void function [-Wreturn-type]
cpersoninfo.h: In function 'int GetAge()':
cpersoninfo.h:83:1: warning: control reaches end of non-void function [-Wreturn-type]


Nearly all of these errors are related to the use of the template. I've been cross-referencing my textbook and my notes from class, and as far as I can tell, I'm doing what I should be, but I'm obviously not doing something right.

If I could get some help that would be great, thanks!
Last edited on
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
#ifndef CPERSONINFO_H
#define CPERSONINFO_H

#include    <iostream>
#include    <string>
// using namespace std;  // *** do not have this in a header file

template <class T>
class   CPersonInfo
{
        public:
        // constructors
        CPersonInfo();
        CPersonInfo(const CPersonInfo  &other);
        CPersonInfo(T weight, T height, int age, std::string fName);
         ~CPersonInfo(); // Destructor

        // member functions
        T       GetWeight() const;
        T       GetHeight() const;
        int     GetAge() const;
        std::string  GetName() const;
        int     GetNameLength() const;
        void    SetWeight(T weight);
        void    SetHeight(T height);
        void    SetAge(int  age);
        void    SetName( std::string fName );

        private:
                T m_weight; // pounds
                T m_height; // inches
                int m_age; // years // ****** not a pointer
                std::string m_fullName;
};

template < class T >
CPersonInfo<T>::CPersonInfo()
{
    m_weight = 0;
    m_height = 0;
    m_age = 0;
    m_fullName = "NONE";
}

// ...

template < class T >
CPersonInfo<T>::~CPersonInfo()
{
    // **** empty 
}

template < class T >
T CPersonInfo<T>::GetWeight() const
{
    return m_weight;
}

// ...

template < class T >
std::string CPersonInfo<T>::GetName() const
{
    return m_fullName;
}

// and like-wise for all the other member functions
// ...

#endif  // CPERSONINFO_H 
That fixed most of the errors, thanks! I was able to fix the rest and got the code to compile, but now I get a segmentation fault when I try to run it.

EDIT: Nevermind! I realized I forgot to delete the pointers- they were put there by my professor for some reason. Getting rid of them fixed it. Thank you! :)
Last edited on
Topic archived. No new replies allowed.