2

TestTempB.h

#ifndef TESTTEMPB_H_
#define TESTTEMPB_H_
#include <string>
using namespace std;
namespace ddl_lib {

class TestTempB {
public:
    TestTempB();
    virtual ~TestTempB();
    template<class ... Args>
    void callCommandB(const string&, const uint32_t*, Args ...);
    template<class ... Args>
    void callCommandA(const string&, Args ...);
};

} /* namespace ddl_lib */

#endif /* TESTTEMPB_H_ */

TestTempB.cpp

#include "TestTempB.h"
#include "TestTempA.h"
#include "stdint.h"
#include <iostream>
namespace ddl_lib {

TestTempB::TestTempB() {
    // TODO Auto-generated constructor stub

}

TestTempB::~TestTempB() {
    // TODO Auto-generated destructor stub
}

template<class ... Args>
void TestTempB::callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
    cout << cmdName << endl;
}
template<class ... Args>
void TestTempB::callCommandA(const string& cmdName, Args ... params) {
    callCommandB(cmdName, NULL, params...);
}
} /* namespace ddl_lib */

main

#include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;

int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

I'm using the variadic-template. Why the gcc can't deduced the real func? When the callCommandA and callCommandB in the main file,it works just fine.

#include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;
template<class ... Args>
void callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
    cout << cmdName << endl;
}
template<class ... Args>
void callCommandA(const string& cmdName, Args ... params) {
    callCommandB(cmdName, NULL, params...);
}
int main() {
    string aa = "15615";
    bool ccc = false;
    callCommandA("name", &aa, &ccc); //no erro
}

Why and how could I fix it? Thanks!!!

huppyuy
  • 133
  • 4
  • 10

1 Answers1

5

You CANNOT put template function into .cpp. you must put it into .h because compiler must know what the template is when specializing it.

your first main file

#include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;

int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

is equal to

#include <iostream>
#include <string>
using namespace std;

namespace ddl_lib {

class TestTempB {
public:
    TestTempB();
    virtual ~TestTempB();
    template<class ... Args>
    void callCommandB(const string&, const uint32_t*, Args ...);
    template<class ... Args>
    void callCommandA(const string&, Args ...);
};

} /* namespace ddl_lib */
using namespace ddl_lib;

int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

Now think as if you were the compiler. you're trying to compile b.callCommandA("name", &aa, &ccc). You have to specialize the template, but you do not know what it exactly is. To compile it, you have to know that, like this:

#include <iostream>
#include <string>
using namespace std;

namespace ddl_lib {

class TestTempB {
public:
    TestTempB();
    virtual ~TestTempB();
    template<class ... Args>
    void callCommandB(const string&, const uint32_t*, Args ...);
    template<class ... Args>
    void callCommandA(const string&, Args ...);
};

template<class ... Args>
void TestTempB::callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
    cout << cmdName << endl;
}
template<class ... Args>
void TestTempB::callCommandA(const string& cmdName, Args ... params) {
    callCommandB(cmdName, NULL, params...);
}

} /* namespace ddl_lib */
using namespace ddl_lib;

int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

Now you do know what callCommandA is, then you can specialize and compile it.

ikh
  • 10,119
  • 1
  • 31
  • 70