ページ 11

VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月11日(月) 23:14
by とうだ
VC++2005でプログラムをつくっていて
publicの関数を作っているところです
labelへの文字出力をどう作れば良いのかまよっています

label.textへの入力は普通どのようにおこなえばいいか教えてください

label.text = =iteminfo::HB::itemname;
などの変数での入力したいです

イメージとしてはフォームAにあるボタン1を押すとフォームBがひらいて名前などをlabelに表示できるようにするプログラムを作りたいです

//インクルードとプロトタイプ宣言

コード:

#include <string> 
//Form1.h
#include <iostream> 
#include"item.h"
void setitemname(String^,int); 

    //関数本体↓ 
    void setitemname(string^ name,int cnt){ 
    cnt=0;//入力数の初期化 
    KVS::label1->Text=iteminfo::HB::itemname; 


} 
自分なりにクラスを作ってみました

//item.h

コード:

#include <string.h>
using namespace std;

class iteminfo{
	class HB{
	public:
		String itemname="HB";
		int	price=100;
	}
};

よろしくお願いします

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月12日(火) 12:29
by ookami
こんにちは。ookamiです。
C++/CLIでしょうか?
ご質問は、『label.textへの入力は普通どのようにおこなえばいいか教えてください .. 変数での入力したいです』とのことですが、
単に、
  String ^s="HB";
  label1->Text=s;
でも変数での代入にはなるんですが、たぶんそういうことではないですよね...^^;
何かエラーが出ているならそれを貼ってください。

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月12日(火) 14:01
by とうだ
kvs.hです

コード:

#include <string>
#include <iostream>
#include "iteminfo.h"
//void setitemname(String^,int);
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;


namespace test {

