Skip to content

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

Closed

Description

@bilburdev

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();

	}
	};
}

Activity

  1. szikra commented on Feb 2, 2017

    @szikra

    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

  2. ibroheem commented on Feb 2, 2017

    @ibroheem

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

    [Problem with Microsoft and its ideologies]

  3. nlohmann commented on Feb 3, 2017

    @nlohmann
    Owner

    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.

  4. nlohmann commented on Feb 5, 2017

    @nlohmann
    Owner

    @FunnyBilbur Did this solve your problem?

  5. bilburdev commented on Feb 8, 2017

    @bilburdev
    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;

    }
    
  6. bilburdev commented on Feb 8, 2017

    @bilburdev
    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"
    }

  7. nlohmann commented on Feb 8, 2017

    @nlohmann
    Owner
  8. bilburdev commented on Feb 8, 2017

    @bilburdev
    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));
    }

  9. bilburdev commented on Feb 8, 2017

    @bilburdev
    Author

    @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;

  10. bilburdev commented on Feb 8, 2017

    @bilburdev
    Author

    @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" //
    }

  11. nlohmann commented on Feb 8, 2017

    @nlohmann
    Owner

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

  12. bilburdev commented on Feb 8, 2017

    @bilburdev
    Author

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

  13. nlohmann commented on Feb 8, 2017

    @nlohmann
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions