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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:05:42+00:00 2026-06-15T16:05:42+00:00

Possible Duplicate: Why can templates only be implemented in the header file? I am

  • 0

Possible Duplicate:
Why can templates only be implemented in the header file?

I am trying to practice C++ templates and while doing it, g++ gave me the following linker error:

g++ main.o list.o -o app
main.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `List<int>::List(List<int> const&)'
collect2: ld returned 1 exit status
make: *** [app] Error 1

I tried to do the following in my main :

#include "List.h"

int main() {
    List<int> a;
    List<int> b(a);
    return 0;
}

while my List.h looks like :

#ifndef LST_H
#define LST_H

template <typename T>
class List {
    public:
    template <typename TT> class Node;
    private:
    Node<T> *head;
    public:
    List() { head = new Node<T>; }
    ~List() { delete head; }
    List( const List &l );

    template <bool DIM> class iterable_frame;
    template <bool DIM> class supervised_frame {
        iterable_frame<DIM> sentinels;
        iterable_frame<DIM> data;
        bool sentinel_completeness;

        public:
        supervised_frame( const List& );
        ~supervised_frame() {}
        void copy_sentinels( iterable_frame<DIM>& );
        void copy_cells( iterable_frame<DIM>& );
    };
    template <bool DIM> class iterable_frame {
        Node<T> *head;
        Node<T> *caret;

        public:
        iterable_frame( const List& );
        ~iterable_frame() {}
        inline bool end() { return head == caret; }
    };
    template <typename TT> class Node {
        unsigned index[2];
        TT num;
        Node<TT> *next[2];
        public:
        Node( unsigned x = 0, unsigned y = 0 ) { index[0]=x; index[1]=y; next[0] = this; next[1] = this; }
        Node( unsigned x, unsigned y, TT d ) { index[0]=x; index[1]=y; num=d; next[0] = this; next[1] = this; }
        ~Node() {}
        friend class List;
    };
};

#endif

and my List.cpp like :

#include <iostream>
#include "List.h"

template <typename T>
List<T>::List( const List<T> &l ) {
    head = new Node<T>;
    iterable_frame<1> in(l);
    supervised_frame<1> out(*this);
}

template <typename T> template <bool DIM>
List<T>::supervised_frame<DIM>::supervised_frame( const List<T> &l  ) {
    std::cout << DIM << std::endl;
}
template <typename T> template <bool DIM>
void List<T>::supervised_frame<DIM>::copy_sentinels( iterable_frame<DIM>& ) {}
template <typename T> template <bool DIM>
void List<T>::supervised_frame<DIM>::copy_cells( iterable_frame<DIM>& ) {}

template <typename T> template <bool DIM>
List<T>::iterable_frame<DIM>::iterable_frame( const List<T> &l ) {
    std::cout << '\t' << DIM << std::endl;
}

The code is still draft-like, but I’ll explain a bit my intentions. The idea behind was that I wanted to have a class List that could act as a sparse matrix, so I make first template to easily switch between the number types stored (int, float etc.). Then I create two auxiliary iterator-like classes for handling the elements, that are implemented by templates too, because the direction of the iteration can be twofold – column-wise or row-wise and the different matrix methods would choose one that serves them best. And the list is to be a two dimensional ring.

  • 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-15T16:05:42+00:00Added an answer on June 15, 2026 at 4:05 pm

    The only copy constructor you have declared in your class definition is:

    List( const List &l );
    

    and it has no visible implementation (being a template and all), ergo the error. Move the implementation to the header.

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

Sidebar

Related Questions

Possible Duplicate: Why can templates only be implemented in the header file? I've been
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: Why can templates only be implemented in the header file? I just
Possible Duplicate: Why can templates only be implemented in the header file? “Undefined symbols”
Possible Duplicate: Why can templates only be implemented in the header file? When I
Possible Duplicate: Why can templates only be implemented in the header file? I ran
Possible Duplicate: Why can templates only be implemented in the header file? Undefined symbol
Possible Duplicate: Why can templates only be implemented in the header file? I've been

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.