CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    2

    class derived from template with operator overloads

    hello, everyone! i would appreciate if any of c++ guru's will help me to resolve the issue i have faced.

    i have a template class with operator overloads:
    Code:
    template <class Object>
    class ObjectSet
    {  
    public:
        const ObjectSet<Object> operator+(const ObjectSet<Object> &) const;
        const ObjectSet<Object> operator-(const ObjectSet<Object> &) const;
        const ObjectSet<Object> operator*(const ObjectSet<Object> &) const;
    ///
    };
    i have a class derived from it.

    Code:
     class NodeSet: public ObjectSet<Node>
    {
    ///
    };

    in main() i have the following:

    Code:
            
    1      ObjectSet<int> aos;
    2      ObjectSet<int> bos;
    3      ObjectSet<int> cos;
    4      cos = aos+bos;
    5      NodeSet ans;
    6      NodeSet bns;
    7      NodeSet cns;
    8      cns = ans+bns;
    when i try to compile it compiler error with the following error emerges on line 8:

    Code:
    no match for 'operator=' in 'cns = ObjectSet<Object>::operator+(const ObjectSet<Object>&) const [with Object = Node](((const ObjectSet<Node>&)((const ObjectSet<Node>*)((ObjectSet<Node>*)(&bns)))))'
    though there is no error about line 4. could you please help me to understand, what can be the problem that does not allow me to compile the code?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: class derived from template with operator overloads

    Code:
    template <class Object>
    class ObjectSet
    {  
    public:
        const ObjectSet<Object> operator+(const ObjectSet<Object> &) const;
        const ObjectSet<Object> operator-(const ObjectSet<Object> &) const;
        const ObjectSet<Object> operator*(const ObjectSet<Object> &) const;
    ///
    };
    
    class NodeSet: public ObjectSet<Node>
    {
    ///
    };
    
    int main()
    {
          ObjectSet<int> aos;
          ObjectSet<int> bos;
          ObjectSet<int> cos;
          cos = aos+bos;
          NodeSet ans;
          NodeSet bns;
          NodeSet cns;
          cns = ans+bns;
    }
    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout ! 
    
    Your Comeau C/C++ test results are as follows: 
    
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 11: error: identifier "Node" is undefined
      class NodeSet: public ObjectSet<Node>
                                      ^
    
    "ComeauTest.c", line 25: error: no operator "=" matches these operands
                operand types are: NodeSet = const ObjectSet<<error-type>>
            cns = ans+bns;
                ^
    
    "ComeauTest.c", line 20: warning: variable "cos" was set but never used
            ObjectSet<int> cos;
                           ^
    
    2 errors detected in the compilation of "ComeauTest.c".
    
    In strict mode, with -tused, Compile failed
    Please provide a complete example that duplicates the compiler error.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: class derived from template with operator overloads

    The reason as it looks is that you have the default assignment operator for the derived class. And the default one has the signature as:
    Code:
    NodeSet& operator=(const NodeSet& rhs);
    and from the statement:
    Code:
    cns = ans+bns;
    the rhs of operator= evaluates to an object of type of its base class since you don't provide the arithematic operators for the derived class. Hence, the derived class inherits the base operator and evaluates the expression 'ans+bns' as sum of two base objects returning a base type object. Now, assignment derived=base doesn't work. Fix depends on what you want to accomplish. Hope it helps.

  4. #4
    Join Date
    Feb 2009
    Posts
    2

    Re: class derived from template with operator overloads

    Quote Originally Posted by exterminator View Post
    The reason as it looks is that you have the default assignment operator for the derived class. And the default one has the signature as:
    Code:
    NodeSet& operator=(const NodeSet& rhs);
    and from the statement:
    Code:
    cns = ans+bns;
    the rhs of operator= evaluates to an object of type of its base class since you don't provide the arithematic operators for the derived class. Hence, the derived class inherits the base operator and evaluates the expression 'ans+bns' as sum of two base objects returning a base type object. Now, assignment derived=base doesn't work. Fix depends on what you want to accomplish. Hope it helps.
    thank you man!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured