0

This a continuation to the question. Got compile error

g++ -fpermissive -std=c++11 te2b.cc te2d.cc
/tmp/ccUVm8OV.o: In function `notify(std::string, int)':
te2b.cc:(.text+0x80): undefined reference to `void notify<char const*>(char const*, char const*&&)'
/tmp/ccUVm8OV.o: In function `main':
te2b.cc:(.text+0xe6): undefined reference to `void notify<int>(char const*, int&&)'
collect2: error: ld returned 1 exit status

Guess my extern for a template function was not declared right, don't know how to correct it. Any ideas? Thanks!

====== te2b.cc======

#include <vector>
#include <iostream>
#include <unordered_map>
#include <stdarg.h>
using namespace std;

extern char notifyBuf[];
template <typename ... Ts>
extern void notify(const char* format, Ts&&... args);
void notify(string s, int maxSize=0) {
    if (maxSize && (maxSize < s.size())) {
        s.erase(maxSize);
    }
    printf("second invokation\n");
    notify("%s\n", s.c_str());
}
int main(int argc, char *argv[]) {
    string s = "hi tom";
    //notify(s);
    notify("hi %5d \n", 89);    
    printf("%s\n", &notifyBuf[0]);
    notify(s, 5);
    printf("%s\n", &notifyBuf[0]);
    return 0;
}

====== te2d.cc =======

#include <sys/shm.h>
#include <malloc.h>
#include <unordered_map>
#include <string>
#include <iostream>
typedef unsigned char uchar;
typedef unsigned short uint16;
using namespace std;

char notifyBuf[0x100000] = "s";
template <typename ... Ts>
void notify(const char* format, Ts&&... args)  {
    sprintf(&notifyBuf[1], format, std::forward<Ts>(args)...);
}
Community
  • 1
  • 1
packetie
  • 4,839
  • 8
  • 37
  • 72
  • the definition of the templated version of `notify` should be in the same translation unit where it is used (unless it's explicitly instantiated, which is not the case here), preferably in a header file – Piotr Skotnicki Sep 07 '15 at 17:49
  • @PiotrSkotnicki, yeah, this hint helps. Move the definition to a header file works. Please write it as an answer. Thanks. – packetie Sep 07 '15 at 17:57

0 Answers0