Iterator on vector error for operands "=" and "!=" - c++

using namespace std;
vector<IDrawable*>::const_iterator itDrawable;
for(itDrawable= scene.getDrawables().begin(); itDrawable!=scene.getDrawables().end();itDrawable++){
IDrawable *drawable =(*itDrawable);
drawable->draw();
}
This code is passing me the error:
Description Resource Path Location Type no match for 'operator!='
(operand types are 'std::vector<IDrawable*>::const_iterator {aka
__gnu_cxx::__normal_iterator<IDrawable* const*, std::vector<IDrawable*> >}' and 'std::vector<const
IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<const
IDrawable* const*, std::vector<const IDrawable*> >}')
And
Description Resource Path Location Type no match for 'operator='
(operand types are 'std::vector<IDrawable*>::const_iterator {aka
__gnu_cxx::__normal_iterator<IDrawable* const*, std::vector<IDrawable*> >}' and 'std::vector<const
IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<const
IDrawable* const*, std::vector<const IDrawable*> >}')
I have looked these up and i should have something to do with the const_iterator ?
yet my scene.getDrawables() looks like:
const std::vector<const IDrawable*>& getDrawables() const {
return drawables;
}
So the iterator should be a const_iterator right ?
I have no clue what has to change...

Your
const std::vector<const IDrawable*>& getDrawables() const
returns a const reference to a vector of const IDrawable* pointers. However your
vector<IDrawable*>::const_iterator itDrawable;
declares a const_iterator to a vector of different type (IDrawable*, not const IDrawable*). Either change the definition to vector<const IDrawable*>::const_iterator itDrawable; or simply use auto to declare the iterator in your for loop,
for(auto itDrawable= scene.getDrawables().cbegin(); ...)

The formatting of your question hides the real problem, but you used:
vector<IDrawable*>::const_iterator itDrawable;
where you should have used
vector<const IDrawable*>::const_iterator iDrawable;
Likewise, you then need
const IDrawable *drawable =...
instead of
IDrawable *drawable =...
All this due to the fact that you have a std::vector<const IDrawable*>.

Related

C++ unordered_map no matching member function for call to 'find' [duplicate]

