need urgent..

closed account (S1Rf92yv)
hi this sagar.plz send the solution for this problem..


const hvector<T>& operator-= (const hvector<T>& c) const
{
typename vector<T>::iterator lhs= const_cast<T *>(begin());
typename vector<T>::iterator rhs= const_cast<T *>(c.begin());
for(;(lhs!=end) && (rhs!=c.end()); lhs++,rhs++)
{
*lhs-=*rhs;
}
return *this;
}

note: The above set of statements does not show any errors.

const hvector<string>& operator-= (const hvector<string>& c) const
{
typename vector<string>::iterator lhs= const_cast<string *>(begin());
typename vector<string>::iterator rhs= const_cast<string *>(c.begin());
for(;(lhs!=end) && (rhs!=c.end()); lhs++,rhs++)
{
*lhs-=*rhs;
}
return *this;
}

note: the above statements show error as
error: invalid const_cast from type 'std::vector<string, std::allocator<IValue*> > ::const_iterator' to type 'string*'

note:[b] begin( ) is function which returns const_iterator.

note: and also we noticed that for generic type <T> it is accepting. but for specific type <string> it shows error.

so please check it once. I am waiting.

Thank you
Last edited on
We aren't here to write programs for people; we're here to help people write their own programs.

This code makes no sense. What is it supposed to do?

The first function looks like it is supposed to be a template function but I don't see a template. If it is, then yes, synttactically it could be correct. Further semantic checking is done once the template is instantiated. Once the template is instantiated, then yes, it won't compile, because begin() returns an iterator which you are casting to the iterated type. Which makes no sense, but I think you are attempting to cast away the const-ness of the iterator.

Given what the function appears to be doing, why are you passing by const reference and not just reference? This would alleviate the need for const_cast, which IMHO, should rarely be used anyway.
closed account (S1Rf92yv)
Thank U Jsmith for ur info. actually it is class template. We have define class template at the beginning of the class as"template <class T>".
I have remove that particular const_cast operator. But it shows same error. Even it is not posting different error.
Last edited on
hvector<string>::begin() const

is being called which returns a const_iterator.

You need

vector<string>::const_iterator lhs( begin() );
vector<string>::const_iterator rhs( c.begin() );

to make it compile, but then the assignment

*lhs -= *rhs;

won't work because string does not have an operator-= and even if it did, you would be calling it on a const object and certainly operator-= should be a non-const member function.

Your operator-=() method should not be declared const.

closed account (S1Rf92yv)
Thanks Mr. Jsmith. very useful information for me..
Thanks lot
Topic archived. No new replies allowed.