[C++] 함수 템플릿을 사용하는데 컴파일 에러가...

gurumong의 이미지

컴파일 에러가 나는데 무엇이 문제인지 모르겠습니다 ㅜ.ㅜ

//stack.h

#ifndef STACK_H_
#define STACK_H_
 
template <typename T>
void swap(T & a, T & b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}
 
struct person {
  char name[10];
  unsigned char age;
};
 
void swap(person & a, person & b)
{
  unsigned char temp;
  temp = a.age;
  a.age = b.age;
  b.age = temp;
}
 
#endif

//stacker.cpp
#include <iostream>
#include "stack.h"
using namespace std;
 
int main(void)
{
  int a, b;
  a = 10;
  b = 20;
  swap(a, b);
  cout << a << b << endl;
 
  person aa = {"A", 11};
  person bb = {"B", 22};
  swap(aa, bb);
  cout << aa.age << bb.age << endl;
 
}

//컴파일 에러

g++ -Wall -W -o stacker stacker.cpp
stacker.cpp: In function `int main()':
stacker.cpp:10: error: call of overloaded `swap(int&, int&)' is ambiguous
stack.h:6: note: candidates are: void swap(T&, T&) [with T = int]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:126: note:   \
              void std::swap(_Tp&, _Tp&) [with _Tp = int]
stacker.cpp:15: error: call of overloaded `swap(person&, person&)' is ambiguous
stack.h:20: note: candidates are: void swap(T&, T&) [with T = person]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:126: note:   \
              void std::swap(_Tp&, _Tp&) [with _Tp = person]
ifree의 이미지

std 에 있는 swap() 와 햇갈리는 듯.
::swap() 으로 해보세요.

gurumong의 이미지

아 함수이름을 고치니까 잘 되네요
감사합니다 T.T