	/// <summary>
	/// KVS の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class KVS : public System::Windows::Forms::Form
	{
	private:
		String^ name;
	public:
		KVS(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
		}
		void setitemname(string name,int cnt){
			cnt=0;//入力数の初期化
			KVS::label1->Text=iteminfo::HB::price;

		
		}



	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~KVS()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(26, 334);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(118, 37);
			this->button1->TabIndex = 0;
			this->button1->Text = L"←";
			this->button1->UseVisualStyleBackColor = true;
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(150, 334);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(118, 37);
			this->button2->TabIndex = 1;
			this->button2->Text = L"→";
			this->button2->UseVisualStyleBackColor = true;
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(26, 378);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(118, 37);
			this->button3->TabIndex = 2;
			this->button3->Text = L"ズーム";
			this->button3->UseVisualStyleBackColor = true;
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(150, 378);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(118, 37);
			this->button4->TabIndex = 3;
			this->button4->Text = L"サーブ";
			this->button4->UseVisualStyleBackColor = true;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(21, 22);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(0, 12);
			this->label1->TabIndex = 4;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(148, 22);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(0, 12);
			this->label2->TabIndex = 5;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(24, 178);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(0, 12);
			this->label3->TabIndex = 6;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(148, 178);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(0, 12);
			this->label4->TabIndex = 7;
			// 
			// KVS
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(295, 415);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Name = L"KVS";
			this->StartPosition = System::Windows::Forms::FormStartPosition::Manual;
			this->Text = L"KVS";
			this->Load += gcnew System::EventHandler(this, &KVS::KVS_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void KVS_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
};
}
iteminfo.hです
ここにクラスをつくってここの itemname という変数をlabel.textに代入?したいです

コード:

#include <string.h>
using namespace std;

class iteminfo{
	class HB{
	public:
		String itemname="HB";
		int	price=100;
	}
};
エラー文です

1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C2146: 構文エラー : ';' が、識別子 'itemname' の前に必要です。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C2864: 'iteminfo::HB::itemname' : スタティック const 整数データ メンバ以外をクラス内で初期化することはできません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(8) : error C2864: 'iteminfo::HB::price' : スタティック const 整数データ メンバ以外をクラス内で初期化することはできません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(10) : error C2059: 構文エラー : '}'
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(10) : error C2143: 構文エラー : ';' が '}' の前にありません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(10) : error C2238: ';' の前に無効なトークンがあります。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(41) : error C2597: 静的でないメンバ 'iteminfo::HB::price' への参照が正しくありません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(41) : error C3867: 'iteminfo::HB::price': 関数呼び出しには引数リストがありません。メンバへのポインタを作成するために '&iteminfo::HB::price' を使用してください
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(41) : error C2440: '=' : '' から 'overloaded-function' に変換できません。
1>test.cpp
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C2146: 構文エラー : ';' が、識別子 'itemname' の前に必要です。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(7) : error C2864: 'iteminfo::HB::itemname' : スタティック const 整数データ メンバ以外をクラス内で初期化することはできません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(8) : error C2864: 'iteminfo::HB::price' : スタティック const 整数データ メンバ以外をクラス内で初期化することはできません
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(10) : error C2059: 構文エラー : '}'
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(10) : error C2143: 構文エラー : ';' が '}' の前にありません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\iteminfo.h(10) : error C2238: ';' の前に無効なトークンがあります。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(41) : error C2597: 静的でないメンバ 'iteminfo::HB::price' への参照が正しくありません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(41) : error C3867: 'iteminfo::HB::price': 関数呼び出しには引数リストがありません。メンバへのポインタを作成するために '&iteminfo::HB::price' を使用してください
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(41) : error C2440: '=' : '' から 'overloaded-function' に変換できません。

>ookamiさん
VC++でformプログラミングでプロジェクトを作成しました

kvs.hにある
void setitemname(string name,int cnt)で
どこからでも使えるようにしたいです

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月12日(火) 15:47
by ookami
クラスに静的メンバ変数を持たせたい場合は、static指定子を使います。
http://www.geocities.jp/ky_webid/cpp/language/019.html
エラーの半分くらいはこれで解決するはずです。

ただ、ちょっと設計に違和感を感じるというか。
アイテムの種類は「HB」だけではなく今後増やしていくんですよね?
「こんなことをしたいが、どんな設計がいいか」というトピックを立ててもいいかもしれませんね。

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月12日(火) 22:14
by YuO
必要が無いなら,C++/CLIでstd::stringやstd::iostreamなどのC++系のライブラリは使わない方がよいでしょう。
Formで使うのはSystem::Stringなので,std::(w)stringなどへの相互変換が必要になります。
std::(w)stringの機能がどうしても必要という状況で無ければ,CLIのライブラリを使うようにしましょう。
# 両方使うと混乱の原因になります。
とうだ さんが書きました:

コード:

#include <string.h>
using namespace std;

class iteminfo{
	class HB{
	public:
		String itemname="HB";
		int	price=100;
	}
};
色々問題がありますが……。

コード:

namespace iteminfo { // classである必然性が見つからないので名前空間に変更。
    ref class HB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    public:
        System::String ^ itemname; // Stringのハンドルを保持するように変更。
        int price;

        HB (void) : itemname("HB"), price(100) // 初期化を正しく初期化構文を使うように変更。
        {
        }
    };
}
かな,と。
C++/CLIの文法についての勉強が必要かと思います。
# C++の文法とC++/CLIの文法は同じところと異なるところがあります。

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月12日(火) 23:21
by とうだ
ookamiさん YuOさんありがとうございました


>必要が無いなら,C++/CLIでstd::stringやstd::iostreamなどのC++系のライブラリは使わない方がよいでしょう。
C++のライブラリを使って処理するのが基本だと思ってたので調べながら書いてみました

Formで使うのはSystem::Stringなので,std::(w)stringなどへの相互変換が必要になります。
std::(w)stringの機能がどうしても必要という状況で無ければ,CLIのライブラリを使うようにしましょう。

今後自分でやりたいことをより詳しく明記したいと思います

