Hey folks,

I am working on a project (its a continuation of my previous project)... but with a few additions. First off, i have to use class templates... I have used them before, but not when working with multiple class definitions and multiple files.

Below i will post sections of my Person.h, and Person.cpp files. I am getting linker errors with all of my functions when trying to edit them to work with templates. I am sure i am missing something rather obvious...

Person.h

#define PERSON_H

#include "340.h"

template <class T>
class Person;

template <class T>
class Person {
	public:
      Person();
      virtual ~Person();
      void setFirstName(T &x);
      T getFirstName() const;
      void setLastName(T &x);
      T getLastName() const;
      void setSSN(T &x);
      T getSSN() const;
      void setDOB(T &x);
      T getDOB() const;
      void setGender(T &x);
      T getGender() const;
      
    private:
      string firstName;
      string lastName;
      string SSN;
      string DOB;
      string gender;
};

#endif //PERSON_H

Person.cpp

#include "Person.h"

/****************************************************************
   FUNCTION:   Person()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Person() constructor
****************************************************************/
template <class T>
Person<T>::Person()
{
}

/****************************************************************
   FUNCTION:   ~Person()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Person destructor
****************************************************************/  
template <class T>  
Person<T>::~Person()
{
}

/****************************************************************
   FUNCTION:   setFirstName(const string &x)
   ARGUMENTS:  const string &x - the first name of student
   RETURNS:    none
   NOTES:      this function sets the students first name
****************************************************************/
template <class T>
void Person<T>::setFirstName(T &x)
{
     firstName = x;
}

/****************************************************************
   FUNCTION:   getFirstname() const
   ARGUMENTS:  none
   RETURNS:    string firstName
   NOTES:      this function returns the students first name
****************************************************************/
template <class T>
T Person<T>::getFirstName() const 
{
     return firstName;
}
...//more functions, but lots of space used... you could comment out the functions methods in the class definition if you wanted to try compiling?!

I hope someone sees an obvious mistake i am overlooking. My textbooks and previous projects all seem to be similar to what i have done on this...yet they seemed to work!

Thanks again folks

Whats the exact error, thats the easiest way :)

Chris

[Linker error] undefined reference to `Person::setFirstName(std::string&)'
[Linker error] undefined reference to `Person::setLastName(std::string&)'
[Linker error] undefined reference to `Person::setSSN(std::string&)'
... and the same for each function listed in the class...

So not really an 'error' but a linker error...thanks!!!

***EDIT***

I just edited my variables in the class definition to be of type T...

private:
      T firstName;
      T lastName;
      T SSN;
      T DOB;
      T gender;

Thinking that might be it...but no go there... is it something else? Cheers for helping all.

That looks like an error in getting an appropiate type built.
That can occur when you have multiple files. (compile+compiler flag dependent)

Add a template instanciation template class person<std::string>; at the end of person.cpp and if we are lucky it will fix/change the problem.

Note: If that is the problem, then expect a lot of compiler errors in person since you will not have actually compiled any instance of person yet.

StuXYZ --- thank you for your effort, i tried you above mentioned idea, and that did not work! (thank you for trying/helping though!) Soooo....

I am going to upload all files of my code. I realize thats a pain in the so and so... but for people to see exactly what im doing and how i am getting errors, it might help seeing all appropriate files for your own tests/compiling.

Thanks for your patience and time.

***EDIT****

After editing my student.h and student.cpp files... to use templates... i get ONE error...so i might be getting somewhere. I think you were right to mention that the error could be caused by using multiple files/templates.

So here is my new error:

23 C:\Users\Tony\Documents\Spring 2009\CSCI 340\Project 2\Student.h expected class-name before '{' token

On this line:

template <class T>
class Student : public Person {
	public:

I will re-upload my Student.h and Student.cpp so that changes can be seen etc.

Thanks a ton.

You have some errors: ALL of which should have been explained by you compiler, that you can get to linking is just telling you to get a better compiler (gcc is the minimum and free.).
(a) to (c) stop your code compiling/linking.

(a) student inherrits from Person BUT person is a template class so
which person is student inherriting from e.g. Person<int> Person<std::string> ????

(b) Put the line given in my last post at the bottom of Person.cpp

(c) You cant use Person:: in print_info (student) , so get rid of it, just getFirstName() etc.
[well you can use Person<std::string> or whatever you decided on Student to inherrit from,
but that will give you an unnecessary dependence on your template choise.]

(d) Fixed size array of courseInfo looks bad.

(e) return const references for strings in the getGrade() etc.

(f) void CourseInfo::incrementCount(int &x) {x = x + 1;} This HAS to be wrong!! (I did lol at this!)

(g) Grade/year as strings???

(h) Add some checking etc.

Those threefixes (a-c) get it to compile and link.

Adding your functionallity is another story.

can a template class of person be made????

can we make a person template class
plx explain ur reasons

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.