is there a way to pass this as const? - c++

I have a class of items and a function that returns it's size.
I have the operator == which gets 2 const parameters of the class type and return the result of item1.size() == item2.size (). size function is non-parametres func and need only hidden this parameter.
The problem is when I try to use size on const reference of classes, it's give me an error:
'function' : cannot convert 'this' pointer from 'type1' to 'type2'
The compiler could not convert the this pointer from type1to type2.
This error can be caused by invoking a non-const member function on a const object. Possible resolutions:
Remove the const from the object declaration.
Add const to the member function.
The piece of code as it is on my problem:
bool operator==(const CardDeck& deck1, const CardDeck& deck2){
if (deck1.size() != deck2.size()) {
return false;
}
//...
}
The error:
'unsigned int CardDeck::size(void)' : cannot convert 'this' pointer from 'const CardDeck' to 'Cardeck&'
If I want that size will get the object as const, I must make it friend and pass the object as const refference or is there a way to tell size get the class type this as constant ???
Thanks for helping.

Most likely you forgot to qualify the size member function as const: size_t size() const { return /* compute/return size */; }
The alternative is that you really did typo CardDeck as Cardeck somewhere (the spelling from your error message).

Related

Passing private static array as an argument to public member function in c++?

Online documents seem to suggest that a private member variable of a class that is passed as an argument to a public function in the same class needs to be declared as static. Still, I get a compilation error:
class C{
private:
static std::string table1[50];
public:
bool try (){
helper(&table1);
return true;
}
bool helper (std::string * table){
return true;
}
But I am getting this compilation error:
./c:72:31: error: cannot initialize a parameter of type 'std::string *' (aka
'basic_string<char, char_traits<char>, allocator<char> > *') with an rvalue of type
'std::string (*)[50]'
Is there something else that I missed?
Your helper function takes as a parameter a pointer to std::string. You are passing to it a pointer to an array of 50 std::string. Instead, pass the first element of the array (in this case the array decays to a pointer), like
helper(table1);
or
helper(&table1[0]);
I have serious doubts though that that's what you need. Pointers to std::string look a bit fishy here. Better use a std::vector<std::string>, or std::array<std::string, 50>.
Side note: don't call your member function try(), as try is a reserved C++ keyword.

function returning reference to a vector element

I cannot figure out how to return a reference to a vector element. The [] and at() are returning reference, no?
But when I try the following, it won't compile.
I'm using Visual C++ and it gives cannot convert from 'const float' to 'float & error.
T& GetElement(size_t x) const {
return _vector.at(x);
}
GetElement is a method, and _vector is a member variable.
This does not compile because you are trying to return a non-constant reference to an element of the vector, which is itself const.
The reason the vector is const is that your member function is declared const:
T& GetElement(size_t x) const // <<== Here
This const-ness marker gets propagated to all members, including the _vector.
To fix this problem, add const to the type of the reference being returned (demo).
You haven't shown your error, but as a guess, it looks like _vector is a member (from the _ prefix you've given it) and you're in a const member function, so at will return a const reference. So you probably need:
const T& GetElement(size_t x) const {
return _vector.at(x);
}

Why is an array argument treated as a const array?

I am working with a member function that just sets the object's internal orientation to the values given in the argument:
void A::SetOrientation(float a[3]);
In another class, I have the following:
class B
{
public:
RestoreStateTo(A* const o_pA) const
private:
float d_orientation[3];
};
void
B::RestoreStateTo(A* const o_pA) const
{
o_pA->SetOrientation(d_orientation);
}
I get the following compiler error (with Visual Studio 2010):
error C2664: 'void A::SetOrientation(float [])' : cannot convert parameter 1 from 'const float [3]' to 'float []'
I found that I can avoid the issue with o_pA->SetOrientation(const_cast<float *>(d_orientation));, but I'd like to get a better grasp of what is going on.
I would appreciate an explanation as to why the array argument is converted to a const array as well as suggest the right approach to dealing with the error?
Because the prototype of your function void B::RestoreStateTo(A* const o_pA) const says you will not modify any member of B.
Since d_orientation is an attribute of B, it is const in this function.
Your SetOrientation function should take in a const,
void A::SetOrientation(const float a[3]);
Otherwise, it's possible that A::SetOrientation will modify the array you pass in. Because you have "B::RestoreStateTo(A* const o_pA) const", it means that the compiler won't let you pass a pointer to B::d_orientation as a non-const input, because A::SetOrientation has no guarantee not to modify it.
You have declared a const member function, and therefore all the members of this will be treated as const inside that function. So it shouldn't be surprising that d_orientation is treated as const.
It's the same principle that makes this code illegal:
const B* p = ...;
p->d_orientation[0] = 0.0f; // error, assigning member of `const` object

Argument conversion for function taking an arg as 'const arg *&'

Here is a problem i am facing while reviving a old code
here is another one--
so here is definition of a function:-
int shen_get_entity_from_list(const ENTITY_LIST& list, const ENTITY*& e);
and here is the way that variables are defined
ENTITY *t_ent = NULL, *p_e = NULL, *s_e = NULL, *t_e = NULL;
now when i call this function
s_pos = shen_get_entity_from_list(sortedACISfeatureList, s_e);
it gives me the following error
error C2664: 'shen_get_entity_from_list' : cannot convert parameter 2 from 'ENTITY *' to 'const ENTITY *&'
The s_e variable needs to be declared as:
const ENTITY* s_e = NULL;
to match the declaration of the function. This says that you won't use the s_e pointer to modify the ENTITY that it points so after the function is called.
isn't the reference already const type? and requires value at creation time itself?
null pointer references aren't allowed.
because of all these i don't feel this problem is solvable
Try moving the const:
int shen_get_entity_from_list(const ENTITY_LIST& list, ENTITY* const& e);
This gives you a const-reference to the type you are passing, rather than a non-const reference to a pointer to a const ENTITY.

pointer assignment

I have the following template structure:
template <typename scalar_type>
struct postc_params{
scalar_type delta;
unsigned int time_horizon;
matrix_math::matrix<scalar_type> dynamics;
boost::shared_ptr<continuous_set> invariant_set_ptr;
boost::shared_ptr<continuous_set> input_set_ptr;
boost::shared_ptr<continuous_set> initial_set_ptr;
};
Now, I have a templated class with a private member of the above structure type
template <typename scalar_type>
class A{
....
private:
....
postc_params<scalar_type> my_postc;
};
Inside a member function definition of class A, I have the following line of code:
my_postc.initial_set_ptr = my_postc.initial_set_ptr->transform(some_obj);
transform function returns a pointer of type
boost::shared_ptr<continuous_set>
With this code, I have the following error:
passing 'const boost::shared_ptr' as 'this' argument of 'boost::shared_ptr< >& boost::shared_ptr< >::operator=
(const boost::shared_ptr&) [with Y = const continuous::continuous_set, T = continuous::continuous_set]' discards qualifiers
Can anyone help me out with the cause?
Is the member function in A const?
If I am reading your code right, you are trying to change a member of a class from a const member function which is not allowed. Either remove the const from the member function or make the member mutable.
So,
mutable postc_params<scalar_type> my_postc;
However, I would take care with this method. Maybe reevaluate why the method that is changing my_postc is const. Either it should not be const or it should not be changing my_postc.
you are trying to assign to a const pointer as per the error message: "passing 'const boost::shared_ptr' as 'this' argument"
the member function you mention is surely const hence the error
you should rather reconsider your design than throw mutable here and there in your code.