Please explain me where I am wrong. I want to switch between several encoding utilities using pointer to function. I declare it like
int (*enc_routine)();
Later I switch coding utilities like
enc_routine = g723_24_encoder;
where utility by itself is something like
extern int g723_24_encoder(
int sample,
int in_coding,
struct g72x_state *state_ptr);
Everything was cute and fine on Linux, but now I am on Visual Studio 2012 and it says:
a value of type "int (*)(int sample, int in_coding, g72x_state *state_ptr)" cannot be assigned to an entity of type "int (*)()"
Thank you for help (if any)!
You need to declare the parameters for your function pointer. You can't declare it to take no parameters and set it equal to a function that requires 3 parameters. I'm shocked it worked on linux.
Related
I need your help. I tried a lot to find a solution for the problem but failed so far.
Idea:
Create a vector of "command tuples" like the following:
typedef boost::tuple<std::string, boost::function<void()>> command_tuple;
std::vector<command_tuple> commands {
boost::make_tuple("command1", boost::bind(&myclass::command1, this))
};
If the string "command1" is used the void command1() function is called. The ID index value is based on find_if search for the string ("command1" is ID=0) on the vector.
This is working fine!
boost::get<1>(commands[ID])();
Problem:
I found no way so far to use a vector of boost::function (pointers) defined with any kind type of function parameters:
typedef boost::tuple<std::string, boost::function<void(const char *)>> command_tuple;
std::vector<command_tuple> commands {
boost::make_tuple("command1", boost::bind(&myclass::command1, this, std::placeholders::_1))
};
This will fail with a cannot convert std::_Placeholder<1> to type const char * compilation error.
This is therefore not possible:
boost::get<1>(commands[ID])("dynamic string argument");
Pinning down of the problem:
hmmm ... so I found out so far that even a simple vector of boost::function does not seem to work (in the way I am trying to use it):
std::vector<boost::function<void(const char *)>> commands {
boost::bind(&myclass:command1, this, std::placeholders::_1)
};
Fails with the same compilation error.
It is possible to replace the std::placeholders::_1 with "static string constants" to avoid any compilation errors. But my idea was to use a "dynamic string argument".
Can someone please give me a quick hint of what I am doing wrong or maybe show some simple alternatives. I want to have quite a big list of function pointers that I can call based on their "string name".
I simply want to avoid a big "switch select case" style code block to
select the function to be executed based on a dynamic "input string".
Thanks for your time!
like T.C. replied in the comments. You should never mix std::bind, std::placeholders and boost::bind.
I have this sample code and it throws an error:
std::random_device rd; // only used once to initialise engine
std::mt19937 rng(rd); // random-number engine used
std::uniform_int_distribution<int> uni(0, 7); // guaranteed unbiased
int random_integer = uni(rng);
The error is:
Error 1 error C2039: 'generate' : is not a member of
'std::random_device' c:\program files (x86)\microsoft visual studio
12.0\vc\include\random 1618 1 Life
Can somone explain me please, why is this happening? It seems to be an error in the header file and not in my code.
How can I fix it?
Thank you.
std::mt19937 has two constructors, one taking a single 32-bit unsigned value as parameter (default value 5489u), the other taking a seed-sequence (a template type) as a parameter. The latter is required to have a method called generate.
As a random_device does not have such a method, your code is not valid.
What you probably wanted to do is
std::mt19937 rng(rd());
That is extracting a value from the device and use that as a parameter.
I haven't worked with C++ for several years and now I'm required to maintain a C++ project. I have the following piece of code which seems to compile in one project, but not in the other.
list.h
#include "mytype.h"
#include <set>
typedef std::set<MYTYPE> MYTYPE_LIST;
typedef MYTYPE_LIST::iterator MYTYPE_LIST_ITERATOR;
class LIST {
[...]
MYTYPE_LIST list;
};
list.cpp
void
LIST::somemethod(MYTYPE* requester)
{
MYTYPE_LIST_ITERATOR it;
for (it = list.begin(); it != list.end() ; it++ )
{
MYTYPE& info = (*it); // Error on this line
[...]
}
}
I am compiling my project with VS 2010, and I get the following errors on the marked line:
error C2440: 'initializing' : cannot convert from 'const MYTYPE' to 'MYTYPE &'
IntelliSense: qualifiers dropped in binding reference of type "MYTYPE &" to initializer of type "const MYTYPE"
I googled these errors, and as far as I understand, (*it) should not be const (because 'it' is not a ::const_iterator, see here), and therefor, I should be able to assign it to the reference variable.
In a std::set the data is the key, and since keys are constant you can't get a non-const reference.
Eventually I found that in VS 2010, Microsoft introduced a change which breaks backward compatibility with this code.
Apparently, this change was made for good reasons - modifying a set element might break the set's invariants. See here, "Problem 3: error C2662: 'NamedNumber::change_name' : cannot convert 'this' pointer from 'const NamedNumber' to 'NamedNumber &'".
The other project was compiled with and old version of Visual Studio, so we had no problem there.
I'm just starting learning C++/XAML windows store app development but for the life of me I can't find a nice way to print variable values to the "Output" window in VS2012.
Debug.WriteLine() doesn't seem to exist for Windows Store apps and I can't find a way to print other than OutputDebugString() which I can't use to print variable values (without some heavy formatting).
Is there just an easy way to print the example line:
mouse position X: 12
for example, where 12 is an integer that comes from MouseDelta.
Thanks for your time,
Poncho
Not really, no. You could write a little function that formatted like printf and passed along the resulting string to OutputDebugString() but there's nothing more straightforward available.
I guess you could use ToString(), Platform::String::operator+, and Platform::String::Data() to accomplish this; though it's a little ugly:
OutputDebugString( ("mouse position X:" + MouseDelta.X.ToString())->Data() );
Here is a nice alternative: http://seaplusplus.com/2012/06/25/printf-debugging-in-metro-style-apps/, basically it allocates a console for your Windows Store App, obviously this will fail certification but given that this may be just for debug purposes only, it will fine. I'm copying the relevant code here:
// Include Windows.h for WINBASEAPI and WINAPI:
#include <Windows.h>
// Declare AllocConsole ourselves, since Windows.h omits it:
extern "C" WINBASEAPI int WINAPI AllocConsole();
auto main(Platform::Array<Platform::String^>^) -> int
{
AllocConsole();
std::wcout << L"Hello there!" << std::endl;
std::getchar();
return EXIT_SUCCESS;
}
However if you want to see such output inside your app, then you may want to use Console Class for Modern UI Apps which implements part of the .NET System.Console and can be safely used inside Windows Store apps.
This solution uses a wrapper around OutputDebugString:
void WinLog(const wchar_t *text, int n)
{
wchar_t buf[1024];
_snwprintf_s(buf, 1024, _TRUNCATE, L"%s %d\n", text, n);
OutputDebugString(buf);
}
which can be called as follows:
WinLog(L"The Answer is", 42);
CString szMsg;
//Other non related code
//stOrderInfo.bstrOrderNum is defined as a _bstr_t
szMsg += ", Order: " + stOrderInfo.bstrOrderNum;
I'm converting the above from VS 6.0 to VS2k10 and I'm getting the following error (compiles in VS 6.0):
error C2593: 'operator +=' is ambiguous
What exactly does this mean and how can I fix it?
Because you've hard-coded ", Order: " the compiler is having a hard time to decide which type it should be.
The obvious type should be CString, but it might try to make it to some other string type, and add the number to it.
So it probably can't decide if it's a CString or another string type. So it can't decide what type you're adding to szMsg.
You could just use a type cast:
szMsg += (CString)(", Order: ") + (CString)((char *)(stOrderInfo.bstrOrderNum));
Cast between string types:
How to: Convert Between Various String Types
This means that compiler cannot choose which + operation to use for BSTR + char concatenations. You have a mismatch of three types: CString, _bstr_t, and char.
Try to unify all three operands to a single type, e.g. to CString
The implementation of CString::operator+= is known to have changed in Visual Studio 2010. For example in previous versions it handled embedded null characters OK, just like operator+ keeps doing, but the new version doesn't. So it might be related to this.
EDIT
Link to discussion on this topic:
http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/c5d7f383-da80-4776-b9b8-a6065839bd87
Better use CString::AppendFormat. But ensure you pass correct format-specifier.