代码之家  ›  专栏  ›  技术社区  ›  Tometzky

与字符串(GCC)一起使用时对函数模板的未定义引用

  •  4
  • Tometzky  · 技术社区  · 17 年前

    replace_all search 在……里面 subject replace .

    template < class T >
    T replace_all(
            T const &search,
            T const &replace,
            T const &subject
    ) {
            T result;
    
            typename T::size_type done = 0;
            typename T::size_type pos;
            while ((pos = subject.find(search, done)) != T::npos) {
                    result.append (subject, done, pos - done);
                    result.append (replace);
                    done = pos + search.size ();
            }
            result.append(subject, done, subject.max_size());
            return result;
    }
    

    #include <iostream>
    
    template < class T >
    T replace_all(
            T const &search,
            T const &replace,
            T const &subject
    );
    
    // #include "replace_all.cc"
    
    using namespace std;
    
    int main()
    {
            string const a = "foo bar fee boor foo barfoo b";
            cout << replace_all<string>("foo", "damn", a) << endl;
            return 0;
    }
    

    当我尝试使用gcc 4.1.2编译它时

    g++ -W -Wall -c replace_all.cc  
    g++ -W -Wall -c test.cc  
    g++ test.o replace_all.o  
    

    test.o: In function `main':
    test.cc:(.text+0x13b): undefined reference to `
       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
       replace_all< std::basic_string<char, std::char_traits<char>, std::allocator<char> > >(
           std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&,
           std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&,
           std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&
       )
    '
    collect2: ld returned 1 exit status
    

    #include "replace_all.cc" 在test.cc中,按照以下方式编译:

    g++ -W -Wall test.cc
    

    damn bar fee boor damn bardamn b
    

    2 回复  |  直到 17 年前
        1
  •  9
  •   Mykola Golubyev    17 年前

    您不能链接模板,因为编译器在有人尝试使用(实例化)模板之前不知道要生成哪些代码。


    如果你愿意,把这个放到你的.cc文件中:

    template std::string replace_all( std::string const& search,
                                      std::string const& replace,
                                      std::string const& subject );
    
    
    template glibmm::ustring replace_all( glibmm::ustring const& search,
                                          glibmm::ustring const& replace,
                                          glibmm::ustring const& subject );
    
        2
  •  7
  •   sth    17 年前

    编译器需要在实例化时看到模板定义,否则无法生成专门用于实例化模板的类型的代码。正确的方法是把模板函数的实现 in the header file or to #include the .cc 就像你做的那样。