정수 벡터의 벡터 벡터 벡터의 모든 요소를 합산하려고합니다. 모든 레이어에 필요가없는 문자는 std::vector<std::vector<std::vector<std::vector<int>>>>
입니다. 같은 사이즈.정수 벡터의 벡터 벡터의 합 ...
내가 템플릿을 사용하여 달성하고자하는, 그래서 그것을했다 :
namespace nn
{
template < class T >
int sumAllElements(std::vector<T> v)
{
int size = v.size();
int output = 0;
for (int i = 0 ; i < size ; i++)
{
//should call the function below
output += sumAllElements(v[ i ]); //or this function, depending in
//which "layer" we are
}
return output;
}
int sumAllElements(std::vector<int> v)
{
int size = v.size();
int output = 0;
for (int i = 0 ; i < size ; i++)
{
output += v[ i ]; //we've reached the bottomest layer,
//so just sum everybory
}
return output;
}
}
하지만, 이런 일이 :
CMakeFiles\test.dir/objects.a(main.cpp.obj): In function `main':
D:/test/main.cpp:49: undefined reference to `int nn::sumAllElements<std::vector<int, std::allocator<int> > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\test\build.make:141: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: test] Error 2
정말 ... 왜 모르는
미리 감사드립니다.
왜 이것이 다운 voted인지 확실하지 않습니다. 템플릿에서의 이름 검색은 간단합니다. – MSalters
질문이 downvoted 경우 나는 볼 수 없다 ... –
'벡터'대신'int sumAllElements (int x)'를 쓰고'return x'를 쓰면 어떨까요? –
zahir