So, I'm trying to choose C ++, and for this I decided to write a common group class using templates that takes type and size as template parameters:
in group .h:
#ifndef __GROUP_H
#define __GROUP_H
#define MAX_SIZE 10
template <class Type, int max>
class Group {
private:
std::string name;
int count, size;
Type * members[max];
public:
Group();
Group(const std::string & _name);
~Group();
virtual void show();
void add_member(Type &);
void list();
void set_name(const std::string &);
const std::string & get_name();
};
#endif
group.cc:
template <class Type, int max>
Group<Type, max>::Group() : count(0), size(max), name("New Group") {};
template <class Type, int max>
Group<Type, max>::Group(const std::string & _name) : name(_name), count(0), size(max) {};
template <class Type, int max>
Group<Type, max>::~Group() {
int i = 0;
while(i < this->count) {
delete this->members[i];
++i;
}
}
template <class Type, int max>
void Group<Type, max>::show() {
std::cout << "<#Group - Name: " << this->name << ", Members: " << this->count << "/" << this->size << " >\n";
}
template <class Type, int max>
void Group<Type, max>::add_member(Type & member) {
if (this->count < this->size) {
this->members[this->count] = &member;
this->count++;
} else {
std::cout << "Error - this Group is full!\n";
}
}
template <class Type, int max>
void Group<Type, max>::list() {
int i = 0;
std::cout << "The following are members of the Group " << this->name <<":\n";
while (i < this->count) {
std::cout << i << ". ";
(this->members[i])->show();
++i;
}
}
template <class Type, int max>
void Group<Type, max>::set_name(const std::string & _name) {
this->name = _name;
}
template <class Type, int max>
const std::string & Group<Type, max>::get_name() {
return this->name;
}
I also implemented the Person class and the Employee class (which inherits from Person), and both work and have a show () method.
My main thing is this:
test.cc
#include <iostream>
#include "group.h"
int main (int argc, char const *argv[])
{
Person p1("John", 25);
Person p2("Jim", 29);
Group <Person, 5> g("Ozcorp");
g.add_member(p1);
g.add_member(p2);
g.list();
}
I compiled it with a simple Makefile:
test: test.cc group.o
g++ -o test test.cc group.o
group.o: group.h group.cc
g++ -c group.cc
And finally (whew), when I started it with ./test, I got the following errors:
Undefined symbols:
"Group<Person, 5>::list()", referenced from:
_main in ccaLjrRC.o
"Group<Person, 5>::Group(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
groups() in ccaLjrRC.o
_main in ccaLjrRC.o
"Group<Person, 5>::~Group()", referenced from:
groups() in ccaLjrRC.o
_main in ccaLjrRC.o
_main in ccaLjrRC.o
"Group<Person, 5>::add_member(Person&)", referenced from:
_main in ccaLjrRC.o
_main in ccaLjrRC.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [test] Error 1
- - , . (), , . g++ 4.2.1 mac osx 10.6.4. , / . !