no suitable user-defined conversion from System::String ^ to System::String exists - c++-cli

I'm relatively inexperienced with C++ and I'm currently creating a simple Windows Forms project. The following code is providing a strange error that I just can't wrap my head around.
if (folderBrowserDialog1->ShowDialog() == ::DialogResult::OK) {
String filepath = folderBrowserDialog1->SelectedPath;
for (auto& p : std::filesystem::directory_iterator(filepath)) {
// read files from filepath
}
}
The error, "no suitable user-defined conversion from System::String ^ to System::String exists" is referring to the folderBrowserDialog1->SelectedPath line. Despite the docs I've checked stating that SelectedPath is a string, apparently it's not the same type of string as C++ is expecting? I assume I need to do some kind of conversion but I'm at a total loss as to how I should do that as I really assumed that SelectedPath would be a regular String.

Related

SystemC ERROR: type name requires a specifier or qualifier

I am trying to write synthesizable SystemC code.
My code:
struct test:sc_module{
sc_in<sc_lv<4>> inp;
sc_out<sc_lv<4>> outp;
void run(){
sc_lv<4> temp = inp.read();
outp.write(temp);
}
SC_CTOR(test){
SC_METHOD(run);
sensitive << inp;
}
};
I am able to simulate the code, but when I run synthesis, Vivado HLS v.2019 throws the following errors. Can someone please help me understand how to fix this error?
ERROR: [HLS 200-70] Compilation errors found: In file included from test2/test.cpp:1:
test2/test.cpp:4:18: error: use of undeclared identifier 'inp'
sc_in<sc_lv<4>> inp;
^
test2/test.cpp:4:21: error: type name requires a specifier or qualifier
sc_in<sc_lv<4>> inp;
^
test2/test.cpp:4:21: warning: declaration does not declare anything [-Wmissing-declarations]
sc_in<sc_lv<4>> inp;
When I add spaces between the angular brackets (as below), it does not throw an error, and synthesis runs successfully.
sc_in< sc_lv<4> > inp;
sc_out< sc_lv<4> > outp;

Does C++/WinRT provide mapping from enum symbol to string name?

