-
February 27th, 2009, 10:46 AM
#1
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?
-
February 27th, 2009, 01:19 PM
#2
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
-
February 28th, 2009, 11:30 AM
#3
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: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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 2nd, 2009, 01:07 PM
#4
Re: class derived from template with operator overloads
Originally Posted by exterminator
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: 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|