>ただ、ちょっと設計に違和感を感じるというか。
>アイテムの種類は「HB」だけではなく今後増やしていくんですよね?
>「こんなことをしたいが、どんな設計がいいか」というトピックを立ててもいいかもしれませんね。
設計とかよく考えてなかったです
今後 HBだけではなくてほかの名前で中身はほぼ一緒のものを増やしていく予定です 基本ができればですけど・・・

自分が作りたいプログラムは

①フォームAにあるHBボタンを押すとフォームBとフォームCが表示される
②同時にフォームBとフォームCに  [HB 1]や[HB 2]という感じにlabelに表示させる
③フォームAにあるHBボタンを押すごとに金額をカウントする
④フォームAにある合計ボタンを押すと会計する

ちなみにこれは実際にあるソフトを自分で再現できるところまで再現してみようとおもって始めました


まだこれからいろいろ付け加えたいところがありますが今は②を作成途中という感じです

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月13日(水) 00:21
by とうだ
iteminfo.hをこのようにしました
publicで今後いろいろなものを追加予定です

YuOさんのを参考にこうつくってみました

コード:

namespace iteminfo { // classである必然性が見つからないので名前空間に変更。
    ref class HB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    public:
        System::String ^ itemname; // Stringのハンドルを保持するように変更。
        int price;
 
        HB (void) : itemname("HB"), price(100) // 初期化を正しく初期化構文を使うように変更。
        {
        }
	
    };

	ref class CB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    public:
        System::String ^ itemname; // Stringのハンドルを保持するように変更。
        int price;
 
        CB (void) : itemname("CB"), price(120) // 初期化を正しく初期化構文を使うように変更。
        {
        }
	
    };

	ref class MCP { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    public:
        System::String ^ itemname; // Stringのハンドルを保持するように変更。
        int price;
 
        MCP (void) : itemname("マックポーク"), price(120) // 初期化を正しく初期化構文を使うように変更。
        {
        }
	
    };

	ref class CCRI { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    public:
        System::String ^ itemname; // Stringのハンドルを保持するように変更。
        int price;
 
        CCRI (void) : itemname("クリスプ"), price(100) // 初期化を正しく初期化構文を使うように変更。
        {
        }
	
    };

	ref class DCB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    public:
        System::String ^ itemname; // Stringのハンドルを保持するように変更。
        int price;
 
        DCB (void) : itemname("DCB"), price(270) // 初期化を正しく初期化構文を使うように変更。
        {
        }
	
    };
}

kvs.hの void setitemname の内容をいじってみました


#

コード:

include <string>
#include <iostream>
#include "iteminfo.h"
//void setitemname(String^,int);
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;


namespace test {

