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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:27:59+00:00 2026-06-11T10:27:59+00:00

Error stack.cc:53:28: error: no matching function for call to ‘Stack<std::basic_string<char> >::push(std::string)’ stack.cc:53:28: note: candidate

  • 0

Error

stack.cc:53:28: error: no matching function for call to ‘Stack<std::basic_string<char> >::push(std::string)’
stack.cc:53:28: note: candidate is:
stack.cc:32:11: note: Stack<T>& Stack<T>::push(T&) [with T = std::basic_string<char>]

stack.cc

#include<iostream>

template <typename T>
class Stack {
private:
    T* array_;
    int length_;
    T* last_;
    void expandArray();

public:
    Stack(int length = 8) {
        array_   = new T[length];
        length_  = length;
        last_    = array_;
    }

    Stack<T>& push(T&);
    T pop();
};

template<typename T>
void Stack<T>::expandArray() {
    T* array_temp = new T[length_ << 1];
    memcpy(array_temp, array_, length_);
    std::swap(array_, array_temp);
    delete[] array_temp;
    length_ <<= 1;
}

template<typename T>
Stack<T>& Stack<T>::push(T& data) {
    if (last_ == (array_ + length_ - 1)) {
        expandArray();
    }
    last_[0] = data;
    last_++;
    return *this;
}

template<typename T>
T Stack<T>::pop() {
    if(array_ != last_) {
        T temp = last_[0];
        last_--;
        return temp;
    }
    return NULL;
}

int main() {
    Stack<std::string> s;
    s.push(std::string("a"))
     .push(std::string("b"))
     .push(std::string("c"))
     .push(std::string("d"));
    std::cout << s.pop() << std::endl;
    std::cout << s.pop() << std::endl;
    std::cout << s.pop() << std::endl;
    std::cout << s.pop() << std::endl;
}

Wanted to understand why a conversion is happening from std::string to std::basic_string<char>?

Please feel free to comment on the code quality as well.

  • 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-11T10:28:01+00:00Added an answer on June 11, 2026 at 10:28 am

    The actual problem is that you’re attempting to pass a reference to an object created in the argument rather than stored in a variable. See that T& argument to Stack<T>::push? You cannot pass your temporary here because it’s a non-const reference. Try as follows…

    Stack<T>& push(const T&);
    

    Use this signature for your implementation, too.


    That being said, know that std::string is a mere typedef over std::basic_string<char>. This is because the functionality for strings can be extended to the other character types, too — std::wstring for wchar_t, std::u16string for char16_t, and std::u32string for char32_t. 😉

    See §21.4 Class template basic_string [basic.string] of the C++11 specification.

    // basic_string typedef names
    typedef basic_string<char> string;
    typedef basic_string<char16_t> u16string;
    typedef basic_string<char32_t> u32string;
    typedef basic_string<wchar_t> wstring;
    

    Note that you shouldn’t rely on iostream to include string for you. You should also include cstring and specify the scope for your memcpy call.


    By the way, you should really look into using an initializer list for Stack‘s constructor… see as follows.

    Stack(int length = 8) : length_(length), array_(new T[length]), last_(array_) { }
    

    Note that this functioning properly on array_ preceding last_ as a class member declaration 😉


    … AND a final note. Your pop is incorrect, since you’re returning the element past the top. Instead, try something as follows…

    template<typename T>
    T Stack<T>::pop() {
        if (array_ != last_) {
            return *--last_;
        }
        /* other stuff here */
    }
    

    You need to decrement prior to dereferencing, since last_ points past the top. As a side note, you’re also returning a copy of the std::string from pop, even though you stated you wanted to avoid as such.

    Note you shouldn’t be returning NULL since this isn’t a pointer type. In fact, you’ll just be creating an std::string via the constructor that takes a const char *… which is explicitly prohibited in the case of NULL. See §21.4.2¶8-9…

    basic_string(const charT* s, const Allocator& a = Allocator());
    

    Requires: s shall not be a null pointer.

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

Sidebar

Related Questions

The command virtualenv --no-site-packages env/ produces the following error stack trace: The --no-site-packages flag
What are the usual causes of Error C stack overflow in the Hugs Haskell
My app crashes several times without any error or stack trace on my console.
What is a stack overflow error? What type of programs/programming languages is it likely
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
I'm stumped about a stack overflow error--out of stack space (application error code: 12246)--that
Ok so I have a recursion problem thats causing a stack overflow error at
I am getting this error: integerMultiplication.rb:4:in `untMul': stack level too deep (SystemStackError) in this
Until now I was logging the Error message and the stack trace of an
Is the most likely cause of an Error 28 - Out of stack space

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.