teynon
teynon

Reputation: 8308

not1 was not declared in this scope

I was following some information from this: How to find the first character in a C++ string

When I tried to implement it into my code, I got the error not1 was not declared in this scope.

void ASTree::indent(int ind, int inc) {
    std::string theText;

    for (std::vector<ASTree*>::const_iterator i = child.begin(); i != child.end(); ++i) {
        switch ((*i)->nodeType) {
            case category:
                (*i)->indent(ind + inc, inc);
                break;
            case token:
                {
                //out << std::setw(indent) << " ";
                theText = (*i)->text; // << std::endl;
                std::string::iterator firstChar = std::find_if(theText.begin(), theText.end(), std::not1(std::isspace));
                theText.erase(theText.begin(), firstChar);
                (*i)->text = theText;
                }
                break;
            case whitespace:
                //out << (*i)->text;
                break;
        }
    }
}

I'm somewhat new to C++ and working on these projects for in class.

Upvotes: 1

Views: 2508

Answers (2)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361662

Have you included this header:

#include <functional>

Also, use std::not1, not just not1, for it is defined in std namespace.

I hope you've not written using namespace std in your code, which is a bad idea anyway.


Alright after reading this comment by you:

get yet another error. :) no matching function for call to ânot1(<unresolved overloaded function type>)â I also updated the code above to show you my current

I guess there is another function with name isspace is present in std namespace, which is causing the problem while resolving the names.

So here are two solutions. Try one by one:

  • Use just ::isspace. Without using std. Just ::isspace. See if it works!
  • Or, cast explicitly to help the compiler in selecting the desired overload, as

     std::not1(((int)(*)(int))(std::isspace));
    

Since the casting looks very clumsy, you can use typedef also, as:

 //define this inside the function, or outside the function!
 typedef int (*fun)(int);

 //then do this:
 std::not1((fun)(std::isspace))

I hope this should help you.


A similar problem has been seen before, see this:

Upvotes: 8

Chad
Chad

Reputation: 19052

To use std::not1 you need to #include <functional>, also you need to prefix it with the namespace properly (if you don't have a using directive):

std::string::iterator firstChar = std::find_if(
    theText.begin(),
    theText.end(),
    std::not1(isspace));

Upvotes: 1

Related Questions