	/// <summary>
	/// KVS の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class KVS : public System::Windows::Forms::Form
	{
	private:
		String^ name;
	public:
		KVS(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
		}
		/*変更箇所*/
		void setitemname(System::string name,int cnt){
			cnt=0;//入力数の初期化
			cnt=cnt+1;
			
			//iteminfo::HB::HB();
			KVS::label1->Text=name;
			return
		//
		}

		

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~KVS()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(26, 334);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(118, 37);
			this->button1->TabIndex = 0;
			this->button1->Text = L"←";
			this->button1->UseVisualStyleBackColor = true;
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(150, 334);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(118, 37);
			this->button2->TabIndex = 1;
			this->button2->Text = L"→";
			this->button2->UseVisualStyleBackColor = true;
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(26, 378);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(118, 37);
			this->button3->TabIndex = 2;
			this->button3->Text = L"ズーム";
			this->button3->UseVisualStyleBackColor = true;
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(150, 378);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(118, 37);
			this->button4->TabIndex = 3;
			this->button4->Text = L"サーブ";
			this->button4->UseVisualStyleBackColor = true;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(21, 22);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(0, 12);
			this->label1->TabIndex = 4;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(148, 22);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(0, 12);
			this->label2->TabIndex = 5;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(24, 178);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(0, 12);
			this->label3->TabIndex = 6;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(148, 178);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(0, 12);
			this->label4->TabIndex = 7;
			// 
			// KVS
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(295, 415);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Name = L"KVS";
			this->StartPosition = System::Windows::Forms::FormStartPosition::Manual;
			this->Text = L"KVS";
			this->Load += gcnew System::EventHandler(this, &KVS::KVS_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void KVS_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
};
}
#

コード:

include "KVS.h"
#include "D_KVS.h"
#include "R_KVS.h"
#include "size.h"
#pragma once


namespace test {

	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>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TabControl^  tabControl1;
	protected: 
	private: System::Windows::Forms::TabPage^  tabPage1;
	private: System::Windows::Forms::TabPage^  tabPage2;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TabPage^  tabPage3;
	private: System::Windows::Forms::TabPage^  tabPage4;
	private: System::Windows::Forms::TabPage^  tabPage5;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Button^  button5;
	private: System::Windows::Forms::Button^  button6;
	private: System::Windows::Forms::Button^  button7;

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->tabControl1 = (gcnew System::Windows::Forms::TabControl());
			this->tabPage1 = (gcnew System::Windows::Forms::TabPage());
			this->tabPage2 = (gcnew System::Windows::Forms::TabPage());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->tabPage3 = (gcnew System::Windows::Forms::TabPage());
			this->tabPage4 = (gcnew System::Windows::Forms::TabPage());
			this->button7 = (gcnew System::Windows::Forms::Button());
			this->tabPage5 = (gcnew System::Windows::Forms::TabPage());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->button5 = (gcnew System::Windows::Forms::Button());
			this->button6 = (gcnew System::Windows::Forms::Button());
			this->tabControl1->SuspendLayout();
			this->tabPage2->SuspendLayout();
			this->tabPage4->SuspendLayout();
			this->SuspendLayout();
			// 
			// tabControl1
			// 
			this->tabControl1->Controls->Add(this->tabPage1);
			this->tabControl1->Controls->Add(this->tabPage2);
			this->tabControl1->Controls->Add(this->tabPage3);
			this->tabControl1->Controls->Add(this->tabPage4);
			this->tabControl1->Controls->Add(this->tabPage5);
			this->tabControl1->Location = System::Drawing::Point(232, 73);
			this->tabControl1->Name = L"tabControl1";
			this->tabControl1->SelectedIndex = 0;
			this->tabControl1->Size = System::Drawing::Size(466, 251);
			this->tabControl1->TabIndex = 0;
			// 
			// tabPage1
			// 
			this->tabPage1->Location = System::Drawing::Point(4, 21);
			this->tabPage1->Name = L"tabPage1";
			this->tabPage1->Padding = System::Windows::Forms::Padding(3);
			this->tabPage1->Size = System::Drawing::Size(458, 226);
			this->tabPage1->TabIndex = 0;
			this->tabPage1->Text = L"ブレックファースト";
			this->tabPage1->UseVisualStyleBackColor = true;
			// 
			// tabPage2
			// 
			this->tabPage2->Controls->Add(this->button2);
			this->tabPage2->Controls->Add(this->button1);
			this->tabPage2->Location = System::Drawing::Point(4, 21);
			this->tabPage2->Name = L"tabPage2";
			this->tabPage2->Padding = System::Windows::Forms::Padding(3);
			this->tabPage2->Size = System::Drawing::Size(458, 226);
			this->tabPage2->TabIndex = 1;
			this->tabPage2->Text = L"サンドイッチメニュー";
			this->tabPage2->UseVisualStyleBackColor = true;
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(79, 78);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(45, 46);
			this->button2->TabIndex = 1;
			this->button2->Text = L"CB";
			this->button2->UseVisualStyleBackColor = true;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(79, 26);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(45, 46);
			this->button1->TabIndex = 0;
			this->button1->Text = L"HB";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// tabPage3
			// 
			this->tabPage3->Location = System::Drawing::Point(4, 21);
			this->tabPage3->Name = L"tabPage3";
			this->tabPage3->Padding = System::Windows::Forms::Padding(3);
			this->tabPage3->Size = System::Drawing::Size(458, 226);
			this->tabPage3->TabIndex = 2;
			this->tabPage3->Text = L"サイドメニュー";
			this->tabPage3->UseVisualStyleBackColor = true;
			// 
			// tabPage4
			// 
			this->tabPage4->Controls->Add(this->button7);
			this->tabPage4->Location = System::Drawing::Point(4, 21);
			this->tabPage4->Name = L"tabPage4";
			this->tabPage4->Padding = System::Windows::Forms::Padding(3);
			this->tabPage4->Size = System::Drawing::Size(458, 226);
			this->tabPage4->TabIndex = 3;
			this->tabPage4->Text = L"ドリンクメニュー";
			this->tabPage4->UseVisualStyleBackColor = true;
			// 
			// button7
			// 
			this->button7->Location = System::Drawing::Point(14, 19);
			this->button7->Name = L"button7";
			this->button7->Size = System::Drawing::Size(58, 51);
			this->button7->TabIndex = 0;
			this->button7->Text = L"コーラ";
			this->button7->UseVisualStyleBackColor = true;
			this->button7->Click += gcnew System::EventHandler(this, &Form1::button7_Click);
			// 
			// tabPage5
			// 
			this->tabPage5->Location = System::Drawing::Point(4, 21);
			this->tabPage5->Name = L"tabPage5";
			this->tabPage5->Padding = System::Windows::Forms::Padding(3);
			this->tabPage5->Size = System::Drawing::Size(458, 226);
			this->tabPage5->TabIndex = 4;
			this->tabPage5->Text = L"ハッピーセットメニュー";
			this->tabPage5->UseVisualStyleBackColor = true;
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(590, 430);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(59, 39);
			this->button3->TabIndex = 1;
			this->button3->Text = L"IN/UP";
			this->button3->UseVisualStyleBackColor = true;
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(271, 426);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(59, 39);
			this->button4->TabIndex = 2;
			this->button4->Text = L"グリル";
			this->button4->UseVisualStyleBackColor = true;
			// 
			// button5
			// 
			this->button5->Location = System::Drawing::Point(704, 387);
			this->button5->Name = L"button5";
			this->button5->Size = System::Drawing::Size(103, 37);
			this->button5->TabIndex = 3;
			this->button5->Text = L"IN合計";
			this->button5->UseVisualStyleBackColor = true;
			// 
			// button6
			// 
			this->button6->Location = System::Drawing::Point(704, 430);
			this->button6->Name = L"button6";
			this->button6->Size = System::Drawing::Size(103, 37);
			this->button6->TabIndex = 4;
			this->button6->Text = L"OUT合計";
			this->button6->UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(816, 471);
			this->Controls->Add(this->button6);
			this->Controls->Add(this->button5);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->tabControl1);
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"POS1";
			this->tabControl1->ResumeLayout(false);
			this->tabPage2->ResumeLayout(false);
			this->tabPage4->ResumeLayout(false);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				//HB
				//KVSへの表示
				KVS^ f_kvs = gcnew KVS();
				f_kvs->Location = Point(0,0);
				iteminfo::HB^ HB =gcnew iteminfo::HB;
				setitemname(HB->itemname,1);
				f_kvs->Show();
				//HB
				//R_KVSへの表示
				R_KVS^ r_kvs = gcnew R_KVS();
				r_kvs->Location = Point(0,440);
				r_kvs->Show();
			 }
	private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {
				//D_KVSへの表示
				//D_KVS^ d_kvs = gcnew D_KVS();
				//d_kvs->Location = Point(1130,0);
				//d_kvs->Show();
				size^ d_s =gcnew size();
				d_s->Location = Point(600,250);
				d_s->Show();

			}
	};
}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) がボタンHBを押したときの処理です


