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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:47:06+00:00 2026-05-25T17:47:06+00:00

I am practicing function specialization, and I was trying to create a little storage

  • 0

I am practicing function specialization, and I was trying to create a little storage object with a specialized print function. Here’s my code:

#ifndef STORAGE_HPP_
#define STORAGE_HPP_

#include <iostream>

using namespace std;

template <typename T, int size> class Storage
{ //LINE 16 ERROR
public:
    Storage():arr(new T*[size]){};
    ~Storage()
    {
        for (int i = 0; i < size; i++)
        {
            delete arr[i];
        }
        delete[] arr;
    }
    void push(T obj, int i)
    {
        arr[i] = new T(obj);
    }
    void print()
    {
        for (int i = 0; i < size; i++)
        {
            cout << *arr[i];
        }
        cout << endl;
    }


private:
    T** arr;
};

template <typename T, int size> void Storage<int,size>::print() //LINE 38 ERROR
{
    for (int i = 0; i < size; i++)
        {
            cout << (char) *arr[i];
        }
        cout << endl;
}
#endif /* STORAGE_HPP_ */

And I get this error:

../Storage.hpp:38:63: error: invalid use of incomplete type
class Storage<int, size>
../Storage.hpp:9:1: error: declaration of ‘class Storage<int, size>’

So, first question: can specialized functions be implemented inside a class? I tried but got an error.
Second, why do I get the error I’ve attached?
Thanks!

EDIT: I tried something new as someone here suggested. I’ve change print inside the class to be only void print() and I’ve implemented it outside so I can overload the function. Here:

template <typename T, int size>
void Storage<T,size>::print()
{
    for (int i = 0; i < size; i++)
    {
        cout << *arr[i];
    }
    cout << endl;
}


template <typename T, int size>
void Storage<int,size>::print() //ERROR HERE
{
        for (int i = 0; i < size; i++)
        {
            cout << *arr[i];
        }
        cout << endl;
}

Now I get invalid use of incomplete type ‘class Storage<int, size>’Where I wrote ERROR HERE (obviously!)
I understand that’s a common solution, am I right? And why do I get 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-25T17:47:06+00:00Added an answer on May 25, 2026 at 5:47 pm

    The problem is that you are trying to use a partial specialization of the entire class without having defined the partially-specialized class.

    If print were itself a function template, the situation would be different, because you can indeed specialize function templates. However, your construction only has the entire class as a template.

    This means that template <typename T, int n> class Storage<T, n> and template <int n> class Storage<int, n> are entirely different, unrelated classes. Thus you must first define the latter class:

    template<int n> class Storage<int, n>
    {
      // define everything
    };
    
    template<int n> void Storage<int, n>::print() { /* implement */ }
    

    Consider that the partial specialization Storage<int, n> may be an entirely different class from the primary template, and it may have totally different member functions. The compiler has no way of knowing this until you actually define that class.


    Following sbi’s comment, here’s one idea:

    //... in the class definition
    
    template<typename S, int m> friend void print_helper(const Storage<S, m> &);
    template<int m> friend void print_helper(const Storage<int, m> &);
    
    void print() { print_helper(*this); }
    

    // outside:
    
    template <typename S, int size> void print_helper(const Storage<S, size> & s)
    {
      // ...
    }
    template <int size> void print_helper(const Storage<int, size> & s)
    {
      // ...
    }
    

    Instead of the friend you could also make the helper function template static, but that might add a lot of code bloat, since you’ll have two static function templates per class type, rather than just two globally.

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

Sidebar

Related Questions

I am practicing with jQuery and trying to write a little interaction where a
I am new to PHP and I am practicing the file_put_contents() function, and I
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
I am practicing with PHP and AJAX but I have some problems! I'm trying
For practicing purposes, I’m about to create a new ASP.NET MVC 3.0 application. My
I am practicing sending emails with Google App Engine with Python. This code checks
I am practicing how to find and remove dead code. I have the following
I'm practicing Haskell, and writing a summation function that takes in two numbers (upper
I'm practicing object oriented syntax in javascript and am having some problems. This is
I am practicing regex with C#. This is my code: string test = this

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.