Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:16:39+00:00 2026-06-18T12:16:39+00:00

I have a templated class with a constructor which takes a single argument; a

  • 0

I have a templated class with a constructor which takes a single argument; a function pointer to a function that takes an argument of the second template-type. and returns an element of the first template-type.

Here’s the code:

cache_controller.hpp:

template<class TYPE, class KEY>
class cache
{
    private:
        TYPE (*create_func)(KEY);
    public:
        cache(TYPE (*func)(KEY));
};

extern sf::Texture * create_texture(std::string path);

cache_controller.cpp:

template<class TYPE, class KEY>
cache<TYPE, KEY>::cache(TYPE (*func)(KEY))
{
    this->create_func = func;
}

sf::Texture * create_texture(std::string path)
{
    sf::Texture * tex_p = new sf::Texture;
    tex_p->loadFromFile(path);
    return tex_p;
}

Test code:

cache<sf::Texture *, std::string> tcache(&create_texture);

However, when I link I get this error:

[Linker error] main.cpp:11: undefined reference to `cache<sf::Texture*, std::string>::cache(sf::Texture* (*)(std::string))' 

Of course, if there are any easier ways to implement an object cache with arbitary keys, values and creation functions I’m all ears.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:16:40+00:00Added an answer on June 18, 2026 at 12:16 pm

    You provide a definition for member functions of a template class in a .cpp file.

    The definition of those member functions must be visible to the compiler at the instantiation point (i.e., from the .cpp file that invokes those functions), otherwise only their declaration will be seen, and the compiler will just rely on that function being defined somewhere else, while processing a different translation unit.

    Since there is no instantiation of that function in the translation unit (i.e. .cpp file) where its definition is visible to the compiler, none will be generated, and the linker will eventually complain that no definition is provided.

    You should put the definition of cache‘s constructor in the header file where class template cache is defined (which is, in your case, cache_controller.hpp).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a templated class Rect with conversion constructor which allows conversion between Rect
I have a templated container class that looks something like this: template <class ItemType>
I have the following templated function... template< class T > T *create_object( lua_State *L
Lets say I have a function which takes a function pointer as a parameter,
I have a kind of buffer class that takes an std::vector as a constructor
I have non-template class with a templatized constructor. This code compiles for me. But
I have a class with a template constructor, and the code is actually calling
I have a templated class that I want to avoid copying (because of the
I have that template class that uses a policy for it's output and another
I have an implementation of a template class Triple, which is a container holding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.