Newbie subclassing and header question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cypherzero
    New Member
    • Jul 2007
    • 15

    Newbie subclassing and header question

    I've defined a subclass of std::string in 'mstring.h' and the main program in 'main.cpp'

    mstring.h contains the full class definition (as follows) and main.cpp contains an #include "mstring.h" command, yet I recieve over 100 errors telling me theres no such class as MSTRING. I feel I am doing something very simple very wrong, can anyone spot the mistake!?

    Thanks in advance.

    Code:
    ///////////////
    // mstring.h //
    ///////////////
    
    class MSTRING : public std::string
        {
        public:
            bool contains(MSTRING txt)
                {
                if (txt.empty())
                    return true;
                else
                    return (this->lcase().find(txt.lcase()) != npos);
                }
    
            MSTRING lcase(MSTRING i)
                {
                MSTRING r(i);
                std::transform(r.begin(), r.end(), r.begin(), tolower);
                return r;
                }
    
            . . . more code . . .
        }
    
    //////////////
    // main.cpp //
    //////////////
    
    #include <windows.h>
    #include <iostream>
    #include <psapi.h>
    #include <shlobj.h>
    #include <vector>
    #include <cctype>
    #include <string>
    #include <algorithm>
    #include "mstring.h"
    
    . . . lots more code . . .
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by cypherzero
    I've defined a subclass of std::string in 'mstring.h' and the main program in 'main.cpp'

    mstring.h contains the full class definition (as follows) and main.cpp contains an #include "mstring.h" command, yet I recieve over 100 errors telling me theres no such class as MSTRING. I feel I am doing something very simple very wrong, can anyone spot the mistake!?

    Thanks in advance.

    Code:
    ///////////////
    // mstring.h //
    ///////////////
    
    class MSTRING : public std::string
        {
        public:
            bool contains(MSTRING txt)
                {
                if (txt.empty())
                    return true;
                else
                    return (this->lcase().find(txt.lcase()) != npos);
                }
    
            MSTRING lcase(MSTRING i)
                {
                MSTRING r(i);
                std::transform(r.begin(), r.end(), r.begin(), tolower);
                return r;
                }
    
            . . . more code . . .
        }
    
    //////////////
    // main.cpp //
    //////////////
    
    #include <windows.h>
    #include <iostream>
    #include <psapi.h>
    #include <shlobj.h>
    #include <vector>
    #include <cctype>
    #include <string>
    #include <algorithm>
    #include "mstring.h"
    
    . . . lots more code . . .
    Could you post the errors please?

    Comment

    • cypherzero
      New Member
      • Jul 2007
      • 15

      #3
      The errors are all of the type
      Code:
      MSTRING rfilter;
      // error C2146: syntax error : missing ';' before identifier 'rfilter'
      or similar, suggesting that the class hasn't been identified.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by cypherzero
        The errors are all of the type
        Code:
        MSTRING rfilter;
        // error C2146: syntax error : missing ';' before identifier 'rfilter'
        or similar, suggesting that the class hasn't been identified.
        What did you put after the last right curly bracket of your class in your .h file?

        kind regards,

        Jos

        Comment

        • cypherzero
          New Member
          • Jul 2007
          • 15

          #5
          Ok, sorry, I found the error -
          A ';' was needed at the end of the class definition.

          Just read your reply as well - that would be it =o)
          Cheers

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by cypherzero
            Ok, sorry, I found the error -
            A ';' was needed at the end of the class definition.

            Just read your reply as well - that would be it =o)
            Cheers
            It is a nasty little beginners' error; now you know the syntax for the rest of your
            life ;-)

            kind regards,

            Jos

            Comment

            Working...