今現在のエラーコードです

1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(40) : error C2039: 'string' : 'System' のメンバではありません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(45) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : 1 番目の引数を 'std::string' から 'System::String ^' に変換できません。(新しい機能 ; ヘルプを参照)
1> 使用可能なユーザー定義された変換演算子がない、または
1> この変換を実行可能なユーザー定義変換演算子がないか、または演算子を呼び出せません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(48) : error C2059: 構文エラー : '}'
1>test.cpp
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(40) : error C2039: 'string' : 'System' のメンバではありません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(45) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : 1 番目の引数を 'std::string' から 'System::String ^' に変換できません。(新しい機能 ; ヘルプを参照)
1> 使用可能なユーザー定義された変換演算子がない、または
1> この変換を実行可能なユーザー定義変換演算子がないか、または演算子を呼び出せません。
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\KVS.h(48) : error C2059: 構文エラー : '}'
1>d:\documents and settings\ti\my documents\visual studio 2005\projects\test\test\Form1.h(251) : error C3861: 'setitemname': 識別子が見つかりませんでした


void setitemname(System::string name,int cnt)の作り方とクラスへのアクセスの仕方が正しいかアドバイスお願いします

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月13日(水) 07:19
by naohiro19
ref class のメンバーはpublic メンバーでなく private メンバーにし、property 構文を使うようにしましょう。
.NET Frameworkによるプロパティ構文参照

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月13日(水) 13:38
by ookami
読む限りでは、たぶん今後、itemの種類が増えていき、同じフォームでいろいろなitemの情報を表示したり、買ったりできるようにするんだろうなぁ、と思います。

