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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:43:20+00:00 2026-06-02T00:43:20+00:00

I am trying to implement my own OrderedList data structure using class templates. Each

  • 0

I am trying to implement my own OrderedList data structure using class templates. Each constructor and function in my CPP file has an error on the opening ‘{‘. The error reads “redefinition of FUNCTION_NAME FUNTION_NAME previously declared here”.

I’ve had a few colleagues look at it, but to no avail. Here are my files and the error:

CPP FILE “MyOrderedList.cpp”

#include "MyOrderedList.h"

template <class E>
MyOrderedList<E>::MyOrderedList() {/*IMPLEMENTATION*/}

template <class E>
MyOrderedList<E>::MyOrderedList(const MyOrderedList<E>& orig) { /*IMPLEMENTATION*/}

template <class E>
void MyOrderedList<E>::operator =(const MyOrderedList<E>& orig){/*IMPLEMENTATION*/}

template <class E>
MyOrderedList<E>::~MyOrderedList() {/*IMPLEMENTATION*/}

template <class E>
void MyOrderedList<E>::insert(E data){/*IMPLEMENTATION*/}

template <class E>
E MyOrderedList<E>::get(int pos) const{/*IMPLEMENTATION*/}

template <class E>
Node<E>* MyOrderedList<E>::getHead() const{/*IMPLEMENTATION*/}

template <class E>
MyOrderedList<E> MyOrderedList<E>::kLargest(int k) const{/*IMPLEMENTATION*/}

template <class E>
MyOrderedList<E> MyOrderedList<E>::operator +(const MyOrderedList<E>& list {/*IMPLEMENTATION*/}

H FILE “MyOrderedList.h”

#ifndef MYORDEREDLIST_H
#define MYORDEREDLIST_H

#include <iostream>
#include <cstdlib>
#include "Node.h"

template <class E>
class MyOrderedList;
template <class E>
std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list);

template <class E>
class MyOrderedList {
public:
    MyOrderedList();
    MyOrderedList(const MyOrderedList<E>& orig);
    void operator =(const MyOrderedList<E>& orig);
    virtual ~MyOrderedList();

    bool remove(E data);
    MyOrderedList<E> kLargest(int k) const;
    E get(int pos) const;
    void insert(E data);
    Node<E>* getHead() const;

    MyOrderedList<E> operator +(const MyOrderedList<E>& list);

    friend std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list){/*IMPLEMENTATION*/}

private:
    Node<E>* head;
    int size;
};

#include "MyOrderedList.cpp"
#endif  //MYORDEREDLIST_H

Node.h

#ifndef NODE_H
#define NODE_H

#include <iostream>

template <class E>
class Node {
public:
    Node(const E& init_data = NULL, Node<E>* init_link = NULL){data = init_data; link = init_link;}
    Node(const Node<E>& orig);
    virtual ~Node();

    E getData() const{return data;}
    void setData(E newData);

    Node<E>* getLink(){return link;}
    void setLink(Node<E>* nextLink) {link = nextLink;}
private:
    E data;
    Node<E>* link;
};

#endif  /* NODE_H */

Compiler Errors

MyOrderedList.cpp:12: error: redefinition of `MyOrderedList<E>::MyOrderedList()'
MyOrderedList.cpp:12: error: `MyOrderedList<E>::MyOrderedList()' previously declared here
MyOrderedList.cpp:19: error: redefinition of `MyOrderedList<E>::MyOrderedList(const MyOrderedList<E>&)'
MyOrderedList.cpp:19: error: `MyOrderedList<E>::MyOrderedList(const MyOrderedList<E>&)' previously declared here
MyOrderedList.cpp:42: error: redefinition of `void MyOrderedList<E>::operator=(const MyOrderedList<E>&)'
MyOrderedList.cpp:42: error: `void MyOrderedList<E>::operator=(const MyOrderedList<E>&)' previously declared here
MyOrderedList.cpp:77: error: redefinition of `MyOrderedList<E>::~MyOrderedList()'
MyOrderedList.cpp:77: error: `virtual MyOrderedList<E>::~MyOrderedList()' previously declared here
MyOrderedList.cpp:91: error: redefinition of `void MyOrderedList<E>::insert(E)'
MyOrderedList.cpp:91: error: `void MyOrderedList<E>::insert(E)' previously declared here
MyOrderedList.cpp:119: error: redefinition of `E MyOrderedList<E>::get(int) const'
MyOrderedList.cpp:119: error: `E MyOrderedList<E>::get(int) const' previously declared here
MyOrderedList.cpp:134: error: redefinition of `Node<E>* MyOrderedList<E>::getHead() const'
MyOrderedList.cpp:134: error: `Node<E>* MyOrderedList<E>::getHead() const' previously declared here
MyOrderedList.cpp:140: error: redefinition of `MyOrderedList<E> MyOrderedList<E>::kLargest(int) const'
MyOrderedList.cpp:140: error: `MyOrderedList<E> MyOrderedList<E>::kLargest(int) const' previously declared here
MyOrderedList.cpp:158: error: redefinition of `MyOrderedList<E> MyOrderedList<E>::operator+(const MyOrderedList<E>&)'
MyOrderedList.cpp:158: error: `MyOrderedList<E> MyOrderedList<E>::operator+(const MyOrderedList<E>&)' previously declared here
  • 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-02T00:43:21+00:00Added an answer on June 2, 2026 at 12:43 am

    Your problem is that you are including the definition of the functions in the header (indirectly through the #include "MyOrderedList.cpp". Because the functions are not declared as inline the compiler is generating the symbols in all translation units that include the header and thus you get multiply defined symbols.

    One solution to the problem is declaring the functions as inline in the definition, so that the linker can discard the duplicates. Keeping the definitions in a separate file is not something new, but it is not too common either, some standard library implementations do that. But consider renaming the .cpp to something else to avoid confusion (most people will think that .cpp means that it is meant to be compiled, not included).

    I would recommend, on the other hand, to simplify it a bit more, remove the .cpp altogether and just have the .h with everything (still mark all function definitions as inline) or even go one step further and define the functions inside the class definition, where they are inline by default.

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

Sidebar

Related Questions

I'm trying to implement my own List system in Java. the List class file
I am trying to implement my own File class, which extends from the java.io.File
I'm trying to implement my own version of vector class without using iterators. Here
I'm trying to implement the Strategy pattern using Core Data and Objective C. To
I'm trying to implement my own serialization / var_dump style function in PHP. It
I'm trying to implement my own list class but am having trouble reversing just
I'm trying to implement my own stream manipulator inside my logging class. It's basically
I have been trying to implement my own linked list class for didactic purposes.
I am trying to implement my own remote desktop solution in java. Using sockets
I am trying to implement a very simple file transfer client in python using

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.