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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:45:40+00:00 2026-05-24T18:45:40+00:00

This is my first foray into C++ templates, and I’m trying to construct a

  • 0

This is my first foray into C++ templates, and I’m trying to construct a BinaryTree template to help me with a Project Euler problem; however, I seem to be getting an error where BinaryTree class doesn’t recognize all the constructors of the BinaryTreeNode! Here’s a snippet of the code.

template <class T>
class BinaryTreeNode
{
private:
    BinaryTreeNode<T>* _left;
    BinaryTreeNode<T>* _right;
    T* _value;

public:
    BinaryTreeNode();
    explicit BinaryTreeNode(const T& value) : _value(&(T(value))) {}
    BinaryTreeNode(BinaryTreeNode<T>& left, BinaryTreeNode<T>& right, const T& value) :
        _left(&left), _right(&right), _value(&(T(value))){}
};

The BinaryTree class

#include "BinaryTreeNode.h"
template <class T>
class BinaryTree
{
private:
    BinaryTreeNode<T>* _root;
    BinaryTreeNode<T>* _current;
    unsigned int size;

public:
    BinaryTree() : size(0), _root(0), _current(0) { }
    explicit BinaryTree(BinaryTree<T>& leftTree, BinaryTree<T>& rightTree, const T& value) : 
        size(leftTree.Size() + rightTree.Size() + 1), _root(leftTree.Root(), rightTree.Root(), value), _current(_root) {}
    explicit BinaryTree(const T& value) : size(1), _root(value) {}
    const BinaryTreeNode<T>& Root() const { return *_root;}
};

I’m getting these errors.

error C2359: 'BinaryTree<T>::_root' : member of non-class type requires single initializer expression
error C2440: 'initializing' : cannot convert from 'const int' to 'BinaryTreeNode<T> *'
error C2439: 'BinaryTree<T>::_root' : member could not be initialized

The BinaryTreeNode constructor of (BinaryTreeNode<T>&, BinaryTreeNode<T>&, const T& value) works when I include it in my main code, but it doesn’t seem to work under my BinaryTree template. Anyone know why?

  • 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-24T18:45:41+00:00Added an answer on May 24, 2026 at 6:45 pm

    In your initialization expression _root(leftTree.Root(), rightTree.Root(), value), _root is a pointer. You can only initialize it to another pointer. Perhaps you mean to initialize it to a pointer to a new node constructed on those arguments?

    This could be done like this: (updated after your edit)

    _root(new BinaryTreeNode<T>(leftTree.Root(), rightTree.Root(), value))
    

    However, this is very dangerous (think about an exception in the allocation), and you should probably avoid using raw pointers in your class design and instead use smart managing pointers.

    Similarly, the initializer _root(value) does the wrong thing, you might want:

    _root(new BinaryTreeNode<T>(value))
    

    (Also note that you should initialize members in their order of declaration.)

    Update: I changed the first constructor call following your edit, but as @Luc says, your constructors take non-const arguments but Root() only provides a const reference, so you still need to fix that.

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

Sidebar

Related Questions

This is my first foray into using an MVC construct (CodeIgniter). I'm hoping someone
http://jsfiddle.net/nf8NM/3/ This is my first foray into Backbone and I am simply trying to
problem euler #5 i found the solution but i don't know why this first
I've worked with CakePHP 1.3, but this is my first foray into CakePHP 2.0
This is my first foray into designing two versions of images for retina or
This is my first foray into shell scripting so if i'm asking a very
I'm a (near complete) beginner, and this is my first foray into encryption -
I am following this tutorial as a first foray into bootloader/OS development for x86
This is my first foray into OO JS, having issues. Ideally in this scenario,
Well, this is my first foray into memory profiling a .NET app (CPU tuning

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.