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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:33:11+00:00 2026-06-15T06:33:11+00:00

Part of the header file dlist.h is defined as: #ifndef __DLIST_H__ #define __DLIST_H__ #include

  • 0

Part of the header file dlist.h is defined as:

#ifndef __DLIST_H__
#define __DLIST_H__
#include <iostream>

class emptyList {};

template <typename T>
class Dlist {
 public:
    bool isEmpty() const;

 private:
    struct node {
    node   *next;
    node   *prev;
    T      *o;
    };

    node   *first; // The pointer to the first node (NULL if none)
    node   *last;  // The pointer to the last node (NULL if none)
};

#include "dlist.cpp"
#endif

When I create a dlist.cpp file like this:

#include "dlist.h"

template <typename T>
bool Dlist<T>::isEmpty() const
{
    return !first and !last;
}

I get the error message at line 4: redefinition of ‘bool Dlist::isEmpty() const’

If I remove the #include "dlist.h" I get the error at line 4: expected initializer before ‘<‘ token

Any help here? Is there something I’m doing wrong that’s not allowing me to just define my functions from the dlist.h file? Thank you.

  • 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-15T06:33:12+00:00Added an answer on June 15, 2026 at 6:33 am

    You have to put the implementation of the class template’s member functions in the header file or in a file included by the header. The compiler needs access to this code in order to instantiate templates for any given type T.

    In your case, the problem seems to be that you are including the header in the .cpp and vice versa. If you really want to keep declaration and implementation in separate files, I suggest changing the implementation’s suffix from .cpp to something else, e.g. .icpp. Some build systems might try to compile an object file out of anything with a .cpp suffix, and this would also result in error.

    1. Remove #include "dlist.h" from dlist.cpp.
    2. (optional) rename dlist.cpp to something like dlist.icpp. Why? Because many build systems automatically compile any file ending in .cpp into an object file. And many programmers assume that a .cpp file will be compiled into an object file.
    3. (only if step 2 taken) include the re-named dlist.icpp in dlist.h, as is currently done for dlis.cpp.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem: Header file (part of a library's API): template <typename IterType>
ok, so here's my header file (or at least part of it): template<class T>
This is part of my header file aes_locl.h : . . # define SWAP(x)
I have a C header file (it's a part of some SDK) and there
The linux/ folder contains the ip.h header file, wherein part of the structure of
In one header file I have: #include BaseClass.h // a forward declaration of DerivedClass,
I have the following function defined in a header file (the library it is
I just learned that defining a c++ function inside a class's header file make
I have some constants defined in a header file that contain the max length
I have a class file (header and cpp) that I made, that I want

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.