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 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

Related Questions

I'm trying to create a Quicksort base class using VB.NET, taking it an array
I'm trying to create a template class, and when I define a non-member template
Using extension syntax I'm trying to create a left-join using LINQ on two lists
I'm trying to create a sitemap using Linq to Xml, but am getting an
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
I am trying to create a countdown using javascript. I got some code from
I am trying to create a menu using anchor tags, and they should butt
I'm trying to create a DLL using a Dataset.XSD file to access my website's
I'm trying to create a horizontal 100% stacked bar graph using HTML and CSS.
I'm trying to create a new Excel file using jxl, but am having a

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.