Thread: Confusing Template error

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Confusing Template error

    Quote Originally Posted by Evil g++
    In file included from main.cpp:3:0:
    scope.h: In function ‘double mm::get_num(const string&, const mm::scope<std::basic_string<char> >&)’:
    scope.h:40:30: error: passing ‘const mm::scope<std::basic_string<char> >’ as ‘this’ argument of ‘T* mm::scope<T>::find(const string&) [with T = std::basic_string<char>, std::string = std::basic_string<char>]’ discards qualifiers [-fpermissive]
    make: *** [main.o] Error 1
    main.cpp has nothing important now...
    Here is the scope.h
    Code:
    #ifndef SCOPE
    #define SCOPE
    #include<vector>
    #include<map>
    #include<sstream>
    namespace mm
    {
        template<typename T>
        class scope
        {
            std::vector< std::map<std::string,T> > data;
        public:
            T* find(const std::string& s)
            {
                for(auto m=data.rbegin();m!=data.rend();m++)
                {
                    auto p = m->find(s);
                    if(p!=m->end())return &(p->second);
                }
                return nullptr;
            };
            void new_local(const std::map<std::string,T>& sm){data.push_back(sm);};
            void exit_scope(){data.pop_back();};
        };
    
        /*Converts a string to a double
         * If the string is an identifier....
         * it fetches the value from the scope object supplied. 
         */
        double get_num(const std::string& s, const scope<std::string>& ss)
        {
            std::string numstr;
            std::istringstream is(s);
            double d;
            if(is>>d)
                return d;
            else 
            {
                std::string* x = ss.find(s);
                if(x!=nullptr)numstr=*x;
                else throw("NAN");//change later
            }
            is.str(numstr);
            if(is>>d)
                return d;
            else throw("NAN");//change later
            
        }
    }
    
    #endif

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I think the most important part of your error message is "discards qualifiers", this seems to be a const correctness issue.

    Jim

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Correct you are; have my thanks, you do.

    Problem solved by making the scope passed into the get_num not a const.
    (Any idea why the error was there when I never modified the object ?)

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Jim is correct - specifically, ss is declared const, but find() is not a const method, so you can't call it on ss.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Bah - too late!

    Quote Originally Posted by manasij7479 View Post
    (Any idea why the error was there when I never modified the object ?)
    Define "never modified the object". If you mean that find() doesn't modify the object, that's not enough - in order to be able to use it on const-qualified instances of your class, you have to declare find() to be const to let the compiler (a) know that and (b) enforce it within the method.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by JohnGraham View Post
    Define "never modified the object". If you mean that find() doesn't modify the object, that's not enough - in order to be able to use it on const-qualified instances of your class, you have to declare find() to be const to let the compiler (a) know that and (b) enforce it within the method.
    Yes.. I didn't put const because I'd use the pointer returned by it to change the object (in other cases..not this)
    But I now realize that the 'viral-ity' of const won't allow that.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If a function takes a reference or pointer to something and returns a non-const reference or pointer to that same object, then the function itself can be const, but the parameter cannot.
    Typically, the solution is to overload both variants.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confusing error
    By Creatlv3 in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2010, 07:05 AM
  2. confusing error :S
    By teadrinker in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2008, 11:57 AM
  3. Really confusing error
    By Ganoosh in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2005, 11:07 AM
  4. confusing error - please have a look at my code
    By IceBall in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 01:44 PM
  5. confusing error.... plz help
    By heavyd in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2002, 08:44 PM