Problem with overloaded operators, templates and inheritance

This is a discussion on Problem with overloaded operators, templates and inheritance within the C++ Programming forums, part of the General Programming Boards category; Hi ! I have a base class and a derived class ( which inherits the base class ). I want ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    Problem with overloaded operators, templates and inheritance

    Hi !

    I have a base class and a derived class ( which inherits the base class ).

    I want to derived class to overload the [] operator, so that it can perform certain operations and also call the [] operator of the base class. I have used to BaseClass before and I know it isn't the source of the problem. I have abreviated some code with ( ... ) when I felt it was not necessary to put said code.

    Here is what I have done so far :

    Code :
    DerivedClass.h
    Code:
    template <typename T>
    DerivedClass : public BaseClass<T>
    {
    public :
    	...
    	...
    	...
    T &operator[] (int param);
    };
    DerivedClass.cpp
    ...
    ...
    ...
    Code:
    template <typename T>
    T &DerivedClass<T>::operator[] (int param) 
    {
    ...
    ...
    ...
    }
    Main.cpp
    Code:
    ...
    DerivedClass test();
    ...
    test[i] = 12;
    ...
    But I get a Segmentation Fault everytime I run the program.
    I have removed all code in the overloaded operator [] in the derived class so I know this is not the problem.
    If I call test[i] = 12 I get a segfault even if [] does not have any code in it's implementation.
    If I don't call [] I don't have any problems.
    I have tried different values, etc. but after some extensive testing I can see the problem is not there.

    I have no idea what I could be doing wrong... but I really have close to no experience overloading operators and working with templates so I'm no surprised it's not working.

    I would appreciate any help you could give me, thank you in advance !

  2. #2
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    First of all look at this:
    Code:
    ...
    DerivedClass test();
    ...
    test[i] = 12;
    ...
    You have ommited the template argument
    this shouldn't compile unless you have some standart template argument in the list (which you haven't...)

    anyways heres is a small code of my own:
    see if it works for you

    Code:
    //Test derived_Operator
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    //declaration:
    template<typename T>
    class Base 
    {
    public:
      static const int COUNT = 20;
      Base(){}
      virtual ~Base(){}
    
      T& operator [] (int iat);
    private:
      static T invalid;
      T theMember[COUNT];
    };
    //implementation:
    template<typename T>
    T Base<T>::invalid;
    
    template<typename T>
    T& Base<T>::operator [] (int iat)
    {
      if(iat < COUNT)
        return theMember[iat];
      return invalid;
    }
    
    //final Derived class...
    template <typename T>
    class Derived : public Base <T>
    {
    public:
      Derived()
      {
      }
      ~Derived()
      {
      }
      string& operator [] (int iat);
    private:
      static string error;
      string array[3];
    };
    
    template<typename T>
    string Derived<T>::error = "ERROR";
    
    template<typename T>
    string& Derived<T>::operator[] (int iat)
    {
      if(iat < 3)
        return array[iat];
      return error;
    }
    
    
    int main()
    {
      Base<int> b;
      Derived<int> d;
    
      b[0] = 2;
      d[0] = "string no 01";
    
    
      cout <<b[0]<<endl<<d[0]<<endl;
    }///:~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dilemma with templates and inheritance..!
    By Halloko in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2004, 01:01 PM
  2. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  3. Templates and Inheritance problem
    By WebSnozz in forum C++ Programming
    Replies: 0
    Last Post: 04-11-2004, 02:39 PM
  4. Mixing templates and inheritance, and the damage caused ;)
    By SilentStrike in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 11:47 PM

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