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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:22:41+00:00 2026-06-01T20:22:41+00:00

I have a class Stack, using template, one of its methods is push, which

  • 0

I have a class Stack, using template, one of its methods is “push”, which is written below:

template <class T>
void Stack<T>::push(T _data){
    Node<T>* temp = new Node<T>;
    temp->data = _data;
    temp->next = head;
    head = temp;
}

The stack works well with int, double, string, char….
But it says

prog.cpp:32: note: synthesized method ‘Node<Tree>::Node()’ first required here

when I use a class “Tree” as data type.
I don’t understand, why it works with “string” but not with “Tree”, they are both classes, not primitive types.

http://ideone.com/NMxeF
(Ignore the other error, my IDE only gives one error at line 32 and some warnings)

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-06-01T20:22:43+00:00Added an answer on June 1, 2026 at 8:22 pm

    Edit after reading the actual code (the “note” shown above is fairly misleading about the real problem).

    Looking at the code, where you try to use new Node<T>;, that needs a default constructor for T (which in this case is Tree) because your Node template contains an instance of T:

    struct Node {
        T data;    // <--- instance of T, not being initialized in your code.
        Node *next;
    };
    

    Tree doesn’t have a default constructor, so that fails (and the note is showing you where the default constructor would be needed).

    You have a few choices about how to fix that. The most obvious would be for a Node to hold either a pointer or a reference to a T instead of containing an actual instance of T.

    Another would be to have Node‘s constructor take a reference to a (probably const) T, and copy that T into the Node:

    class Node { 
        T data;
        Node *next;
    public:
        Node(T const &dat) : data(dat), next(0) {}
    };
    

    The choice between these two approaches is fairly fundamental. If you have Node store a pointer/reference to T, then it will be the responsibility of calling code to ensure the passed object remains valid as long as the Node exists. The node and calling code will share access to a single instance of T.

    By contrast, if you copy the passed object into the Node, then this copy will be destroyed when the Node is destroyed. The original T (Tree, in your case) you passed to the Node will remain the responsibility of the calling code, and the Node will take responsibility for its copy.

    In the usual case, you’d tend to favor the latter — it gives cleaner semantics, and keeps ownership of the data clear. In the case of a Tree, however, you probably don’t want to copy an entire tree into a Node if you can avoid it. One compromise position would be to use something like a Node<shared_ptr<Tree> > instead. The shared_ptr can keep copying fast and cheap, while avoiding writing a Node that’s only suitable for a few kinds of objects and situations. That also makes fairly explicit that you’re storing only a pointer that gives shared access to the original object.

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

Sidebar

Related Questions

I have to write a stack class template using arrays in C++ for my
I have some code below. This code is a basic push/pop stack class that
For my compsci class, I am implementing a Stack template class, but have run
In this I need C++ array class template, which is fixed-size, stack-based and doesn't
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
We've written a smart pointer class and have been using it with great success
I have 3 C++ files: genericStack.h: template <class T> class Stack{ public: Stack (int
I have class A, which exposes an event. an object of class B subscribed
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have just started with Gui Programming in netbeans (Using the template Java Desktop

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.