上で「設計が...」と言ったのは、各itemは同じクラスを継承した方が、プログラムが楽になるかもしれない(そうじゃないかもしれない)とか、
そういうことなんですが、まずは1種類のitemで、ともかくエラーを消して動くようにするところからですね。

いろいろ出ていますが、
string関連のエラーは、YuOさんも仰っているとおり、stringではなくString^を使ったほうがいいです。
void setitemname(System::string name,int cnt)

void setitemname(System::String ^name,int cnt)

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月13日(水) 16:15
by naohiro19
基本的には、C/C++ネイティブのライブラリに、C++/CLIの薄いラッパーDLLをかぶせ、C#でGUIを書く。
C++/CLIで、GUIを書いてもいいが、C#の方がより簡単にGUIを記述できるので、GUIはC#に任せたほうが良い。
ということです。

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月13日(水) 20:30
by とうだ
>naohiroさん

コード:

public ref class ConvertString
{
    String^ m_ManageString;
public:
    property String^ ManageString{
        void set(String^ value)
        {
            m_ManageString = value;
        }
        String^ get()
        {
            return this->m_ManageString;
        }
    }
};
を参考にこんな風につくってみました

コード:

/* String^ m_ManageString;
public:
    property String^ ManageString{
        void set(String^ value)
        {
            m_ManageString = value;
        }
        String^ get()
        {
            return this->m_ManageString;
        }
    }

*/

namespace iteminfo { // classである必然性が見つからないので名前空間に変更。
    ref class HB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
    String^  itemname;
	int      price;

