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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:42:49+00:00 2026-06-07T23:42:49+00:00

It has been a while since I have used templates with C++, but now

  • 0

It has been a while since I have used templates with C++, but now I really need them.

I reproduced a problem I am having and I don’t remember how the solution actually went.

#include <iostream>
#include <vector>

namespace problem {
    template <typename T>
    class data {
        public:
            inline data(T var) {
                this->var = var;
            }
        private:
            T var;
    };

    class storage {
        public:
            inline void push(problem::data<T> * data) {
                this->VData.push_back(data);
            }
        private:
            std::vector<problem::data<T> *> VData;
    };
};

int main() {
    problem::storage * testStorage = new problem::storage();
    problem::data<int> * testData = new problem::data<int>(256);

    testStorage->push(testData);

    delete testData;
    delete testStorage;
    return 0;
}

g++ -Wall problem.cpp gives me the following errors.

problem.cpp:17:35: error: ‘T’ was not declared in this scope
problem.cpp:17:36: error: template argument 1 is invalid
problem.cpp:21:30: error: ‘T’ was not declared in this scope
problem.cpp:21:31: error: template argument 1 is invalid
problem.cpp:21:34: error: template argument 1 is invalid
problem.cpp:21:34: error: template argument 2 is invalid
problem.cpp: In member function ‘void problem::storage::push(int*)’:
problem.cpp:18:17: error: request for member ‘push_back’ in ‘((problem::storage*)this)->problem::storage::VData’, which is of non-class type ‘int’
problem.cpp: In function ‘int main()’:
problem.cpp:29:28: error: no matching function for call to ‘problem::storage::push(problem::data<int>*&)’
problem.cpp:29:28: note: candidate is:
problem.cpp:17:16: note: void problem::storage::push(int*)
problem.cpp:17:16: note:   no known conversion for argument 1 from ‘problem::data<int>*’ to ‘int*’

I know I can use member templates, but what I do with the vector?

template <typename T>
inline void push(problem::data<T> * data) {
    this->VData.push_back(data);
}

If I use the member template then the vector definition will leave these errors.

problem.cpp:22:30: error: ‘T’ was not declared in this scope
problem.cpp:22:31: error: template argument 1 is invalid
problem.cpp:22:34: error: template argument 1 is invalid
problem.cpp:22:34: error: template argument 2 is invalid
problem.cpp: In member function ‘void problem::storage::push(problem::data<T>*)’:
problem.cpp:19:17: error: request for member ‘push_back’ in ‘this->.VData’, which is of non-class type ‘int’
  • 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-07T23:42:51+00:00Added an answer on June 7, 2026 at 11:42 pm

    If you want storage that can store more than one type of value you can try something like this: http://ideone.com/jjuVq

    class storage {
        struct data_base {};
    
        template <class K> 
        struct data: data_base {
            data(K value): value_(value) {}
            K value_;
        };
    
        typedef std::vector<data_base*> container_type;
    
    public:
        ~storage() {
            while(!this->VData.empty()) {
                delete this->VData.back();
                this->VData.pop_back();
            }
        }
        template <class P>
        inline void push(P v) {
            this->VData.push_back(new data<P>(v));
        }
        template <class P>
        P &get(int i) { return static_cast<data<P>*>(this->VData[i])->value_; }
    private:
        container_type VData;
    };
    

    or just use boost::any as a container’s value type.

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

Sidebar

Related Questions

It has been a while since I have used regular expressions and I'm hoping
This Jquery problem has been bugging me for a while now. I developed a
It has been a while since I've used Mathematica, and I looked all throughout
Ruby has been around for a while now so I was wondering if there
This has been frustrating me for a while now. I started developing a site
This has been annoying me for a while in several XCode projects. I really
This has been annoying me for a while yet the solution must be really
This one has been stumping me for a while. But I'm no expert. This
While I realize that this question has been asked once or twice ago but
It's been a while since I last used T4 and this is probably a

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.