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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:37:37+00:00 2026-05-11T19:37:37+00:00

I am using C++ and I am trying to create a templated class (a

  • 0

I am using C++ and I am trying to create a templated class (a Stack).
I would like to define the copy constructor and the assignment operator.

There are defined in the header and then I implement them in the cpp file.

Here are the problems I get:
– For the copy constructor:
prototype for ‘Stack::Stack(const Stack&)’ does not match any in class ‘Stack’
– For the assignement operator:
stack.cpp:28: error: no ‘Stack& Stack::operator=(const Stack&)’ member function declared in class ‘Stack’
– For the constructor:
stack.cpp:4: error: candidate is: Stack::Stack()

Here is the header file:

// stack.hpp

#ifndef STACK_HPP
#define STACK_HPP


#include <stdio.h>
#include <assert.h>


template <class T> 
class Stack
{

public:
  Stack();
  ~Stack();

  Stack(const Stack&);
  Stack& operator=(const Stack&);


private:
  T* v_;
  size_t vsize_;
  size_t vused_;

};

Here is the cpp file:

// stack.cpp

#include "stack.hpp"

template <class T> 
Stack<T>::Stack() :
  v_(0), 
  vsize_(10), 
  vused_(0) 
{
  v_ = new T[vsize_];
}

template <class T> 
Stack<T>::~Stack()
{
  delete[] v_;
}


//  Stack(const Stack&);
template <class T> Stack<T>::Stack( const Stack<T>& other) :
  v_(NewCopy(other.v, other.vsize_, other.vsize_)),
  vsize_(other.vsize_),
  vuser_(other.vused)
{
}

//  Stack& operator=(const Stack&);
template<class T> Stack<T>& Stack<T>::operator=(const Stack<T>& other)
{
  if (this != &other)
  {
    T* v_new = NewCopy(other.v_, other.vsize_, other.vsize__;
    delvete v_;
    v_ = v_new
    vsize_ = other.vsize_;
    vused_ = other.vused_;
  }
  return *this

}

One last thing, here is the log from the compilation:

g++ -c stack.cpp -o stack.o 
stack.cpp:20: error: prototype for ‘Stack<T>::Stack(const Stack<T>&)’ does not match any in class ‘Stack<T>’
stack.cpp:4: error: candidate is: Stack<T>::Stack()
stack.cpp:28: error: no ‘Stack<T>& Stack<T>::operator=(const Stack<T>&)’ member function declared in class ‘Stack<T>’

I am sure this is only a small typo error but I cannot seem to find it.
Thanks for your help

  • 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-11T19:37:37+00:00Added an answer on May 11, 2026 at 7:37 pm

    You can’t compile templates into separate compilation units. In short, all the template code has to all be in one header. This is likely whats causing your problem.

    The reason is that templates don’t define true classes. Templates specify how code can be generated. So when you make a

     Stack<int> myStack;
    

    The compiler uses the template to generate a copy constructor:

     Stack<int>::Stack<int>( const Stack<int>& src);
    

    This is a completely different type than a

     Stack<float>
    

    which will define a completely independent copy constructor, and completely independent set of methods.

    One option many use is to include the cpp in the header, in a reverse kind-of-way.

    ie in Stack.hpp, at the bottom

    #include "Stack.cpp"
    

    but that kind of hides the fact that really its all just plopped into one header.

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

Sidebar

Ask A Question

Stats

  • Questions 182k
  • Answers 182k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: function getXML() { var XML = null; $.ajax({… May 12, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer Considering your additional requirements posted as comment to Checkers' reply… May 12, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer Have you just tried the EmbeddedResources property? Dim resources As… May 12, 2026 at 4:32 pm

Related Questions

I need some mind reading here, since I am trying to do what I
I am trying to create a simple dialog in MFC using Visual C++. My
I am trying to create an app in Qt/C++ with Qt4.5 and want the
I've been trying to solve this for ages (3 days) now and I just

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.