그래서 C++을 선택하려고 시도 했으므로 서식 파일 매개 변수로 Type 및 Size를 사용하는 템플릿을 사용하여 일반 그룹 클래스를 작성하기로했습니다.템플릿으로 작업 할 때 g ++ 중복 심볼 오류
group.h에서:
#ifndef __GROUP_H
#define __GROUP_H
#define MAX_SIZE 10
/**********************************************************
* Define a Group class that handles a collection of members
* of some Type
**********************************************************/
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();
// display instance info
virtual void show();
// add member
void add_member(Type &);
// list memebers
void list();
// name setter/getter
void set_name(const std::string &);
const std::string & get_name();
};
#endif
group.cc : 또한
/**********************************************************
* class methods for Group
**********************************************************/
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";
// assumes the member has a show() method implemented
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;
}
내가 Person 클래스와 Employee 클래스 (Person에서 상속)를 구현했으며, 둘 다 작동하고 show() 메소드를가집니다. 이 같은
내 주요 외모 :
test.cc 나는 간단한 메이크 파일을 컴파일
#include <iostream>
#include "group.h" // this also has the declarations and implementation for Person/Employee
int main (int argc, char const *argv[])
{
// Person ctor takes name and age
Person p1("John", 25);
Person p2("Jim", 29);
// Group takes name to init
Group <Person, 5> g("Ozcorp");
g.add_member(p1);
g.add_member(p2);
g.list();
}
: 마지막으로
test: test.cc group.o
g++ -o test test.cc group.o
group.o: group.h group.cc
g++ -c group.cc
그리고 (휴), 내가 달릴 ./test
와 함께 다음 오류가 발생했습니다.
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
감사의 말을 전하는 이유는 무엇입니까? 감사합니다. 가능한 한 코드에서 (분명히) 공유하려고 했으므로 많이 용서하면 용서하십시오. Mac OS X 10.6.4에서 소스가 g ++ 4.2.1로 컴파일되었습니다. 또한 모든 스타일/좋은 코딩 습관에 대한 정보를 제공 할 것입니다. 감사!
__SOMETHING을 헤더 가드로 사용하지 마십시오. 선행 밑줄 (또는 두 개)이있는 매크로는 구현이 예약되어 있습니다. 당신의 경우에는 GROUP_H를 사용하십시오. – rubenvb