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

  • Home
  • SEARCH
  • 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 3482114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:31:18+00:00 2026-05-18T10:31:18+00:00

I am writing a template class for an array of objects, call it arrayobjclass,

  • 0

I am writing a template class for an array of objects, call it arrayobjclass, which holds pointers to other objects, specifically to other arrays in my implementation. The arrays are implemented as objects as well, call them arrayclass.
Looking for compilation ready with minimal changes.

when I try to test my classes with the following line,

g++ main.cpp arrayclass.cpp arrayobjclass.cpp -o arrayobj

I get the following error:

/tmp/ccEpROXj.o(.text+0x17c): In function `main':
: undefined reference to `arrayobjclass<arrayclass, int>::arrayobjclass(int)'
/tmp/ccEpROXj.o(.text+0x1dc): In function `main':
: undefined reference to `arrayobjclass<arrayclass, int>::addelem(arrayclass*)'
collect2: ld returned 1 exit status

I really can’t understand what is wrong. any help would be appreciated. the short relevant part of the code is below if it helps. THANKS IN ADVANCE!

This is what i have in main:

#include "arrayclass.h"
#include "arrayobjclass.h"
#include <iostream>

// 5 arrays of 10 maxsize each
#define MAXSIZE_array 10
#define NUMB_objs 5

using namespace std;

int main () {

    //create a simple array as an arrayclass object
    arrayclass * numbers1 = new arrayclass (MAXSIZE_array);

    //array of objects to hold pointers to simple arrays as created above
    arrayobjclass<arrayclass,int> * myobjs = new arrayobjclass<arrayclass,int> (NUMB_objs);

    //fill up the simple array
    int i;
    for (i=0; i<10; i++) {
        numbers1->addelem(i); 
    }

    //add a pointer to the simple array in my array of objects
    myobjs->addelem(numbers1);    
}

//arrayobjclass.h
//declarations of an array of pointers to objects

template <class obj, class key>
class arrayobjclass {

private:
    //obj * arrayptr;
    obj * objarray [];
    int maxsize;
    int totalelem;
public:
    arrayobjclass(int);
    bool addelem(obj *);
};

//arrayobjclass.cpp
//implementation of arrayobjclass, array of pointers to objects

#include "arrayobjclass.h"
#include "arrayclass.h"

template <class obj,class key>
arrayobjclass<obj,key>::arrayobjclass (int size){    
    maxsize=size;
    objarray = new obj[maxsize];
    totalelem = 0;
}

template <class obj, class key>
bool arrayobjclass<obj,key>::addelem (obj * newobj) {   
    if (totalelem < maxsize ) {
        objarray[totalelem] = newobj;
        totalelem ++;
        return true;
    }
    return false;
}

//arrayclass.h

class arrayclass {

private:
    int * arrayptr;
    int maxsize;
    int totalelem;
public:
    arrayclass(int);
    bool addelem(int);
};

//arrayclass.cpp

#include "arrayclass.h"

arrayclass::arrayclass (int size){   
    maxsize=size;
    arrayptr = new int[maxsize];
    totalelem = 0;
}

bool arrayclass::addelem (int addval) {    
    if (totalelem < maxsize ) {
        arrayptr[totalelem] = addval;
        totalelem ++;
        return true;
    }
    return false;
}
  • 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-18T10:31:19+00:00Added an answer on May 18, 2026 at 10:31 am

    You can’t put template declarations in .cpp files like that. Template declarations and implementation need to be visible in the same translation unit. Put template implementations in headers that you #include directly.

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

Sidebar

Related Questions

I'm writing a template class which should take an array as its type. public
I am writing a memory-managing template class in which I want to create a
I've written a (array) container class template (lets call it smart array ) for
When I'm writing a simple (non-template) class, if the function implementation is provided right
I am writing a template class that takes as an input a pointer and
Hey there, I'm writing a template container class and for the past few hours
I am writing a basic templating class for my own project. The basic usage
Here is a weird question. I am building an array of objects manually, like
I'm following an example in Accelerated C++ and writing a simple Handle class that
Given the following template: template <typename T> class wrapper : public T {}; What

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.