简体   繁体   中英

C++ Error: "There are no arguments to 'setw' that depend on a template parameter

This is the code:

template <class TYPE, class KTYPE>
void  AvlTree<TYPE, KTYPE> :: _print (NODE<TYPE> *root, int level)
{
    int i;

    if (root)
        {
         _print ( root->right, level + 1 );

         cout << "bal "     << setw(3) << root->bal
              << ": Level " << setw(3) << level;

         for (i = 0; i <= level; i++ )
            cout << "....";
         cout << setw(3) << root->data.key;
         if (root->bal == LH)
            cout << " (LH)\n";
         else if (root->bal == RH)
            cout << " (RH)\n";
         else
            cout << " (EH)\n";

         _print ( root->left, level + 1 );
        }

}

This is the driver im using:

AvlTree<node, int> tree;
if (tree.AVL_Empty())
    cout << "tree is empty\n";

node newItem;
newItem.word = "ryan";
newItem.key = 1;
tree.AVL_Insert(newItem);
tree.AVL_Print();

return 0;

The error im getting is:

error: there are no arguments to 'setw' that depend on a template parameter, so a declaration of 'setw' must be available [-fpermissive]

error: 'setw' was not declared in this scope

I have tried using the "this->" as per other similar questions with no luck. the errors disappears but its replaced with this error:

error: 'class AvlTree<node, int>' has no member named 'setw'

The problem is that you aren't including <iomanip> . You need to include that header file because that's what actually declares std::setw .

Notice the compiler error is pretty clear on this: 'setw' was not declared in this scope . It's telling you it doesn't know what setw is. Since setw comes from the <iomanip> header, you need to include that header so the compiler is properly informed and knows what setw is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM