I've been getting this error, and I can't figure out how to fix it:
btree.tem:98: instantiated from 'std::pair<typename btree<T>::iterator, bool> btree<T>::insert(const T&) [with T = char]'
test.cpp:13: instantiated from here
btree.tem:37: error: no matching function for call to 'btree<char>::addElem(std::_List_iterator<node<char>*>&, node<char>*&)'
btree.h:178: note: candidates are: void btree<T>::addElem(std::_List_iterator<node<T>*>&, node<T>&) [with T = char]
btree.tem:98: instantiated from 'std::pair<typename btree<T>::iterator, bool> btree<T>::insert(const T&) [with T = char]'
test.cpp:13: instantiated from here
btree.tem:48: error: no matching function for call to 'btree<char>::addElem(std::_List_iterator<node<char>*>&, node<char>*&)'
Inside my header file I have this setter function:
void addElem (std::_List_iterator<node<T>*>& itr, node <T>& n) {
neighbours.insert(itr, n);
}
and I don't know what's wrong with it. The error seems to happen whenever I call it like this:
class list < node<T>* >::iterator itr = bt->level().begin();
node <T>*n = new node<T>(elem, bt->max());
bt->addElem(itr, n);
What is the problem?
The compiler is looking for:
btree<char>::addElem(std::_List_iterator<node<char>*>&, node<char>*&)
But it only found something for:
btree<char>::addElem(std::_List_iterator<node<char>*>&, node<char>&)
You're passing a pointer to your function. You've not defined an addElem that takes a pointer as its last argument.
The error is not "instatiated from..." - that's the context describing which template instantiations lead to the error.
The error is
btree.tem:37: error: no matching function for call to
'btree<char>::addElem(std::_List_iterator<node<char>*>&, node<char>*&)'
And a candidate is listed:
note: candidates are:
void btree<T>::addElem(std::_List_iterator<node<T>*>&, node<T>&)
[with T = char]
So it is expecting a node<char>, not a pointer node<char>*.
n is a pointer, so you have to write this:
bt->addElem(itr, *n);
It is clear from the error message:
btree.tem:37: error: no matching function for call to
'btree<char>::addElem(std::_List_iterator<node<char>*>&, node<char>*&)'
btree.h:178: note: candidates are:
void btree<T>::addElem(std::_List_iterator<node<T>*>&, node<T>&) [with T = char]
See the second parameter type in the error, as well as in the suggested candidates.
Related
So I'm getting these errors when trying to insert to a binary search tree, I've literally been stuck on this one problem for hours not knowing what to do and I couldn't find anything on the internet to help so thanks in advance.
In file included from Indexer.h:19,
from Indexer.cpp:20:
BinarySearchTree.h: In member function ‘void BinarySearchTree<Comparable>::insert(const Comparable&, BinarySearchTree<Comparable>::BinaryNode*&) [with Comparable = Word]’:
BinarySearchTree.h:108: instantiated from ‘void BinarySearchTree<Comparable>::insert(const Comparable&) [with Comparable = Word]’
Indexer.cpp:109: instantiated from here
BinarySearchTree.h:165: error: passing ‘const Word’ as ‘this’ argument of ‘bool Word::operator<(Word&)’ discards qualifiers
BinarySearchTree.h:167: error: no match for ‘operator<’ in ‘t->BinarySearchTree<Word>::BinaryNode::element < x’
Word.h:33: note: candidates are: bool Word::operator<(Word&)
make: *** [Indexer.o] Error 1
And the Code I think is creating it is from Indexer.cpp line 109
Word takes in two parameters, current being the string and count is equal to 0 for this function.
Indexer.cpp
BinarySearchTree<Word> filterTree;
Line 109: filterTree.insert(Word(current, count));
BinarySearchTree.h
void insert( const Comparable & x )
{
Line:108 insert( x, root );
}
Word.cpp :operator<
bool Word::operator<(Word &RHS)
{
if(m_wordText < RHS.GetWord())
{
return true;
}
else
return false;
}
Member comparators should (almost) always be const functions that take const parameters,
bool Word::operator<(Word const&) const;
Any function called within a const function must also be const itself
std::string Word::GetWord() const;
(As #NeilKirk correctly points out this should probably return std::string const&).
You should learn about const correctness as it can stop you from making silly mistakes in your code.
Also using a non-member comparator is usually preferred as it allows both sides of the expression to use conversion operators
bool operator<(Word const& lhs, Word const& rhs)
{
return lhs.GetWord() < rhs.GetWord();
}
I am trying to use a boost::function in my ReceiveRequest to run on its own thread but I must be sending the wrong parameters. (At least thats what I think the compiler is trying to tell me)
Here are the lines that is causing the issue:
//some variables for the function call
std::string something("");
asio::system_error e();
asio::thread daThread();
CurrentRequest.payload;
//attempts to call function
CurrentRequest.Callback(something, &e, CurrentRequest.payload); //line 184
CurrentRequest.Callback(something, &e, &CurrentRequest.payload); //line 185
Here is what the compiler is telling me:
g++ -o ss -pthread -lrt StringSocket.cpp main.cpp -I ~/asio/include -I ~/boost/include ~/boost/lib/*.a
StringSocket.cpp: In member function ‘void StringSocket::ProcessReceive()’:
StringSocket.cpp:184: error: no match for call to ‘(ReceiveRequest::receiveCallback) (std::string&, asio::system_error (*)(), void*&)’
/home/jsander/boost/include/boost/function/function_template.hpp:761: note: candidates are: R boost::function3<R, T1, T2, T3>::operator()(T0, T1, T2) const [with R = void, T0 = std::string*, T1 = asio::system_error&, T2 = void*]
StringSocket.cpp:185: error: no match for call to ‘(ReceiveRequest::receiveCallback) (std::string&, asio::system_error (*)(), void**)’
/home/jsander/boost/include/boost/function/function_template.hpp:761: note: candidates are: R boost::function3<R, T1, T2, T3>::operator()(T0, T1, T2) const [with R = void, T0 = std::string*, T1 = asio::system_error&, T2 = void*]
Here is the ReceiveRequest class:
class ReceiveRequest
{
typedef boost::function<void (std::string *message, asio::system_error& e, void *payload) > receiveCallback;
public:
receiveCallback Callback;
void* payload;
ReceiveRequest(receiveCallback _Callback, void* _payload)
{
Callback = _Callback;
payload = _payload;
}
~ReceiveRequest() { }
};
These errors seem to be making a distinction between pointers and references to variables. I thought they could be used interchangeable as parameters. boost::function also appears to turn all of my local variables into references.
I am also confused that one of my parameters passes as "e" turns into "asio::system_error (*)()". Why is there a second pair of parenthesis added to my variable?
There are multiple issues here:
asio::system_error e();
This isn't doing what you want. Because of the way C++ syntax works, this is actually declaring a function e that takes no parameters and returns an asio::system_error. If you add a void in the parenthesis, this becomes easier to see. It should be declared as:
asio::system_error e;
Secondly, your typedef says your function should take a reference to a system_error: asio::system_error& e. However, when you pass the above in (assuming you fix the first problem), you're trying to pass a pointer:
CurrentRequest.Callback(..., &e, ....); // Should just be 'e'
This is probably a stupid error, but I am getting the following error:
error: request for member ‘speak’ in ‘it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Bird* const*, _Container = std::vector<Bird*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Bird* const&]()’, which is of non-class type ‘Bird* const’
The code that is causing the error is:
void Aviary::speakAll(std::ostream &os) const{
for(std::vector<Bird*>::const_iterator it = birds.begin(); it != birds.end(); it++){
it->speak(os);
}
}
The structure that I am using to store the Bird pointers is:
typedef std::vector<Bird*> Birds;
void Aviary::addBird(Bird *bird){
if(!bird) throw std::logic_error("");
birds.push_back(bird);
}
The question is how do I call an objects member function from a pointer?
You are iterating over pointers, not values. To refer to the value an iterator points to, use -> or *, your value is a pointer, so you need a second dereferentation:
(*it)->speak(os);
// or
(**it).speak(os);
Here, *it means give me the pointer, then the second * or the -> means dereference the pointer.
I get the error mentioned here:
C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> >
Here is the error(again):
main.cpp: In function ‘int main()’:
main.cpp:21:21: error: no matching function for call to ‘Test<int>::foo(std::vector<int, std::allocator<int> >)’
main.cpp:21:21: note: candidate is:
main.cpp:14:6: note: void Test<T>::foo(std::vector<T>&) [with T = int]
main.cpp:14:6: note: no known conversion for argument 1 from ‘std::vector<int, std::allocator<int> >’ to ‘std::vector<int, std::allocator<int> >&’
The problem is that I have a more complex situation and I don't know how to solve it (without breaking too much code).
I have a Binary Search Tree class that is generic. I want to populate a vector of elements of type T(generic) with all the values from the binary search tree nodes - so I can't make the vector const. The function that traverses the tree is recursive.
So I have:
/*main*/
BST<int> t;
t.add(21);
t.add(12);
//.... etc.
vector<int> elements;
t.inorder(elements);
/* ------------ */
and:
/*BST.h*/
template<typename T>
class BST{
//....
Node<T>* root;
//....
void inorder_rec(Node<T>* root, vector<T>& result);
void inorder(vector<T>& result);
//....
};
template<typename T>
void BST<T>::inorder_rec(Node<T>* root, vector<T>& result){
// recursive call for the child nodes
}
void BST<T>::inorder(vector<T>& result){
inorder_rec(this->root, result);
}
/* ------------ */
You are trying to call a function that takes a reference with a temporary. A temporary can only bind to a reference to const. Also, it would be wise to show, where the error actually originated.
Is this your actual code? The definitions of inorder and inorder_rec need to have return types. Otherwise, this part of the code looks fine, no temporary in sight:
vector<int> elements;
t.inorder(elements);
Dumb question, but did you save your file? Or is this error coming from a different part of the code?
I hope this is not painfully obvious. I am getting this cryptic error :
fold.cpp:92: error: expected primary-expression before ‘)’ token
The line that it is referring to is:
if (binary_search (corpus.begin(),corpus.end(), left, customArray::operator<(customArray)))
I arrived at this error after using the simpler call:
if (binary_search (corpus.begin(),corpus.end(), left))
and getting this error message (the important part is the note at the end, saying to change it to the above call)
In function ‘bool std::binary_search(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::_List_iterator<customArray>, _Tp = std::string [3]]’:
fold.cpp:92: instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:4240: error: no match for ‘operator<’ in ‘__val < __i. std::_List_iterator<_Tp>::operator* [with _Tp = customArray]()’
/usr/include/c++/4.2.1/bits/stl_algo.h: In function ‘_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::_List_iterator<customArray>, _Tp = std::string [3]]’:
/usr/include/c++/4.2.1/bits/stl_algo.h:4239: instantiated from ‘bool std::binary_search(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::_List_iterator<customArray>, _Tp = std::string [3]]’
fold.cpp:92: instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:2906: error: no match for ‘operator<’ in ‘__middle. std::_List_iterator<_Tp>::operator* [with _Tp = customArray]() < __val’
fold.cpp:16: note: candidates are: bool customArray::operator<(customArray)
Essentially, I am trying to use a binary search on a linked list of custom (array type) objects. The rest of the relavent code is here:
// here is the custom class I am using in the list
class customArray
{
public:
// this is a somewhat lame way to compare, but it seems to work
bool operator< (customArray temp)
{
return array[0] < temp.array[0];
}
bool operator> (customArray temp)
{
return array[0] > temp.array[0];
}
bool operator== (customArray temp)
{
return ((array[0] == temp.array[0]) && (array[1] == temp.array[1]) && (array[2] == temp.array[2]));
}
string array[3];
};
//All of this stuff is in main
customArray one;
//some processing here to fill one
corpus.push_back (one);
// sort the list
corpus.sort();
corpus.unique();
string left [3];
if (binary_search (corpus.begin(),corpus.end(), left, customArray::operator<(customArray)))
{
}
I hope this is easy to understand. Let me know if there is any way that I can clarify.
Your first error message was because binary_search uses < on the iterators, but list's iterators don't support <. This error has nothing to do with whether or not you pass a comparison function as an argument to binary_search.
Your second error message is because you specified the type when passing the function as an argument. That's basically the same as calling a function as f(int x) instead of f(x), which is syntactically incorrect. It should just be customArray::operator<. However, as I said before, that won't help you because you'll just get the first error message again.
Basically you can't perform a binary search on a linked list.
You are putting the whole signature of your functor inside your call to binary_search. You don't need the 'bool' there.