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.
Related
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.
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
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*>.
This code does not compile, using Boost 1.48 and GCC:
// const char* left, const char* right
boost::filesystem::path p = boost::filesystem::absolute(
boost::filesystem::path(right, boost::filesystem::native), // line 314
boost::filesystem::path(left, boost::filesystem::native) ); // line 315
Error messages:
LoggerImplementation.cpp|314|error: invalid conversion from ‘bool (*)(const std::string&)’ to ‘void*’
LoggerImplementation.cpp|314|error: initializing argument 2 of ‘boost::filesystem3::path::path(const Source&, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<Source>::type>, void>::type*) [with Source = const char*]’
LoggerImplementation.cpp|315|error: invalid conversion from ‘bool (*)(const std::string&)’ to ‘void*’
LoggerImplementation.cpp|315|error: initializing argument 2 of ‘boost::filesystem3::path::path(const Source&, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<Source>::type>, void>::type*) [with Source = const char*]’
Under MSVC it compiles. How can I fix this?
Your second argument (boost::filesystem::native) is wrong. boost::filesystem::path simply doesn’t have a constructor which takes this argument – leave it off and the code compiles.
In fact, boost::filesystem::native is a function, and using it in the manner you tried makes no sense. Furthermore, if MSVC compiles this code, that’s a definitive bug (it is using an implicit conversion from a function pointer to void*, which doesn’t exist according to the standard).
When programming in c++ STL, or intensively using 'templatization', and some compiling error happens, often the error report is really long, and often too much not needed information is given.
I'm talking about gcc, i don't know if with other compilers is different, but some times even for just a typo, it takes a while to catch the error purging the
<ns::type<ns::type<ns::type, ns::type<...><ns::type<...> > > > >
I'm looking for some compiler flag, trick, workaround or methodology ( i currently copy past the error and put on two lines what i have and what compiler use to want and removing variables bookmarks... (kinda sad procedure for a not-so-uncommon ctrl+s non well performed)) that could make this task quicker or just helping me ( even only some IDE error syntax highlight... )
STLFilt: An STL Error Message Decryptor for C++ is a popular tool to filter these verbose error messages and turn them into something more legible.
From their website:
STLFilt was initially conceived as a teaching aid, to allow students
taking C++ and/or STL-specific workshops to make sense of typically
overbloated STL error messages. Today, however, even some C++ experts
have adopted STLFilt for use in everyday development. The results may
not always be perfect, but most of the time the information lost
during Decryption is not critical to the application being debugged.
The rest of the time, Decryption is easy enough to bypass.
The distribution for each platform (compiler/library set) is
self-contained and tuned to the idiosyncrasies of that platform. Each
Perl script performs basic regex substitutions for all the standard
(and extended, if present in the library) STL components, while
certain versions of the script go further with respect to message
ordering, line wrapping, library header error treatment, etc., as I
unilaterally deemed appropriate for that platform.
Here's a demo run that shows how it can be useful:
The source program:
#include <map>
#include <algorithm>
#include <cmath>
const int values[] = { 1,2,3,4,5 };
const int NVALS = sizeof values / sizeof (int);
int main()
{
using namespace std;
typedef map<int, double> valmap;
valmap m;
for (int i = 0; i < NVALS; i++)
m.insert(make_pair(values[i], pow(values[i], .5)));
valmap::iterator it = 100; // error
valmap::iterator it2(100); // error
m.insert(1,2); // error
return 0;
}
First, an unfiltered run using the MinGW gcc 3.2 compiler:
d:\src\cl\demo>c++2 rtmap.cpp
rtmap.cpp: In function `int main()':
rtmap.cpp:19: invalid conversion from `int' to `
std::_Rb_tree_node<std::pair<const int, double> >*'
rtmap.cpp:19: initializing argument 1 of `std::_Rb_tree_iterator<_Val, _Ref,
_Ptr>::_Rb_tree_iterator(std::_Rb_tree_node<_Val>*) [with _Val =
std::pair<const int, double>, _Ref = std::pair<const int, double>&, _Ptr =
std::pair<const int, double>*]'
rtmap.cpp:20: invalid conversion from `int' to `
std::_Rb_tree_node<std::pair<const int, double> >*'
rtmap.cpp:20: initializing argument 1 of `std::_Rb_tree_iterator<_Val, _Ref,
_Ptr>::_Rb_tree_iterator(std::_Rb_tree_node<_Val>*) [with _Val =
std::pair<const int, double>, _Ref = std::pair<const int, double>&, _Ptr =
std::pair<const int, double>*]'
E:/GCC3/include/c++/3.2/bits/stl_tree.h: In member function `void
std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(_II,
_II) [with _InputIterator = int, _Key = int, _Val = std::pair<const int,
double>, _KeyOfValue = std::_Select1st<std::pair<const int, double> >,
_Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int,
double> >]':
E:/GCC3/include/c++/3.2/bits/stl_map.h:272: instantiated from `void std::map<_
Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _Input
Iterator = int, _Key = int, _Tp = double, _Compare = std::less<int>, _Alloc = st
d::allocator<std::pair<const int, double> >]'
rtmap.cpp:21: instantiated from here
E:/GCC3/include/c++/3.2/bits/stl_tree.h:1161: invalid type argument of `unary *
'
And a filtered run using the gcc-specific Proxy c++:
d:\src\cl\demo>c++ rtmap.cpp
*** {BD Software Proxy c++ for gcc v3.01} STL Message Decryption is ON! ***
rtmap.cpp: In function `int main()':
rtmap.cpp:19: invalid conversion from `int' to `iter'
rtmap.cpp:19: initializing argument 1 of `iter(iter)'
rtmap.cpp:20: invalid conversion from `int' to `iter'
rtmap.cpp:20: initializing argument 1 of `iter(iter)'
stl_tree.h: In member function `void map<int,double>::insert_unique(_II, _II)':
[STL Decryptor: Suppressed 1 more STL standard header message]
rtmap.cpp:21: instantiated from here
stl_tree.h:1161: invalid type argument of `unary *'
STL Decryptor reminder:
Use the /hdr:L option to see all suppressed standard lib headers
[Note: demo runs were performed in an 80-column console window with
STLFilt's intelligent line wrapping enabled, and with internal
switches set to produce messages as terse as possible. More detail is
available by tailoring the Decryptor's options.]
The only downside I can see is that it mislabels the C++ Standard Library. :(
Here's a relevant journal article by STLFilt's author.
A few people have made tools to perform this, as there's nothing inbuilt. Tonnes on Google, but the top result returns: http://www.bdsoft.com/tools/stlfilt.html
It should be compatible with visual studio and gcc.
edit::
I need to type less to actually get input in time :)