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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:03:46+00:00 2026-05-25T21:03:46+00:00

I’m trying to create a simple template class in c++. I’ve been trying to

  • 0

I’m trying to create a simple template class in c++. I’ve been trying to compile the following but I only get a compile error. This is the code:

#include <stdlib.h>
#include <stdio.h>

template<int size>
class array {
public:
    int len;
    int data[size];
    array(void) : len(size) {}
    virtual ~array(void) {}
};

int main() {
    array<3> a;
    for (int i=0; i < a.len; ++i) {
        a.data[i] = i;
        printf("%d\n", a.data[i]);
    }
    return 0;
}

This is the error g++-4.2.1 is giving me:

Undefined symbols:
  "__Unwind_Resume", referenced from:
      _main in ccaYob9x.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

If we comment out the line for the destructor then the code compiles as it should and it gives me a list of the number 0, 1, 2.

What my ultimate goal is after understanding how template base classes work is to create specialized template classes. I want to create a multidimensional array but I wish to specialize it for the cases when the dimension is 1, 2, and 3. I mainly want to be able to overload the operator() for those cases. In any case, using a template class like this saves me the trouble of allocating memory dynamically when the dimensions are 1, 2, and 3. Does someone know how to change the code to allocate memory dynamically? Doing so will require you to define the destructor, which is the problem I’m currently facing.

EDIT:

I’m not familiar with template specialization. Does someone know how to make a specialized template when size=1, 2 and 3 to have static memory for data and by default to have dynamic memory?

EDIT 2:

It seems that I’m having trouble due to my g++ compiler. Does anyone see anything with with this:

g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-  werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple- darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

EDIT 3:

There has got to be something wrong with my g++ version. I just tried it with g++4.0 in the same machine and with the macports version g++-mp-4.3 and it works fine. I guess it is time to upgrade to the next version. Thank you for your answers and hints.

  • 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-25T21:03:47+00:00Added an answer on May 25, 2026 at 9:03 pm

    Try this:

    template<int size>
    class array {
    public:
        int len;
        int *data;
        array();
        virtual ~array(void);
    };
    
    // 1 dimension specialized array 
    template <>
    array<1>::array() : len(1)
    {
        data = new int;
    }
    
    template <>
    array<1>::~array()
    {
        delete data;
    }
    
    template <int size>
    array<size>::array() : len(size)
    {
    data = new int[size];
    } 
    
    template <int size>
    array<size>::~array()
    {
    delete [] data;
    }
    
    int main(int, char)
    {
        array<3> a;
        for (int i=0; i < a.len; ++i) {
            a.data[i] = i;
            printf("%d\n", a.data[i]);
        }
    
        array<1> b;
        for (int i=0; i < b.len; ++i) {
            b.data[i] = i;
            printf("%d\n", b.data[i]);
        }
    
        return 0;
    }
    

    With g++ you will not have compiling errors. You can define specialized operator() for each different array sizes without problems, like constructor and destructor seams.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.