This question already has answers here:
How can I create a string from a single character?
(9 answers)
Closed 1 year ago.
I'm trying to initialize an unordered_map in c++ and then run the find command, it keeps failing with this error and I'm not sure what to do next. This is in leetcode if that helps.
string num = "10003";
unordered_map<string,string> mymap = {
{"0","0"},
{"1","1"},
{"6","9"},
{"8","8"},
{"9","6"},
};
unordered_map<string,string>::const_iterator got;
for (auto s: num){
mymap.find(s);
}
Line 15: Char 19: error: no matching member function for call to 'find'
mymap.find(s);
~~~~~~^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:920:7: note: candidate function not viable: no known conversion from 'char' to 'const std::unordered_map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>>>>::key_type' (aka 'const std::__cxx11::basic_string<char>') for 1st argument
find(const key_type& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:924:7: note: candidate function not viable: no known conversion from 'char' to 'const std::unordered_map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>>>>::key_type' (aka 'const std::__cxx11::basic_string<char>') for 1st argument
find(const key_type& __x) const
^
1 error generated.
The problem is that your key type is std::string, but you are passing a single char to find(). std::string does not have a constructor that accepts only a single char, hence the error.
There are other ways to construct a std::string from a char, eg:
mymap.find(std::string(1,s));
mymap.find(std::string(&s,1));
mymap.find(std::string() + s);
using std::literals;
mymap.find(""s + s);
mymap.find({s});
s is of type char, while your map has key's of type string. Change one or the other.

c++: error: cannot convert ‘ns3::TracedValue<ns3::SequenceNumber<unsigned int, int> >’ to ‘uint32_t {aka unsigned int}

I am trying to cast TracedValue<uint32_t> m_bytesInFlight to uint32_t but I get the following error
error: cannot convert ‘ns3::TracedValue<ns3::SequenceNumber<unsigned int, int> >’ to ‘uint32_t {aka unsigned int}
Function prototype and variable declarations are
uint32_t UnAckDCount (void) const;
TracedValue<uint32_t> m_bytesInFlight {0}; //!< Bytes in flight
Here i am calling the function
uint32_t
TcpSocketBase::UnAckDCount () const
{
return m_tcb->m_highTxMark - (uint32_t) m_tcb->m_bytesInFlight;
}
Please suggest some method so that I can execute the return statement to get the result. Thanks in advance
Changing m_tcb->m_highTxMark to m_tcb->m_highTxMark.Get().GetValue() should work.
Seeing the compiler error, it's easy to figure out that variable m_highTxMark is of type ns3::TracedValue<ns3::SequenceNumber<unsigned int, int> >. I checked the documentation of ns3::TracedValue and ns3::SequenceNumber, they both have getter functions Get() and GetValue() respectively.
See:
https://www.nsnam.org/doxygen/classns3_1_1_traced_value.html
https://www.nsnam.org/doxygen/classns3_1_1_sequence_number.html

Trying to reference an overloaded method with const in C++ using clang

In clang there is this file. I'm trying to reference one of the overloaded methods:
00909 std::string getAsString(const PrintingPolicy &Policy) const;
I tried:
std::__cxx11::basic_string<char> (&p2)(const clang::PrintingPolicy&) = &clang::QualType::getAsString;
and got:
REPL:1:73: error: address of overloaded function 'getAsString' does not match required type 'std::__cxx11::basic_string<char> (const clang::PrintingPolicy &)'
std::__cxx11::basic_string<char> (&p2)(const clang::PrintingPolicy&) = &clang::QualType::getAsString
/home/a/julia/usr/bin/../include/clang/AST/Type.h:905:15: note: candidate function has different qualifiers **(expected none but found const)**
std::string getAsString(const PrintingPolicy &Policy) const;
I also tried:
std::__cxx11::basic_string<char> (&p1)(clang::PrintingPolicy&) = &clang::QualType::getAsString;
and got:
candidate function has type mismatch at 1st parameter **(expected 'clang::PrintingPolicy &' but has 'const clang::PrintingPolicy &')**
Am I doing something wrong or is the compiler broken?

boost bind to a class member function passed to QtConcurrent

I am trying to use boost to pass a function to QtConcurrent.
QFutureWatcher<GDALDataset> watcher;
QFuture<GDALDataset> future;
GDALDriver *poNITFDriver;
future = QtConcurrent::run(boost::bind(
&GDALDriver::CreateCopy,
poNITFDriver,
pszDstFilename,
poDataset,
FALSE,
papszOptions,
pfnProgress,
NULL
));
watcher.setFuture(future);
where poNITFDriver is the instance of the class containing the method.
What am I doing wrong here? I get the following compile errors:
error: no match for operator= in future = QtConcurrent::run(FunctionObject) with FunctionObject = boost::_bi::bind_t<GDALDataset*, boost::_mfi::mf6<GDALDataset*, GDALDriver, const char*, GDALDataset*, int, char**, int (*)(double, const char*, void*), void*>, boost::_bi::list7<boost::_bi::value<GDALDriver*>, boost::_bi::value<const char*>, boost::_bi::value<GDALDataset*>, boost::_bi::value<bool>, boost::_bi::value<char**>, boost::_bi::value<int (*)(double, const char*, void*)>, boost::_bi::value<long int> > >

Error on std::pair when compiling for mac / linux

I have a problem when compiling my code under Mac OS. This function declaration in my header file apparently causes some errors (it does work fine under Windows, though):
#include <string>
#include <vector>
#include <map>
#ifdef WIN32
#include <windows.h>
#endif
[...]
int setProcessEnvironment(
const wchar_t * procName,
const wchar_t * appName = NULL,
const wchar_t * workingDir = NULL,
const wchar_t * cmdArgs = NULL,
const std::vector< std::pair<const wchar_t *, int> > &systemEnvVars = std::vector< std::pair<const wchar_t *, int> >()
);
It looks like the compiler doesn't like the input for my pair - maybe I am missing some includes or what is the problem here?
I also don't fully understand the last line of this error message as my function description actually looks very different to the one in this error...
I am starting to think it may have to do with the default initialization, but what is the difference between the Mac and Windows compiler here?
26: error: expected ‘,’ or ‘...’ before ‘>’ token
26: error: wrong number of template arguments (1, should be 2)
/usr/include/c++/4.2.1/bits/stl_pair.h:68: error: provided for ‘template<class _T1, class _T2> struct std::pair’
26: error: template argument 1 is invalid
26: error: template argument 2 is invalid
26: error: default argument missing for parameter 6 of ‘int SysProcManager::setProcessEnvironment(const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const std::vector<std::pair<const wchar_t*, int>, std::allocator<std::pair<const wchar_t*, int> > >&, int)’
159: error: prototype for ‘int SysProcManager::setProcessEnvironment(const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const std::vector<std::pair<const wchar_t*, int>, std::allocator<std::pair<const wchar_t*, int> > >&)’ does not match any in class ‘SysProcManager’
26: error: candidates are: int SysProcManager::setProcessEnvironment(const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const std::vector<std::pair<const wchar_t*, int>, std::allocator<std::pair<const wchar_t*, int> > >&, int)
138: error: int SysProcManager::setProcessEnvironment(const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const std::vector<const wchar_t*, std::allocator<const wchar_t*> >&)
Try #include <utility>
this was pointed out by André Caron:
Out of curiosity, can you typedef
std::vector< std::pair > EnvironmentBlock;
(change the name to your liking).
Replace the two instances in your
function declaration. See if that
clears up any parsing errors.
I am now declaring typedef std::vector< std::pair<const wchar_t*, int> > EnvironmentBlock; at the beginning and it does solve this problem on the Mac and it seems the compiler just can't deal with these nested types properly and screws things up - I did not see this problem on Linux or Windows, so maybe it's time to update my compiler (GCC 4.2).
Thank you Andre!
Several of the errors relate to the fact you have 2 definitions of setProcessEnvironment. One that takes as an added int on the end and another that takes in vector of wchar_t (not a vector of pairs).
I'd focus on these 2 problems to begin with. Failing that we need to see the rest of the code because some of the errors are being generated by code we can't see.