I'm getting this error message
error: no matching function for call to
HCTree::build(std::basic_ifstream < char, std::char_trait< char>> &)
const
note: candidates are: HCNode* HCTree::build(std::ifstream&) < near
match>
in .cpp
int HCTree::decode(ifstream &in) const {
if(root == NULL) {
root = build(in); <-- error here
}
return aux_decode(in, root); <-- similarly error here too
}
in .hpp
HCNode* build(ifstream &in);
I don't quit understand what am I doing wrong
This looks like a constness problem.
Just a wild guess, but does your HCTree class inherit from HCNode?
You're trying to call a non-const member function from a const function, which is illegal. Furthermore, assuming root is a non-mutable member variable of the HCTree or HCNode class, trying to modify it in the const decode function is also illegal. Try removing the const qualifier from HCTree::decode, since it seems not to be const at all.
Alternatively, if root is not a non-mutable member variable, you could declare HCNode::build as const, assuming it actually doesn't mutate any state.
Related
a simple and I guess easy to answer question (if I did not already got it myself). The following overloaded functions:
void BR(const bool set) { backwardReaction_[nReac_] = set; }
bool BR(const int reactionNumber) const { return backwardReaction_[reactionNumber]; }
The first function is a setter and the second a getter function. backwardReaction_ is of type std::vector<bool>. The problem occurs whenever I want to call the second function. Here I get a compiler error overload function BR(xy) ambigious.
int main()
.
.
const int i = 3;
bool a = chem.BR(i);
The compiler error is equal to:
chemistryTestProg.cpp: In function ‘int main()’:
chemistryTestProg.cpp:74:34: error: call of overloaded ‘BR(const int&)’ is ambiguous
const bool a = chem.BR(i);
^
In file included from ../../src/gcc/lnInclude/chemistryCalc.hpp:38:0,
from ../../src/gcc/lnInclude/chemistry.hpp:38,
from chemistryTestProg.cpp:35:
../../src/gcc/lnInclude/chemistryData.hpp:266:18: note: candidate: void AFC::ChemistryData::BR(bool)
void BR(const bool);
^~
../../src/gcc/lnInclude/chemistryData.hpp:322:22: note: candidate: bool AFC::ChemistryData::BR(int) const
bool BR(const int) const;
^~
I guess that I get the problem because of the types bool and int which are identically (true => int(1), false => int(0). As I am changing the getter name to, e.g., bool getBR(const int reactionNumber) {...} everything works fine. So I guess the problem is about the similarities of the bool and int treatment within c++. I also tried a variety of different calls such as:
const bool a = chem.BR(4)
const bool a = chem.BR(int(5))
const bool a = chem.BR(static_cast<const int>(2))
bool a = chem.BR(...)
Thus, I think it is really related to the bool andint overloading arguments. Nevertheless, I made a quick search and did not find too much about these two overload types and resulting problems. Tobi
This is because you declared BR(int), but not BR(bool), to be const. Then when you call BR(int) on a non-const object, the compiler has two conflicting matching rules: parameter matching favours BR(int), but const-ness matching favours BR(bool).
Can anyone tell me why this piece of code generates the "No matching function for call" error? I believe that the syntax is correct, I really have no idea why it does not work. I want to replace an occurence of {{some_key}} in Template_ with the matching value of some_key in unordered_map passed to function.
std::string View::Render(const std::unordered_map<std::string, std::string> &model) const {
for(int i = 0; i < this->Template_.length(); ++i){
if(this->Template_[i] == '{' && this->Template_[i+1] == '{'){
int PositionBegin = i;
std::string Key = FindKey();
if(Key.length() > 0) {
std::unordered_map<std::string, std::string>::const_iterator Found = model.find(Key);
if (Found != model.end())
this->Template_.replace(PositionBegin, Key.length()+4, Found->second);
}
}
}
return this->Template_;
}
View class looks as simple as this:
class View {
public:
View(const std::string &Template);
std::string Render(const std::unordered_map<std::string, std::string> &model) const;
std::string Template_;
};
The full error is:
error: no matching function for call to ‘std::__cxx11::basic_string<char>::replace(int&, std::__cxx11::basic_string<char>::size_type, const std::__cxx11::basic_string<char>&) const’ this->Template_.replace(PositionBegin, Key.length()+4, Found->second);
Your function is defined as
std::string View::Render(const std::unordered_map<std::string, std::string> &model) const
Since it is const that means you cannot modify any of the the class members. replace would modify Template_ so you cannot call it.
You have two ways to fix this. You can get rid of the const on the function if you want to be able to manipulate Template or you can declare Template_ as mutable so it can be modified.
The problem is caused by attempting to modify a member variable of a const object.
The member function is a const member function. Hence, this->Template_ is also a const object. You are trying to modify this->Template_ by using
this->Template_.replace(PositionBegin, Key.length()+4, Found->second);
If your program logic requires you to modify this->Template_ in a const member function, you'll have to qualify it with mutable.
I need a user defined set according to the order I want. But When I wanted to access set members I got error The object has type qualifiers that are nor compatible with member function (I get this error when I place mouse pointer on error line. The error mentioned in title is fromm Error List after build)
typedef struct tagRECT
{
long left;
long top;
long right;
long bottom;
} RECT;
struct LabelRect : public RECT
{
bool isIsolatedFrom(LabelRect* pRect)
{
if (pRect->right < left ||
pRect->left > right ||
pRect->top > bottom ||
pRect->bottom < top)
return true;
return false;
}
};
class CDrawnLabel
{ public:
LabelRect m_LabelRect;
LabelRect* getLabelRect(){ return &m_LabelRect; }
bool operator<(CDrawnLabel & rhs)
{
//This is the set ordering
return getLabelRect()->right < rhs.getLabelRect()->right;
}
}
I have a set like following
typedef std::set<CDrawnLabel> DrawnLabelSet;
DrawnLabelSet m_setDrawnLabel
I got error when I tried to access set members
DrawnLabelSet::iterator itbegin,itend;
LabelRect* pRectSecond;
itbegin=m_setDrawnLabel.begin();
itend=m_setDrawnLabel.end();
pRectSecond=(*itbegin).getLabelRect();// Here I get the error.
The reason you get this error is because keys inside std::set<T> are stored as const T.
So this expression (*itbegin) returns a const CDrawnLabel. Only const member functions can be called from a const object.
You will have to make getLableRect const. Also since const member functions can only return const pointers/references the member should be:
const LabelRect* getLabelRect() const { return &m_LabelRect; }
Not required but it would be a good idea to make your comparator const as well since it's not modifying any data. Another improvement that can be done is instead of taking a reference you should pass a const ref to the comparator.
bool operator<(const CDrawnLabel &rhs) const
{
//This is the set ordering
return getLabelRect()->right < rhs.getLabelRect()->right;
}
The problem here is that std::set<>::iterator is actually a const_iterator so (*itbegin) has the type const CDrawnLabel&. Why is this? Well, if you could change the reference in the set, you could invalidate the ordering. So you need to take the object out of the set, modify it and then put it back in. Or, if you don't want to change it you could define a const function getConstLabelRect()
So in my header file I have these two variables declared as private
private:
char* data;
int len;
and give this to access it
int length() const { return len; }
Then in my cpp file I am trying to override the operators in string implementation like this:
bool MyString::operator>(const MyString& string)
{
//Compare the lengths of each string
if((this.length()) > (string.length())){
return 0;
}
//The given string is shorter
return -1;
}
when I compile this I get this error:
mystring.cpp:263:14: error: request for member ‘length’ in ‘this’, which is of non-class type ‘MyString* const’
From what I can tell by trying to call the .length() on the this is trying to access a variable on the this pointer which is causing a problem, like in this question.
That's fine because I can do this instead:
bool MyString::operator>(const MyString& string)
{
//Compare the lengths of each string
if((this->len) > (string.length())){
return 0;
}
//The given string is shorter
return -1;
}
which compiles fine but now I'm wondering how do you call a function on a this pointer. I thought that because it was a pointer I would have to dereference it first so I tried this:
bool MyString::operator>=(const MyString& string)
{
//Compare the lengths of each string
if((*(this).length()) >= (string.length())){
return 0;
}
//The given string is shorter but not equal
return -1;
}
But again I got this error:
mystring.cpp:273:17: error: request for member ‘length’ in ‘this’, which is of non-class type ‘MyString* const’
It seems like this should have worked fine as I would have dereferenced the pointer into the object it pointed to which does indeed have that method but I seem to be missing something. How would I go about calling a function defined in my class on the this pointer? And is there some functional reason why the way I described above does not work?
if((this.length()) > (string.length())){
This should be
if((this->length()) > (string.length())){
as this is a pointer.Basically this is just a pointer referring to the object on which member function is called. So, you have to use -> for all reference to members of that class.
One more advice stop using variable names which are standard keywords. like string in your case. Had you included std namespace you would have got the reason for not doing so.
I'm having some trouble compiling my code - it has to do with the types I'm passing in. Here is what the compiler says:
R3Mesh.cpp: In copy constructor 'R3Mesh::R3Mesh(const R3Mesh&)':
R3Mesh.cpp:79: error: no matching function for call to 'R3Mesh::CreateHalfEdge(R3MeshVertex*&, R3MeshFace*&, R3MeshHalfEdge*&, R3MeshHalfEdge*&)'
R3Mesh.h:178: note: candidates are: R3MeshHalfEdge* R3Mesh::CreateHalfEdge(const R3MeshVertex*&, const R3MeshFace*&, const R3MeshHalfEdge*&, const R3MeshHalfEdge*&)
R3Mesh.cpp: In constructor 'R3MeshHalfEdge::R3MeshHalfEdge(const R3MeshVertex*&, const R3MeshFace*&, const R3MeshHalfEdge*&, const R3MeshHalfEdge*&)':
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshVertex*' to 'R3MeshVertex*'
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshFace*' to 'R3MeshFace*'
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshHalfEdge*' to 'R3MeshHalfEdge*'
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshHalfEdge*' to 'R3MeshHalfEdge*'
Here is how I define my R3MeshHalfEdge:
struct R3MeshHalfEdge {
// Constructors
R3MeshHalfEdge(void);
R3MeshHalfEdge(const R3MeshHalfEdge& half_edge);
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next);
R3MeshVertex *vertex;
R3MeshFace *face;
R3MeshHalfEdge *opposite;
R3MeshHalfEdge *next;
int id;
};
This is what the first error complains about:
R3MeshHalfEdge *R3Mesh::
CreateHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next)
{
// Create half_edge
R3MeshHalfEdge *half_edge = new R3MeshHalfEdge(vertex, face, opposite, next);
// Set half_edge ID
half_edge->id = half_edges.size();
// Add to list
half_edges.push_back(half_edge);
// Return half_edge
return half_edge;
}
This is what the second error complains about:
R3MeshHalfEdge::
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next)
: vertex(vertex),
face(face),
opposite(opposite),
next(next),
id(0)
{
}
Here is where I call the CreateHalfEdge function:
for(int i=0; i<mesh.NFaces(); i++)
{
R3MeshFace *f = mesh.Face(i);
vector<R3MeshVertex *> face_vertices; // assume vertices are stored in order around the perimeter of the face
for(unsigned int j = 0; j<f->vertices.size(); j++)
{
R3MeshVertex *v1 = f->vertices[j];
R3MeshVertex *v2;
if(j==f->vertices.size()-1)
v2 = f->vertices[0];
else
v2 = f->vertices[j+1];
int v1_id = v1->id;
int v2_id = v2->id;
R3MeshHalfEdge *next = NULL;
R3MeshHalfEdge *opposite = NULL;
R3MeshHalfEdge *half_edge = CreateHalfEdge(v1, f, opposite, next);
}
... }
The constructor is wrong:
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next);
You pass pointers to const and assign them to pointers to non-const, which fails.
Correct it like so:
R3MeshHalfEdge(R3MeshVertex* vertex, R3MeshFace* face,
R3MeshHalfEdge* opposite, R3MeshHalfEdge* next);
As a remark:
there are two level of const with pointers: pointers to const (const X*) and const pointers (X* const) the former can point to something else but cannot change the object pointed to while the latter cannot be rebound to another object but can change the object pointed. You can combine them to have a const pointer to const (const X* const)
don't pass pointers by reference (*&) unless you intend to modify the pointer itself, and not the object pointed.
Saying const type *¶meter means that you can modify parameter:
void foo(const int *¶meter)
{
int bar= 0;
parameter= &bar;
}
I suspect you don't want to do that. Instead of passing pointers by reference, either pass them by value (since you aren't modifying them) or pass them by const reference (const type * const ¶meter).
You're passing non-const pointers to a function that expects const pointer arguments, it seems.
Note:
...But this shouldn't be a problem. I suspect there's something else going on here, but the question is missing some information. The compiler error is about something in the copy constructor R3Mesh::R3Mesh(const R3Mesh&), which isn't shown in the question.
Edit: OK, it is shown now. I would suggest clearing up the const reference to pointer issues first, and seeing what's left.
The second error is pretty clear, actually - either the arguments to the R3MeshHalfEdge constructor shouldn't be references to const pointers (which tell the compiler you don't intend to change the objects they point to) or the data members you assign those arguments to should be const pointers themselves.
You need to look at your argument and reevaluate what should be constant and what shouldn't.
Your compiler error is thrown because you are pointing a nonconstant pointer (vertex, for example) at constant memory (the vertex argument). In C++ constant and nonconstant variables are different types.
Read through this for a lot more detail.
If you just want it to work, remove all your consts and it will compile. If you want your arguments to be constant, you'll have to do copy copying and assignments of data (not pointers!) to remove the error.
Also, rename your arguments so that you don't have name collisions with your member variables.