I'm using C++/WinRT. The projection includes many enums. I find myself building my own table of enum values to string literals. This is not a big deal for enums with only a few defined values, but it's a pain when there are a lot of them.
What I really want is some form of compile-time or run-time reflection that converts an enum value into the string representation of the compile-time name that represents a given enum value. The code snippet below demonstrates. How can this be automated?
std::wostream& operator<< (
std::wostream& wout,
winrt::Windows::Graphics::DirectX::DirectXPixelFormat e)
{
// https://learn.microsoft.com/en-us/uwp/api/windows.graphics.directx.directxpixelformat
using winrt::Windows::Graphics::DirectX::DirectXPixelFormat;
switch (e) {
case DirectXPixelFormat::R8G8B8A8Int:
wout << L"R8G8B8A8Int";
break;
case DirectXPixelFormat::B8G8R8A8UIntNormalized:
wout << L"B8G8R8A8UIntNormalized";
break;
default:
// TODO: Many enums cases are missing.
// Find a way to compile-time-generate the string values from enum value.
wout << L"Unknown (" << std::to_wstring(static_cast<int32_t>(e)) << L")";
}
return wout;
}
I could build something that parses the winrt/*.h files to generate a header containing arrays of string literals, then #include the generated header. There probably exists sample code for doing this type of thing unrelated to C++/WinRT. But maybe C++/WinRT includes metadata in the SDK, which combined with one of the C++/WinRT command line tools, can easily do this for me? If it's there I have not found it.
I did find ApiInformation interface from winrt/Windows.Foundation.Metadata.h, as well as explanation of "Version Adaptive Code". I had hoped that OS COM interface behind ApiInformation has way to query a name for an enum value, but I was unable to find an answer there.
https://learn.microsoft.com/en-us/uwp/api/Windows.Foundation.Metadata.ApiInformation
how about this
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/move-to-winrt-from-cx#tostring
namespace winrt
{
hstring to_hstring(StatusEnum status)
{
switch (status)
{
case StatusEnum::Success: return L"Success";
case StatusEnum::AccessDenied: return L"AccessDenied";
case StatusEnum::DisabledByPolicy: return L"DisabledByPolicy";
default: return to_hstring(static_cast<int>(status));
}
}
}

Rust - How to migrate '\uXXXX' to new bytes string

I'm wondering if there is a possibility to translate following old Rust code:
bytes!("a\u2028t")
Into current language. It seems bytes! was deprecated by b"" but I don't see a way to translate \u2028 into a byte string literal.
If you want a true byte string equivalent, you'll need to find the UTF8 encoding of U+2028, e.g. via
fn main() {
for b in "\u2028".as_bytes().iter() { print!("\\x{:x}", *b) }
}
which prints \xe2\x80\xa8 (i.e. in pre-encoded form), so b"a\xe2\x80\xa8t" should work. Also, the above hints at another method: you can often use "a\u2028t".as_bytes(), although this will not work in static contexts.

aggregate ‘QSqlQuery testQuery’ has incomplete type and cannot be defined

I'm sure this must be something simple, but I can't quite work out what's up here...
I'm trying to create a QSqlQuery, and the compiler is giving me this:
error: aggregate ‘QSqlQuery testQuery’ has incomplete type and cannot be defined
This code is in my mainWindow class:
void MainWindow::on_toolButton_clicked()
{
QString filename;
filename = QFileDialog::getSaveFileName(this, tr("Save to SQL Database"),
"~/temp",
tr("Files (*.fdb)"));
QSqlDatabase testDatabase = QSqlDatabase::addDatabase("QSQLITE");
testDatabase.setDatabaseName(filename);
//this line won't compile:
QSqlQuery testQuery;
testDatabase.close();
QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName());
}
Can anyone see what I'm missing here?
The error message indicates that the type SqlQuery is not completely defined. QSqlQuery is defined in
#include <QSqlQuery>
Include that and things should compile ok.

Trying to write a textfield value to file in c++

I am tearing my hair out over this error.
------ Build started: Project: shotfactorybatchgen, Configuration: Debug Win32 ------
shotfactorybatchgen.cpp
c:\documents and settings\administrator\my documents\visual studio 2010\projects\shotfactorybatchgen\shotfactorybatchgen\Form1.h(307): error C2664: 'fprintf' : cannot convert parameter 2 from 'System::String ^' to 'const char *'
No user-defined-conversion operator available, or
Cannot convert a managed type to an unmanaged type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have looked all around the interwebs but I can not find an answer. Here is the code that the error is happening in.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Decimal value;
if(!Decimal::TryParse(textBox4->Text, value)) {
MessageBox::Show("Non-numeric characters detected in 'Wait Time' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
} else {
if(!Decimal::TryParse(textBox3->Text, value)) {
MessageBox::Show("Non-numeric characters detected in 'Max Upload' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
} else {
FILE *OutFile = fopen("run.bat","w");
fprintf(OutFile,"#ECHO OFF\r\nC:\\Python26\\python.exe factory.py");
if(factoryname->Text != ""){
fprintf(OutFile," -f ");
fprintf(OutFile,factoryname->Text);
}
fclose(OutFile);
}
}
}
Any ideas? It is a simple windows form application. I am using Visual Studio C++ 2010
Thanks
Colum
If factoryname->Text identifies a property, then its getter may throw a CLR exception, in which case you will leak the file handle OutFile. Consider not using fopen, etc. It's a C library function and there are better alternatives such as std::ofstream.
Alternatively, as you have .NET available, you could use StreamWriter which will let you pass System::String and thus avoid your conversion problem as well:
StreamWriter writer(someFilePath);
writer.WriteLine(someString);
C++/CLI will take care of closing the writer when the scope is exited.
It's simply a conversion error:
cannot convert parameter 2 from 'System::String ^' to 'const char *'
Possible solution is posted here:
What is the best way to convert between char* and System::String in C++/CLI
Opening Files
Convert System::String to const char*