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 7192339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:55:12+00:00 2026-05-28T19:55:12+00:00

I have a templated class called PooledResource . It handles loading things like textures

  • 0

I have a templated class called PooledResource. It handles loading things like textures and sounds for me and ensures that I don’t load the same one multiple times on accident. Resources of a certain type will always be in a certain subdirectory, and I wanted to make it easier to load them, so I decided to add a static variable that keeps track of the root folder for a certain type of resource.

template <typename T>
class PooledResource
{
public:
    // ...

    static const char* PathPrefix;
    static ResourceTable myResourceTable;


    static void load(const char* filename)
    {
        // Any attempt to use PathPrefix in here causes a compilation error
        // All I want to do is use PathPrefix and filename to get the full
        // path do a file and load it. 
    }
};

template <typename T>
const char* PooledResource<T>::PathPrefix = NULL;

template <typename T>
typename PooledResource<T>::ResourceTable PooledResource<T>::myResourceTable;

The idea is that I can use a typedef’d version of this for various types. For example, in PooledTexture.hpp I would have:

typedef PooledResource<Texture> PooledTexture;

and in PooledTexture.cpp I would have:

template <>
const char* PooledTexture::PathPrefix = "../assets/textures/";

This will all compile and run fine if I don’t attempt to use PathPrefix in the load function above. As soon as I use it though, I get errors like this:

 90 || CMakeFiles/game.dir/PooledTexture.cpp.o:(.data+0x0): multiple definition of `ag::PooledResource<sf::Texture>::PathPrefix'
 91 CMakeFiles/game.dir/lua/LuaFrame.cpp.o:/usr/include/c++/4.6/i686-linux-gnu/./bits/gthr-default.h|241| first defined here

I’ve tried reproducing this with a simpler case to try to hunt down the problem, but other cases I try seem to work. I’m not sure what I’m doing differently here that would cause this error.

  • 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-05-28T19:55:12+00:00Added an answer on May 28, 2026 at 7:55 pm

    The problem here is that your space for the variable is allocated in the header, which is then included by multiple compilation units – LuaFrame.cpp.o and PooledTexture.cpp.o. Side note: naming your object files .cpp.o is confusing.

    So, there are two variables with the same name.

    You need to move the actual space allocation (not the declaration, but the bit where you assign it) into a cpp file instead of a header, and then build that file in with the rest of your application. Yes, you have it in a header now: the bit where you set it to NULL!

    EDIT: when you do this, the header will no longer have enough information to create an instance of the template, so you’ll have to explicitly instantiate the class for each type with which you wish to use it.

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

Sidebar

Related Questions

I have the following use case: There's a class called Template and with that
I have a dll that contains a templated class. Is there a way to
In C++, can you have a templated operator on a class? Like so: class
I have a class called Device that accepts two policies as far as I
I have a class called LocalizedString that is defined in an external library that
Here's an overview of my problem: I have a templated class called TWMatrix. I've
I have a class, called DynamicTexture that takes in the width and height of
I have a question about templates. I would like to have a templated class
I have a templated class that performs an action on the class that is
I have a class called ReportRequest as: public class ReportRequest { Int32 templateId; List<Int32>

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.