We are two students and we now have an epic big problem that we can't resolve. We asked our teacher for some help but he can't help us, so our last chance is this forum!
We're doing a project: a command interpreter of NPI files.
map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end())
cerr<<"command not found"<<endl;
else
(trouve->*second)();
We must use the object named "map" but we can't get the second parameter, named.. "Second". Why? Code Blocks told us the error is in the "else", here is the error:
'second' was not declared in this scope.
We have tried too:
map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end())
cerr<<"command not found"<<endl;
else
(trouve.second)();
And code blocks answered:
error: 'std::map, void (Interpreteur::*)()>::iterator' has no member named 'second'
If someone can help us, it will save our project, we must end it for tomorrow.. We will be very grateful.
Thank you very much for help, we can answer questions, if there are any :)
A std::map iterator points to a pair. So to access the pair's second element, do this:
trouve->second
Note that in your case, the type of that second element is "pointer to member function of Interpreteur," so to call it, you need to provide an Interpreteur object. Something like this:
(interpreteur.*(trouve->second))()
Related
I'm currently working on a project in C++ and I'm just not allowed to push_back on my vector (compile error).
The method where everything seems to go wrong looks like this:
DetectionResult DetectionManager::Update(DetectionInput& input) const
{
std::vector<DetectionResultUnfiltered> results;
results.reserve(m_detectionModels.size());
for (auto& detectionModel : m_detectionModels)
(
std::future<void> future = std::async(std::launch::async, UpdateDetectionModelAsynchronously, detectionModel, &results, &input);
m_futures.push_back(future); // <-- Compile error only on this line
)
}
I think it is rather unimportant what exactly the other called method does and how those types are structured exactly. The only important thing should be that the field m_futures is of the type std::vector<std::future<void>>.
Even when hovering the m_futures in Visual Studio within that method, it clearly shows me that it is of the correct type (field) std::vector<std::future<void>> DetectionManager::m_futures.
But still the .push_back() call is underlined in red, and when hovered it shows the following error: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::future<void>, _Alloc=std::allocator<std::future<void>>]" matches the argument list and object (the object has type qualifiers that prevent a match) - argument types are: (std::future<void>) - object type is: const std::vector<std::future<void>, std::allocator<std::future<void>>>
I'm pretty sure my vector is not really handled as a vector in this current case, because when auto completing the method calls on my vector I don't get even a suggestion for .push_back() or .emplace_back() or something like this. I think it is handled as an object of a type I imported from another library (opencv::mat or something like that), because at some point it was even shown like that when hovered.
And by the way,
the method call is not shown as an error when I do this:
(static_cast<std::vector<std::future<void>>>(m_futures)).push_back(future);
so by explicitly casting it to a vector it seems to work again.
I just don't know exactly what happens here. I've traced down and commented some includes to make sure nothing weird was included. And while doing that I figured out that I don't get any errors highlighted within the DetectionManager.h even when commenting out the #include (Even if not including ANYTHING in the .h at all, only the #include is needed when working with that type).
It doesn't even work on other vectors as well, so when implementing an example vector which only holds bools, I don't need to include the and everything looks right according to Visual Studio, which is weird enough since I didn't include or which I'm using as well.
Does anyone have any idea what it might be? Or how to track down this error?
Big thanks in advance!
As Borgleader correctly pointed out, the method was marked as const - but when pushing back, I am modifying the class field which is obviously not allowed.
The const was a leftover from an older design I just reworked and thus I completely forgot about it.
That paired with some other weird errors I had before (like the field being shown as a complete different type from some library I imported even though it was clearly declared as a vector) didn't help.
I still don't quite understand why my project is compiling when not including or within the header file at all even when I declare fields with those types, but at least that's not stopping me from building and running the program.
I'm having trouble with c++, trying to sort a playlist with my own functions but it is not working.. I checked many posts speaking about a possible similar problem but I couldn't find any solution. (c++ - <unresolved overloaded function type> -- I'm having the same error as in this post but not sure to understand what to do then).
May I have a hint please? :)
Here is my function to compare, comp just compare strings in lowercase.
bool Playlist::byTitleAscend (Music *a, Music *b) { return comp(a->getTitle(), b->getTitle()); }
Then I try to sort my list with it and it throws an error :/
_musics.sort(byTitleAscend); // ERROR, TODO: why ?
I hope it's clear enough!
In the documentation they use const & but it's also said that if we don't change the objects it's not necessary.
Error:
MusicReader/playlist.cpp:148: error: no matching function for call to 'std::list(Music*)::sort(unresolved overloaded function type)'
_musics.sort(byTitleAscend);
I replaced < by ( because it was not showing.
_musics.sort(byTitleAscend);
line should be in Playlist class scope. If it is not, the line should be
_musics.sort(Playlist::byTitleAscend);
In fact there was several errors,
#cokceken was right because I forgot to add the scope and the real problem was that I forgot to scope the comp function too ..
And then without putting the function as static it couldn't work neither, so thanks to #molbdnilo too !
I'm still practicing C++ and I have issues with char iterators over filestreams.
I want to copy a part of a file to another (temporary) file. I want to find a particular string in the first file (I used std::find algorithm), in order to know where I can "cut" the part of the file to be copied (hope that makes sense). My problem is that with the following code I have a compilation error that I don't really understand.
The part of my code in question looks like this:
ifstream readStream(fileName.c_str());
istreambuf_iterator<char> fStart(readStream);
istreambuf_iterator<char> fEnd;
auto position = find(fStart, fEnd, refToReplace); // refToReplace is a std::string
if (position != fEnd){ // If the reference appears in the file
ofstream tempStream("temp.fsr"); // create the temp file
copy(fStart, position , ostreambuf_iterator<char>(fluxTemp)); // and copy the part I want (before the matching string)
}
else{
continue;
}
And the compilation error I'm getting in "stl_algo.h":
error: no match for 'operator==' in '__first.std::istreambuf_iterator<_CharT, _Traits>::operator*<char, std::char_traits<char> >() == __val'
Thank you in advance.
The compilation error should come with an instantiation backtrace that tells you which call you made ultimately caused the error.
In your case, this will point at the find call. find looks for a single element, and the element type of your iterators is a single character, but you pass a string. (Based on your description. Your snippet doesn't actually tell us what the type of refToReplace is.)
The algorithm you're looking for is search, but that requires forward iterators, which streambuf iterators are not.
You will need to choose a different approach.
Hmmmm I am stumped on this one, using Visual Studio 2012 I have only one error left on my project and I am dying to test it out. Yes it's for a C++ class and I hope I posted enough code.
I get the error:
C2227 left of ->writeData must point to class/struct/union/generic type
In the previous project this line had the element of the array of pointers ( an Employee* pointer I assume) and it worked like so:
Employee* myEmployees[MAX_EMPS];
...
myEmployees[i]->writeData(outputEmployee);
So I implement a vector of Employee*, and assume it also contains pointers:
MyVector<Employee*> employeePtrList;
I do the next couple steps a bit indirectly but have cleared this with Prof. Debry:
Employee* empPtr1 = new HourlyEmployee(EMP1_ID, EMP1_NAME, EMP1_ADDRESS, EMP1_PHONE, EMP1_HOURS, EMP1_WAGE);
2...
3...
4...
employeePtrList.push_back(empPtr1);
2...
3...
4...
Then later in the program this line is giving me the error:
employeePtrList.at(i)->writeData(outputEmployee);
I have tried various things, if I dereference it with *(employeePtrList) just for fun it changes the intellisense error but I still get the same:
C2227 left of ->writeData must point to class/struct/union/generic type
Any idears? Where I get stumped is understand that employeePtrLIst is an Employee pointer so maybe it's looking for at in the wrong class? I guess maybe I am not "pointing" to the function in the MyVector class properly?
Thanks
Without knowing exactly how MyVector works, a few things at first glance could be going wrong:
.at() might be returning a reference to an object (or even a copy of the object), thus you'd need to use the . operator.
Another possibility is that, if .at() returns an iterator of type Employee*, you might need to dereference the iterator (i.e. (*employeePtrList.at(i))->writeData(outputEmployee);) depending on the implementation.
i'm not a c++ guru at all, and i've tried to replicate this error in variuos little trials. the fact is that when i do a little program with 2 o 3 classes with what i wanto to do, there is no error. but in the main applicaiton i'm tring to write the error persist even if i've tried a lot of (even nonsense) solutions.
the problem is that i have a main class that instantiate some resources (as pointers) and a strategy pattern that istantiate different concrete behaviours that take in constructors thoose resources.
in main app, init():
device = new Kinect();
controls = new Gui();
UserPositionBehaviour = new UserPositionBehaviour(device, controls);
and, behaviour constructor:
UserPositionBehaviour(Kinect * device, Gui * controls);
this is the error:
src/App.cpp:30: error: no matching function for call to ‘UserPositionBehaviour::UserPositionBehaviour(Kinect*&, ofTrueTypeFont*&, ofxGui*&)’
src/UserPositionBehaviour.h:15: note: candidates are: UserPositionBehaviour::UserPositionBehaviour(Kinect*, ofxGui*, ofTrueTypeFont*)
src/UserPositionBehaviour.h:13: note: UserPositionBehaviour::UserPositionBehaviour(const UserPositionBehaviour&)
eh?? hei, what is happening? i'm passing pointers, not *& (that i don't event know how to read)
some advice?
i've tried to replicate the error with a simple case builds only on couts but there are no problems so mayebe there is some error hidden somewhereelse..
According to the error message, you're calling the constructor with three arguments, not two, and you've got the last two the wrong way round.
If that's the real error message, then your code probably looks like:
UserPositionBehaviour = new UserPositionBehaviour(device, font, controls);
// ^^^^ ^^^^^^^^
and should be:
UserPositionBehaviour = new UserPositionBehaviour(device, controls, font);
// ^^^^^^^^ ^^^^
If your code really does look like what you've posted, and gives that error message, then something really weird is happening; in that case, please post a complete compilable example so we can investigate further.
You can ignore the extra &s in gcc's error message: it's a slightly odd way of saying that it's looking for functions that take their arguments by either value or reference.
The code that causes the error passes three arguments (not two as you think), and it has the last two in the wrong order. You must be looking at the wrong version of the source files, or something like that.
Check for silly things like out-of-date copies of src/UserPositionBehaviour.h and src/App.cpp lying around, that you might be looking at instead of looking at the version the compiler is actually compiling. Or maybe you're using pre-compiled headers, and something has gone wrong there.
The error message for a not-found function will usually look like that. The Foo *& is type "reference to pointer to Foo", and it just means that your argument expression is an lvalue pointer-to-Foo. That call could match a function that takes a pointer by value, or a function that takes a pointer by reference. The compiler hasn't found either, but it has to pick something for the error message, and your compiler picks that one. If your call contained the argument expression device+0 instead of device, then it would not be eligible to pass by non-const reference (because the result of device+0 is a temporary), and the error message wouldn't have the &.
The code should compile if you truly have the constructor
UserPositionBehaviour(Kinect * device, Gui * controls);
defined, which, according to the compiler, you don't:
src/UserPositionBehaviour.h:15: note: candidates are:
UserPositionBehaviour::UserPositionBehaviour(Kinect*, ofxGui*,
ofTrueTypeFont*) src/UserPositionBehaviour.h:13: note:
UserPositionBehaviour::UserPositionBehaviour(const
UserPositionBehaviour&)
The *& simply means you pass the pointer by reference - i.e. it can be modified inside the constructor.