	public:
		property String^ HB{
			void HB_neme_set(String^ value)
			{
				itemname = value;
			}

			String^ HB_name_get()
			{
				return this->itemname;
			}
			
			void HB_price_set(int value)
			{
				price = value;
			}

			String^ HB_price_get()
			{
				return this->price;
			}

        //System::String ^ itemname; // Stringのハンドルを保持するように変更。
        //int price;
			HB_Setup(){
				price=100;
				String^  itemname="HB";

			}

        HB (void) : itemname("HB"), price(100) // 初期化を正しく初期化構文を使うように変更。
        {
        }
	
    };
こんな感じであってますか?

上の


String^ itemname;
int price;

はprivateになるからpublicをつかって間接的に参照するってのがクラスの隠蔽にあたる部分ですよね?
C++の知識も乏しいもので・・・確認のために考え方とか間違ってたら指摘お願いします

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月14日(木) 11:06
by naohiro19
プロパティ構文で許されているのは get と set のみしか許されていません。
従って、とうださんの書いたプロパティ構文はC++/CLIの仕様に沿っていません。正しくは以下の通りに書き直すべきです。

コード:

preperty 型名 プロパティ名 {
      void set(型名 value){  メンバー変数 = value; }
      型名 get(){ return メンバー変数; }
}
基本的にプロパティはメンバー変数につき1つしか存在しないといけません。
以下のC#のコードですが、C#ではメンバー変数に対するプロパティは1つしか入れることしかできません。つまりC++/CLIでも同様なことが言えます。

コード:

namespace SomethingNamespace
{
    public class SomothingClass
    {
        int price;
        string itemname;

        public int Price
        {
            get { return price; }
            set { price = value; }
        }
        public string Itemname
        {
            get { return itemname; }
            set { itemname = value; }
        }
    }
}

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月14日(木) 12:09
by とうだ
>naohiroさん

ありがとうございます
ちょっと難しくてよく理解できないのですが


メンバー変数に対するプロパティ とはどれのことですか?

public class SomothingClass のことであってますか?

コード:

 public int Price
        {
            get { return price; }
            set { price = value; }
        }
これのことですか?

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月14日(木) 13:47
by naohiro19
それです。

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月14日(木) 20:43
by とうだ

コード:

namespace iteminfo { // classである必然性が見つからないので名前空間に変更。
//   namespace SomethingNamespace
//{
//    public class SomothingClass
//    {
//        int price;
//        string itemname;
// 
//        public int Price
//        {
//            get { return price; }
//            set { price = value; }
//        }
//        public string Itemname
//        {
//            get { return itemname; }
//            set { itemname = value; }
//        }
//    }

//    public class SomothingClass	
	ref  class HB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
	/*String ^ itemname;
	int      price;*/
		//        int price;
		//        string itemname;
		//public:
		//property String^ HB{
			int price;
			int cnt;
			System::String ^ itemname;
 
			public int Price(void)
			{
				get { return  price; }
				set { price = value; }
			}
			public String Itemname(void)
			{
				get { return itemname; }
				set { itemname = value; }
			}
			public int Cnt(void)
			{
				get { return  cnt; }
				set { cnt = value; }
			}
	}

        //System::String ^ itemname; // Stringのハンドルを保持するように変更。
        //int price;
			HB_Setup(){
				HB::price=100;
				HB::String^  itemname="HB";

			}

        //HB (void) : itemname("HB"), price(100) // 初期化を正しく初期化構文を使うように変更。
        //{
        //}
	
    };

}
のように定義してみたのですがエラーが出ます


error C2146: 構文エラー : ';' が、識別子 'itemname' の前に必要です。
visual studio 2005\projects\test\test\iteminfo.h(26) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
visual studio 2005\projects\test\test\iteminfo.h(26) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
visual studio 2005\projects\test\test\iteminfo.h(28) : error C2144: 構文エラー : 'int' は ':' によって先行されなければなりません。
visual studio 2005\projects\test\test\iteminfo.h(29) : error C2473: 'Price' : 関数定義のようですが、パラメータ リストがありません。
visual studio 2005\projects\test\test\iteminfo.h(30) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
visual studio 2005\projects\test\test\iteminfo.h(30) : error C2473: 'get' : 関数定義のようですが、パラメータ リストがありません。
visual studio 2005\projects\test\test\iteminfo.h(30) : error C2059: 構文エラー : 'return'
visual studio 2005\projects\test\test\iteminfo.h(30) : error C2238: ';' の前に無効なトークンがあります。
visual studio 2005\projects\test\test\iteminfo.h(31) : error C2470: 'set' : 関数定義のようですが、パラメータ リストがありません。外見上の本体をスキップします。
visual studio 2005\projects\test\test\iteminfo.h(33) : error C2059: 構文エラー : 'public'
visual studio 2005\projects\test\test\iteminfo.h(34) : error C2143: 構文エラー : ';' が '{' の前にありません。
visual studio 2005\projects\test\test\iteminfo.h(34) : error C2447: '{' : 対応する関数ヘッダーがありません (旧形式の仮引数リスト?)

