Mixing templates and inheritance, and the damage caused ;)

This is a discussion on Mixing templates and inheritance, and the damage caused ;) within the C++ Programming forums, part of the General Programming Boards category; I get like 30 errors when I try to compile the following. It is intended to be used something like ...

  1. #1
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141

    Mixing templates and inheritance, and the damage caused ;)

    I get like 30 errors when I try to compile the following. It is intended to be used something like this.

    Code:
    Parser parser("inputfile.txt");
    parser.addAction("PlayerWeight",new stdRead<float>(someFloat));
    parser.addAction("XWidth",new readAndCheck<int>(xWidth, xWidthChanged);
    parser.read();
    
    if (!xWidthChanged) {
        xWidth=DEFAULT_X_VALUE;
    }
    
    inputfile.txt
    
    PlayerWeight=2.1e2
    xWidth=50


    I can't understand any of the compiler error messages . When trying to extend a class with a template, how does one say, derived<T> inherits base<T>? I have a feeling that is the root of my problem.

    Code:
    //Parse.h
    
    // /*
    
    #ifndef PARSE_H_
    #define PARSE_H_
    
    #include <string>
    #include <fstream>
    #include <map>
    
    using namespace std;
    
    class ParseAction;
    
    typedef map<const string, ParseAction*> ActionMap_t;
    
    class Parser {
    public:
           Parser(istream &inputFile);
           ~Parser();
           
           /* NOTE: calls to this addAction() must pass a dynamic ParseAction* via new, 
              else the destructor will cause a crash */
           void addAction(const string& identifier, ParseAction* action);      
           void read();
    private:
            ActionMap_t actionMap;
            ifstream& input;        
    };
    
    class ParseAction {
    public:
          virtual void getValue(istream& input)=0;
    };
          
    template <class T> 
    class StdRead: public ParseAction {
    public:
           StdRead(T& object);
           virtual void getValue(istream& input);
    protected:
            T& obj;            
    };
    
    template <class T>
    class  ReadAndCheck: public StdRead {
    public:
           ReadAndCheck(T& obj, bool& objectSet_);
           virtual void getValue(istream& input);
    protected:
              bool& objectSet;
    };
    
    template <class T>
    class ReadAndMult: public StdRead {
    public:
           ReadAndMult(T& object, T mult);
           virtual void getValue(istream& input);
    protected:
              T multiplier;
    };
    
    #endif // ifndef PARSE_H_
    
    // */
    The errors are the following.

    1 parsetestmain.cpp
    c:\code\c++\gravitysim\parse.h:47: parse error before `{'

    50 parse.h
    virtual outside class declaration

    51 parse.h
    parse error before `protected'

    56 parse.h
    parse error before `{'

    59 parse.h
    virtual outside class declaration

    60 parse.h
    parse error before `protected'

    3 parse.cpp
    c:\code\c++\gravitysim\parse.h:47: parse error before `{'

    50 parse.h
    virtual outside class declaration

    51 parse.h
    parse error before `protected'

    56 parse.h
    parse error before `{'

    59 parse.h
    virtual outside class declaration

    60 parse.h
    parse error before `protected'

    5 parse.cpp
    prototype for `Parser::Parser(ifstream &)' does not match any in class `Parser'

    30 parse.h
    candidates are: Parser::Parser(const Parser &)

    20 parse.h
    Parser::Parser(istream &)

    12 parse.cpp
    no match for `_Rb_tree_iterator<pair<const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >,ParseAction *>,pair<const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >,ParseAction *> &,pair<const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >,ParseAction *> *> & < _Rb_tree_iterator<pair<const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >,ParseAction *>,pair<const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >,ParseAction *> &,pair<const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >,ParseAction *> *>'

    52 parse.cpp
    syntax error before `::'

    57 parse.cpp
    syntax error before `::'

    62 parse.cpp
    syntax error before `::'

    68 parse.cpp
    syntax error before `::'

    70 parse.cpp
    ANSI C++ forbids declaration `objectSet' with no type

    71 parse.cpp
    parse error before `}'

    57 parse.cpp
    syntax error before `::'

    62 parse.cpp
    syntax error before `::'

    68 parse.cpp
    syntax error before `::'

    70 parse.cpp
    ANSI C++ forbids declaration `objectSetŠµä
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    incremental testing...

    code... compile... pray... repeat...

    break it down into more simple pieces with minimal interactions... then build up...
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    This one hit me as I was scrolling your code:
    Code:
    template <class T>
    class  ReadAndCheck: public StdRead {
    When you derive from template, you must specify its type:
    Code:
    template <class T>
    class  ReadAndCheck: public StdRead<T> {
    Making error is human, but for messing things thoroughly it takes a computer

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21