Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to create a class that stores undefined number of variables of any type (or their pointers actually).

What I have tried:

C++
// ThEz.hpp
using namespace std;

class ThEz
{
    private:
        map <string, void*> mx_;
    public:
        void add( string name, void * ag );
        template <typename T> T get( string name );
};

// ThEz.cpp
// set method looks alright
template <typename T> T ThEz::get( string name ) {
    T * rv = (T*) mx_[name];
    return *rv;
}

// main.cpp
int x = 50;
    ThEz th1;
    th1.add( "hello", &x );

    int y = th1.get<int>("hello"); //error here

    cout << y;


Compiler (GNU GCC) says:
undefined reference to `int ThEz::get<int>(std::string)'
Posted
Updated 3-May-16 11:54am

1 solution

Actually your code compiles with g++ (I am using version 5.2.1). It gives 'undefined reference' for the th1.add call, but that's correct, since you never defined it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-May-16 19:28pm    
5ed.
Also, "undefined number of variables of any type" sounds weird; there is always a finite number of template parameters.
If it wasn't so, where the information on each type would come from?
—SA
CPallini 4-May-16 2:59am    
Thank you.
Cansisti 3-May-16 19:34pm    
Gonna check it tomorrow, thanks.
I found out that problem occurs when code is separated into files. Everything works fine when there is single file only.
CPallini 4-May-16 2:59am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900