Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to create Windows Application in Visual Studio 2015 c++, and i have a problem #443

Closed
DmytriiTsybuliak opened this issue Feb 2, 2017 · 13 comments
Labels

Comments

@DmytriiTsybuliak
Copy link

DmytriiTsybuliak commented Feb 2, 2017

The problem in comment below:

#pragma once
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
namespace Project1 {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	public: System::Windows::Forms::Button^  button1;
	public: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	public:
	protected:

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(157, 164);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(81, 35);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(48, 32);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(173, 20);
			this->textBox1->TabIndex = 1;
			this->textBox1->Text = "Hi Dima";
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(48, 226);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(184, 20);
			this->textBox2->TabIndex = 2;
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button1);
			this->Name = L"MyForm";
			this->Text = L"MyForm";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: int TextSave() {
		using namespace std;
		String^ T = textBox1->Text;
		ofstream file;
		file.open("package.json");
		json obj;
		obj["name"] = T; // <--  Error "No operator "=" matches these operands
		file << obj;
		file.close();
		cin.get();
		return 0;

		
	}
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
		TextSave();

	}
	};
}
@szikra
Copy link

szikra commented Feb 2, 2017

I'm not familiar with managed C++ but T isn't Platform::String class? Shouldn't that be converted to std::string when trying to assign it to obj["name"]?

something like http://stackoverflow.com/questions/28759212/convert-platformstring-to-stdstring

@ibroheem
Copy link

ibroheem commented Feb 2, 2017

This issue is not related to the library.
The above link @szikra should solve your issue though.

[Problem with Microsoft and its ideologies]

@nlohmann nlohmann added the platform: visual studio related to MSVC label Feb 3, 2017
@nlohmann
Copy link
Owner

nlohmann commented Feb 3, 2017

I also think this conversion is out of scope of the library. However, you could add a conversion from Platform::String to json by implementing a to_json function which basically does what is described in the StackOverflow article. See https://github.com/nlohmann/json#arbitrary-types-conversions on how to do this.

@nlohmann
Copy link
Owner

nlohmann commented Feb 5, 2017

@FunnyBilbur Did this solve your problem?

@DmytriiTsybuliak
Copy link
Author

@nlohmann Yes. I'm just convert String^ to string:
void MarshalString(String ^ s, std::string& os) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
private: int TextSave() {
using namespace std;
String^ das = textBox1->Text;
string da;
MarshalString(das, da);
String^ p1 = textBox2->Text;
string p2;
MarshalString(p1, p2);
ofstream file;
file.open("package.json");
json obj;
obj["name"] = da;
obj["name1"]=p2;
obj["name2"]="Dima";
file << obj;
file.close();
cin.get();
return 0;

}

@DmytriiTsybuliak
Copy link
Author

@nlohmann But i Have new question. My program writes the data sequentially into one line(package.json). How to fix this?
primary result: {"name":"Dima1997","name1":"Oleg1997","name2":"Dima"}
but i want like this:
{
"name":"Dima1997",
"name1":"Oleg1997",
"name2":"Dima"
}

@nlohmann
Copy link
Owner

nlohmann commented Feb 8, 2017

The dump() function has a parameter for indentation, see https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5319dc1bb9dfe19ce7ff559aaded3422.html#a5319dc1bb9dfe19ce7ff559aaded3422

@DmytriiTsybuliak
Copy link
Author

@nlohmann thanks. Working. Okay. Then I need convert type string to String^
How to change this method for me?:
void MarshalString(String ^ s, std::string& os) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}

@DmytriiTsybuliak
Copy link
Author

DmytriiTsybuliak commented Feb 8, 2017

@nlohmann Nevermind. I found the way:
json array;
array["name"] = "Dima";
array["name1"] = "Oleg";
array["name2"] = da; // it's TextBox1
array["name3"] = "Alina";
array["name4"] = "Vika";
string sa = array.at("name2");
String^ s = gcnew String(sa.c_str());
label1->Text = s;

@DmytriiTsybuliak
Copy link
Author

DmytriiTsybuliak commented Feb 8, 2017

@nlohmann Look, i need your help.
private: int TextSave() {
using namespace std;
....
.....
...
ofstream file;
file.open("package.json");
json obj;
obj["PCB1"] = d1;
obj["PCB2"]= d2;
obj["PCB3"] = d3;
obj["PCB4"] = d4;
obj["PCB5"] = d5;
obj["PCB6"] = d6;
file << obj.dump(0)<< "\n\n";
file << obj;
file.close();
cin.get();
return 0; }

look: I enter this data:
d1= aa
d2 = bb
d3= cc
d4 = dd
d5 = ee
d6 =ff
and in file package.json I have next results:
{
"PCB1": "aa",
"PCB2": "bb",
"PCB3": "aa", //How is possible? In this place must be "cc"
"PCB4": "aa", //
"PCB5": "aa", //
"PCB6": "aa" //
}

@nlohmann
Copy link
Owner

nlohmann commented Feb 8, 2017

Hard to tell from the code you show. This depends on the type of d1 ... d6.

@DmytriiTsybuliak
Copy link
Author

@nlohmann it's my stupid mistake, because d3-d6 = textBox1->Text ))))))
Thanks

@nlohmann
Copy link
Owner

nlohmann commented Feb 8, 2017

Great. I shall close this ticket, as the original issue has been solved. Please feel free to open a new issue in case you encounter another problem.

@nlohmann nlohmann closed this as completed Feb 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants