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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:54:29+00:00 2026-06-02T13:54:29+00:00

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

  • 0

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

I got the following error while compiling in g++ 4.5.5 on Linux while allegedly my code works fine on Windows.

A.o: In function `A::NewObj(int)':
A.cpp:(.text+0x39): undefined reference to `List<A>::Add(A*)'
collect2: ld returned 1 exit status
make: *** [program] Error 1

I have no idea what is wrong with my code. I’m not sure if I declared object list_A correctly.

The files:

A.h

#ifndef A_H
#define A_H

class A
{
public:
    int key;
    A() { key=0; }
    A(int i) { key = i; }
    void NewObj(int i);
};

#endif

A.cpp

#include "A.h"
#include "List.h"

static    List<A> *list_A = new List<A>();

void A::NewObj(int i)
{
    A *nobject = new A(i);
    list_A->Add( nobject );

}

List.h

#ifndef LIST_H
#define LIST_H

template<typename Objct>
class Node
{
public:
    Objct* datapiece;
    Node *next, *previous;

    Node() { next = previous = 0; }
    Node ( Objct *object, Node *n = 0, Node *p = 0)
    {
        datapiece = object;
        next = n; previous = p;
    }
};

template<typename Objct>
class List
{
public:
    List() { head = tail = 0; }
    void Add( Objct* );
protected:
    Node<Objct> *head, *tail;
};

#endif

List.cpp

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

template<typename Objct>
void List<Objct>::Add( Objct *o )
{
    int i =5;
    Node<Objct> *nnode = new Node<Objct>(o);
    if ( o->key < i )
    std::cout << "Ok" << std::endl;
}

main.cpp

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

int main()
{
    A *magazyn = new A();
    magazyn->NewObj(6);
}

makefile

CC=g++
CFLAGS=-c -Wall -pedantic

program: List.o A.o main.o
    $(CC) List.o A.o main.o -o program
A.o: A.cpp List.h
    $(CC) $(CFLAGS) A.cpp
List.o: List.cpp
    $(CC) $(CFLAGS) List.cpp
main.o: main.cpp A.h
    $(CC) $(CFLAGS) main.cpp
  • 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-02T13:54:30+00:00Added an answer on June 2, 2026 at 1:54 pm

    The problem is the fact that your template class List needs to have its methods defined in every compilation unit that uses it. Templates usually don’t follow the one header, one implementation file rule. See this related question for more information: Why can templates only be implemented in the header file?

    • 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? When I
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've been
Possible Duplicate: Why can templates only be implemented in the header file? I've struggled
Possible Duplicate: Why can templates only be implemented in the header file? hello everyone
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Can the template parameters of a constructor be explicitly specified? following up
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got

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.