0

Hi I'm writing this function into my help.h file.

here is my help.h file

#ifndef help_H
#define help_H

#include <iostream>
#include <string>
#include <tr1/array>

class help {

    public:
    template<std::size_t N>
    static std::string to_string( std::tr1::array<char, N> const& arr);

};

#endif

here is my .cpp file

#include "help.h"

template<std::size_t N>
std::string help::to_string( std::tr1::array<char, N> const& arr)
{
    const char* str = reinterpret_cast<const char*>(arr.data());
    return std::string( str, str+N );
}

I wonder which part is wrong ? when I compile, it is saying:

mpic++ -o local help.cpp globalVar.cpp Graphnode.cpp move.cpp ioCheckFile.cpp mySearch.cpp ods_v3.cpp -L/opt/local/lib/  -lboost_iostreams-mt -lz -I/opt/local/include
Undefined symbols for architecture x86_64:
  "std::string help::to_string<16ul>(std::tr1::array<char, 16ul> const&)", referenced from:
      mySearch::bfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >&) in mySearch-ekZOo8.o
      mySearch::dfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::tr1::array<std::tr1::unordered_map<std::string, char, std::tr1::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, char> >, false>, 2000ul>) in mySearch-ekZOo8.o
      _main in ods_v3-580g0A.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [local] Error 1
weeo
  • 2,619
  • 5
  • 20
  • 29
  • 1
    move the implementation to the header file - templates are like that... – Luchian Grigore Jun 17 '13 at 05:02
  • So as long as there is a template, it doesn't need any function declaration in the .cpp file? – weeo Jun 17 '13 at 05:03
  • also http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – billz Jun 17 '13 at 05:05
  • 1
    Depends - if you define a specialization, you can have the implementation in a cpp file, but that's beyond the scope of this question. Usually, if you want it to be generic, the implementation needs to be visible to all translation units. – Luchian Grigore Jun 17 '13 at 05:06
  • http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file Same question here.. Solved. Many Thanks! – weeo Jun 17 '13 at 05:07
  • btw, it is not a compile error but a linking error – Philip Stuyck Jun 17 '13 at 05:10
  • also `return std::string(std::begin(arr), std::end(arr));`, no need to cast array. – billz Jun 17 '13 at 05:13

0 Answers0