Using enum parameter in functions - c++

I'm working on Windows, in C++ with Visual Studio.
I have a class that has a:
enum algorithmStatus { LOADING, DETECTION, TRACKING, LOST };
In the declaration I want to use a setter and getter to change the status, something like:
void MyStatusClass::setAlgorithmStatus(algorithmStatus newStatus)
{
//_Status = newStatus;
//_Status = MyStatusClass::algorithmStatus::LOADING;
}
But I can't compile because I get:
Error 5 error C2511: 'void MyStatusClass::setAlgorithmStatus(MyStatusClass::algorithmStatus)' : overloaded member function not found in 'Nft_Status' c:\MyStatusClass.cpp 197
How can I do that setter correctly?
EDIT:
In header is already declarated:
void setAlgorithmStatus(MyStatusClass::algorithmStatus newStatus);
and:
void setAlgorithmStatus(algorithmStatus newStatus);
In cpp the function is declared just i write on top.
SOLVED
The problem was i used a MyStatusClass::algorithmStatus in the constructor, you don´t need to use the MyStatusClass::, and its advisable don´t use it if you don´t need it.

The MSDN documentation for error code C2511 gives you a good list to lookout for:
identifier : overloaded member function not found in class
No version of the function is declared with the specified parameters. Possible causes:
Wrong parameters passed to function.
Parameters passed in wrong order.
Incorrect spelling of parameter names.
Always, lookup the error codes to get help in resolving compilation errors.

Related

C++ list::sort <unresolved overloaded function type>

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 !

Error C2668 in bind while porting from VS2008 to VS2013

I'm trying to port my code from VS2008 to VS2013 and I'm getting some errors with std::bind.
The errors say error C2668: 'bind' : ambiguous call to overloaded function.
Here's some code:
// Relevant prototypes:
class CLineaPlanta:public SomeBase { /*...*/ };
int SomeBase::TipoLinea()const;
void SomeBase::TipoLinea(int val);
// function paramater: const std::shared_ptr<CLineaPlanta>& lineasBuscar
// function parameter: int tipoLinea;
std::shared_ptr<CLineaPlanta> lineas;
std::remove_copy_if(lineasBuscar.begin(), lineasBuscar.end(),
std::back_inserter(lineas),
bind(std::not_equal_to<int>(), bind(&CLineaPlanta::TipoLinea, _1), tipoLinea));
This code worked in Visual Studio 2008, but gives the mentioned error in Visual Studio 2013.
Obviously, the compiler's having a hard time figuring out which version of TipoLinea() I'm trying to call. If I rename the getter version to getTipoLinea, the error goes away.
Just in case it is relevant, SomeBase is non-abstract and derives from CObject (not really sure why) and from an interface not related to this part of the code.
Can anyone explain why VS2008 doesn't have any problem with this and how to prevent it (other than by renaming the function, of course)?
I have no idea why this code ever worked in VS2008. It was probably a consequence of 2008's bind being implemented as a macro-based variadics emulation where bind had several overloads for each number of arguments being passed, one of them expecting the first argument to be a pointer to member function with the same number of arguments. This would allow the compiler to disambiguate because you pass one bound argument to bind, so it knows the function argument must have one parameter.
In VS2013, true variadics are used, but this probably means that the type of the first argument is more generic and so the compiler can no longer disambiguate. To make this compile, you need to explicitly cast the member pointer:
std::remove_copy_if(lineasBuscar.begin(), lineasBuscar.end(),
std::back_inserter(lineas),
bind(std::not_equal_to<int>(),
bind(static_cast<int (SomeBase::*)() const>(&CLineaPlanta::TipoLinea),
_1),
tipoLinea));
But as Neil Kirk said, rewriting to a lambda is easier:
std::remove_copy_if(lineasBuscar.begin(), lineasBuscar.end(),
std::back_inserter(lineas),
[tipoLinea](const std::shared_ptr<CLineaPlanta>& linea) {
return linea->TipoLinea() != tipoLinea;
});

Syntax Type Name(Type) in function

I found in sample code strange syntax. I have no idea what this code does, but looks nothing.
What does syntax type Name(Type) in function means?
void doJob()
{
...
bool SetForward(bool); //strange line
...
}
It's a function declaration.
They don't have to be at file scope, but it's fairly rare to see them inside functions if you're not reading relatively ancient C code.
Function Declarations are usually done at the top of your program so that the main can access all of them !! However , what if you want your main not able to access that function ? You simply declare its prototype in the function which is going to use it !! Another Example can be seen a private function in a class which can only be accessed by class methods & not by the main program !!

Ambiguous symbol - cannot convert from ado::DataTypeEnum to DataTypeEnum

Trying to compile a C++ project containing an SQL library and getting these errors:
error C2664: 'CNCQuickADO::ConvertADOType' : cannot convert parameter 1 from 'ado20::DataTypeEnum' to 'DataTypeEnum'
Conversion to enumeration type requires an explicit cast
error C2664: 'CNCQuickADO::ConvertADOType' : cannot convert parameter 1 from 'ado20::DataTypeEnum' to 'DataTypeEnum'
Conversion to enumeration type requires an explicit cast
error C2872: 'DataTypeEnum' : ambiguous symbol
could be 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\dbdaoint.h : DataTypeEnum
or 'c:\amc\source\amthrottling\release\msado15.tlh : ado20::DataTypeEnum
I use the SQL library in several other projects with no issue like this. So I am guessing it must be something to do with where I'm including the library?
Can anyone help? Thanks!
You may be using namespace ado20 somewhere in the code. To tell the compiler that you want to use global DataTypeEnum type (which you probably do since that is what CNCQuickADO::ConvertADOType() expects judging from error messages), declare your variable(s) with fully-qualified type ::DataTypeEnum.
The namespace is very important design in c++.
In ado20::DataTypeEnum, ado20 is the namespace, DataTypeEnum is the name in this namespace.
ado20::DataTypeEnum and othernamespace::DataTypeEnum are different variables which are distinguished from the namespace.
C++ realizes this by modify the variable name. For example, ado20::DataTypeEnum can be processed as _ado20_DataTypeEnum, while othernamespace::DataTypeEnum be _othernamespace_DataTypeEnum.
Another example of using the namespace:
using namespace ado20; // declare the namespace
// other code ...
DateTypeEnum yourvariable; // now you can directly use variable in the namespace

How to define custom error codes in C(++)

I'm desperately trying to define custom error codes in C(++).
As for the project I'm working on it's forbidden to use Exceptions and Signals and any other approach which could allocate dynamic memory.
I used the standard error codes for some methods which produce errors resembling those but for some methods the errors are to specific to be covered by the standard error codes the OS is providing.
I also searched for the error strings in my system but couldn't manage to find the error messages O_O Where are those defined anyway?
So I'm searching for any method which allows me to define a custom error code (e.g. 666) and the correspondig error message ("Satan declared an error!") which will be outputted using strerror.
Is this possible or do I have to meddle with some system related files?
In best regards,
ahnimuhs
If you declare an enum as a type, that can satisfy the domain where typesafety propagates.
Then you can offer a function to access the description as a char buffer (given a locale?, the current locale?).
class t_mon_io_error {
public:
enum t_type {
...
SatanDeclaredAnError = 666
...
};
static const char* Description(const t_type& pError) {
switch(pError) {
...
}
Some implementations of strerror(3) allow for user defined error codes and labels.
You have to provide a _user_strerror() function and define error codes after __ELASTERROR.