0

Solved by added a default constructor to the symbol struct, but also I would like to ask why there is a call to a default constructor if possible. If not, it's fine. thanks.

I'm trying to write a tree, but when I define a node like so:

TreeNode<SymbolPriority>* treeRoot =
    new TreeNode<SymbolPriority>(SymbolPriority('a', 1));

I can't compile and it throws an Error c2512 'SymbolPriority': no default appropriate default constructor; however, in my struct I have the constructor I am trying to use, and I have used it before, so I do not know what is going on.

I have tried this:

SymbolPriority aSymbol( 'a', 1 );

TreeNode<SymbolPriority>* treeRoot = new TreeNode<SymbolPriority> (aSymbol);

but it doesn't work either.

I put down the relevant code below:

template<typename DATA_TYPE> struct TreeNode
{
TreeNode(const DATA_TYPE& value, TreeNode* left = NULL, TreeNode* right = NULL)
{
  Value = value;
  Left = left;
  Right = right;
}

DATA_TYPE Value;
TreeNode* Left;
TreeNode* Right;

bool IsLeaf() const
{
  return Left == NULL && Right == NULL;
}

};

and

struct SymbolPriority
{
   SymbolPriority(char aSymbol, int priority){
       Symbol = aSymbol;
       Priority = priority;
   };

   char Symbol;
   int Priority;

   bool operator > (const SymbolPriority& compareTo) const{
        return (Priority > compareTo.Priority );
   };

   bool operator < (const SymbolPriority& compareTo) const{
    return !( *this > compareTo);
   };

   bool operator==(const SymbolPriority& compareTo) const{
        return (Priority == compareTo.Priority );
   };

};
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

Default constructors are used to initialize objects without use of arguments. To fix this, simply put:

TreeNode() { // NO PARAMETERS
    ... // default initializing variables, or whatever you'd like
}

In your structures, classes, and any others if they are declared without arguments.

In case you'd like to hear more on the concept of "Default Constructors".


There are quite a few stackoverflow questions relating to Error: C2512.
Consider looking at this: error C2512: no appropriate default constructor available


Wikipedia happens to give a great explanation on the necessity of default constructors here.
Some notable purposes are:

  • "When an array of objects is declared, e.g. MyClass x[10];; or allocated dynamically, e.g. new MyClass [10]; the default constructor is used to initialize all the elements"

  • "When an object value is declared with no argument list, e.g. MyClass x;; or allocated dynamically with no argument list, e.g. new MyClass or new MyClass(); the default constructor is used to initialize the object"

  • "When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called"

That last one is important for inheritance.

Community
  • 1
  • 1
Radnyx
  • 210
  • 2
  • 14
  • I considered that, but constructing the symbol outside of the instantiation doesn't work either (did i use that word right)? –  Dec 03 '13 at 05:49
  • @user1375469 You must have tried initializing your object without any constructor type-arguments. Don't forget the type-arguments. – Radnyx Dec 03 '13 at 05:51
  • Okay, I've added default constructors to the symbol struct, and it works now, but I'm confused as to why a call to a default constructor is needed? –  Dec 03 '13 at 05:57
  • @user1375469 The compiler needs to know how to initialize your structure in case no parameters are give. Otherwise it's values (correct me if I'm wrong) MAY give "unknown results". – Radnyx Dec 03 '13 at 06:00