これってC#のコードだからエラーがでてるってことでいいのですか?

わかる範囲でC++に書き換えてみたつもりなんですけどエラーなおりません
わかるかた例みせてもらえませんか?
お願いばかりですいません

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月15日(金) 14:31
by ookami
YuOさんのアドバイスと、naohiro19さんのアドバイスを、もう一度よく読んでください。

YuOさんのアドバイスで、
> 必要が無いなら,C++/CLIでstd::stringやstd::iostreamなどのC++系のライブラリは使わない方がよいでしょう。
→stringがまだ残っていますよね?String^に統一した方がよいです。

また、naohiro19さんのアドバイスで、

コード:

property 型名 プロパティ名 {
      void set(型名 value){  メンバー変数 = value; }
      型名 get(){ return メンバー変数; }
}
となっていますが、とうださんのコードではそうなっていないです。

個人的には、「設計は変えない」と言いながらガンガン変えられていて面食らっています。

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月15日(金) 23:32
by とうだ
すいません よく読みなおしてコード統一してstring^に書き換えてみました

コード:

namespace iteminfo { // classである必然性が見つからないので名前空間に変更。


//    public class SomothingClass	
	ref  class HB { // unmanaged classである必然性が見つからないのでmanaged classに変更。
	
		//property String^ HB{
			int price;
			System::String^ itemname;

 
			property int Price
			{
				void set(int value){  Price = value; }
				int get(){ return Price; }
			
			}
			property System::String^ Itemname
			{
				void set(System::String^ value){  Itemname = value; }
				System::String^ get(){ return  Itemname; }
				
			}

    };

property変数アクセスはどうすればいいのですか?
別途publicの関数をつくってアクセスすればいいのですか?

変数の追加などが設計を変えることになるのですか?
自分の設計に関する認識があまかったですすいませんでした

}

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月16日(土) 09:53
by naohiro19
C++/CLIのref class もしくは ref struct のクラスは規定でprivate なのでほかのクラスからはアクセスできません。
メンバー変数はprivate、メソッド、イベント、デリゲート、プロパティは public にする必要があります。
19.1 クラス定義 or プロパティの宣言

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月16日(土) 22:39
by とうだ
http://vene.wankuma.com/ecma372/19_1_5_ ... ember.aspx
にある

コード:

public ref class Point {
private:
    int x;
    int y;
public:
    property int X {
        int get() { return X; }               // インライン定義
        void get(int value);                  // 宣言のみ
    }
    property int Y {
        int get();                            // 宣言のみ
        void set(int value) { y = value; }    // インライン定義
    }
    ...
};
void Point::X::set(int value) { x= value; }
int Point::Y::get() { return y; }
]
を参考にしてみました

refclassからprivate public property の書き方は一緒だったので

また質問なんですけど

private なのでほかのクラスからはアクセスできないから
下のコードで間接的にアクセスしてると考えてだいじょうぶですか?
void Point::X::set(int value) { x= value; }
int Point::Y::get() { return y; }


上のコードを参考に
void HB::System::String^ Itemname::set(System::String^ value){ Itemname = value; }
としてみたのですが参考にするものまちがえてますか?

Re: VC++でのlabelへの入力とクラスの作成のしかた

Posted: 2012年6月18日(月) 11:51
by ookami
> 変数の追加などが設計を変えることになるのですか?
→そうではないですがここでは置いておきます。

naohiro19さんが仰っているのは、メンバだけでなくプロパティまでprivateになっている、ということです。
ソースコードを変更したなら、変更後のソースと、エラーを貼ったほうが良いです。