KDaker
KDaker

Reputation: 5909

calling c++ function with template in objective-c

im trying to call a c++ template function in objective c that looks like this:

template<typename T>
void test() {
    ...

    std::cout << "hello world! \n";
}

Im getting the following error with calling test() :

Undefined symbols for architecture armv7:
"test()", referenced from:
 -[viewController onNext] in ViewController.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

if i take out template<typename T> it works fine, however, i need to use some functions from a c++ library that require templates and i cant get around it. any idea on whats happening?

I have no experience with c++ what so ever...

Upvotes: 0

Views: 656

Answers (2)

justin
justin

Reputation: 104698

In C++:

  • void test()
  • template<>void test<int>()
  • template<>void test<float>()

(as examples) are all different symbols. by removing the template/parameter, it's a different symbol which matches the one you call in -[viewController onNext].

Upvotes: 1

fstamour
fstamour

Reputation: 799

In C++, when you call a templated function, if the template parameter (i.e. T in your code) can't be deduced from the parameter passed to the function (which is your case since your function doesn't have any parameter), you shall call the function like this:

test< someType >();

Upvotes: 3

Related Questions