I'm trying to sum all the elements of a vector of vectors of vectors ... of integers: something like a std::vector<std::vector<std::vector<std::vector<int>>>> where there's no need to every layer have the same size.
I would like to accomplish it using template, so I did it:
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;
}
}
BUT, this is happening:
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
I really don't know why...
Thanks in advance.
Reading your error message. It looks like your functions are in a separate compilation unit from main.cpp. If your functions are in a .h file, #include the header file in main.cpp.
I would suggest using the template specialization declaration:
template<>
int sumAllElements(std::vector<int> v)
{
...
}
Another, unrelated suggestion, would be to pass the vectors by const reference. Currently, you are passing them by value which could be costly if the vectors are large.
You can't call a function that hasn't been declared yet. Templates can sometimes make that problem go away, but not always. And this is one of the cases where you simple need a declaration of int sumAllElements(std::vector<int> v) prior to template < class T > int sumAllElements(std::vector<T> v)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With