VC++でラベルに表示できない[.NET]

このQ&Aのポイント
  • VC++でラベルに表示できないエラーが発生しています。
  • プログラム中のlabel1が定義されていない識別子としてエラーが出ています。
  • 初心者がつまずいているVC++でのラベル表示エラーについて質問しました。
回答を見る
  • ベストアンサー

VC++でラベルに表示できない[.NET]

VC++でラベルに表示できない[.NET] 以下のプログラムですがエラーが出ます。 #include "stdafx.h" #include "Form1.h" using namespace CursorXY1; [STAThreadAttribute] int main(array<System::String ^> ^args) { static POINT Point; // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // メイン ウィンドウを作成して、実行します Application::Run(gcnew Form1()); label1->Text="これはラベルです"; return 0; } エラー内容: 1>.\Form1.cpp(27) : error C2065: 'label1' : 定義されていない識別子です。 1>.\Form1.cpp(27) : error C2227: '->Text' : 左側がクラス、構造体、共用体、ジェネリック型へのポインタではありません。 1> 型は ''unknown-type'' です。 ヘッダーファイルに「Form1.h,resource.h,stdafx.h」があります。 Designerでlabel1を設置しました。 Windowsフォームアプリケーションを選択しました。 初心者ゆえにずっとつまいずいていて、このたび質問させていただくことになりました。 よろしくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • samtomsan
  • ベストアンサー率55% (1060/1897)
回答No.3

すでにANo.2さんが回答してくださいましたが、mainの中のApplication::Run(gcnew Form1());が働かないとフォームに関する処理ができません。 フォームに関する処理はFormが生成されてからFormの中で行ないます。 フォームと関係ない、フォームを表示する前の処理あるいはフォームを閉じてからの処理は main の中で行うことができます。 例えば、何かの条件をチェックして、条件によってはフォームを表示しないでプログラムを終了させる場合はApplication::Run(gcnew Form1());より前に書きます。 フォームを閉じてからの処理は後ろに書きます。 もっとも、ほとんどの処理はフォームと関係しますよね。 何処に何を書くあるいは書かなければいけない内容かを知る必要がありますね。

ikx522
質問者

お礼

なるほど~よ~くわかったような気がします。 どちらの方も本当にありがとうございます。 ベストアンサーは1番目に回答してくださったsamtomsanにさせていただきます。 おふたり方どうもありがとうございました。

その他の回答 (2)

回答No.2

 こんばんは。  ラベルは存在しますが、呼び出している個所が悪いだけでは。  Form1.hの中に書きます。 namespace winclr { 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 System::Runtime::InteropServices; /// <summary> /// Form1 の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // //取りあえずここら辺 label1->Text="これはラベルです"; } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } //又はLoadイベントを追加して、ここら辺 private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { label1->Text="これはラベルです"; } }; }

ikx522
質問者

お礼

回答ありがとうございます。 Form1.h以外ではダメなんですねぇ。 頭がコンガラがってるのでひとまずゆっくり理解してみることにします。

  • samtomsan
  • ベストアンサー率55% (1060/1897)
回答No.1

> 1>.\Form1.cpp(27) : error C2065: 'label1' : 定義されていない識別子です。 Formにラベルが貼り付けてありませんね。 フォームの表示の方法は知っていますか。

ikx522
質問者

お礼

何度も何度もも申し訳ありません。 フォームにはlabel1がちゃんんと張り付けられています。

ikx522
質問者

補足

分からないかもしれません... 教えていただけると嬉しいのですが。 申し訳ありません...

関連するQ&A

  • VC++2008で、新規フォームアプリのメインプロジェクトの内容について

    aという名前を付けて作ったら、a.ccpの内容は以下のようになっていました。 --------- // a.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include "Form1.h" using namespace a; [STAThreadAttribute] int main(array<System::String ^> ^args) { // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // メイン ウィンドウを作成して、実行します Application::Run(gcnew Form1()); return 0; } --------- この、 using namespace a; の行までと//つきのコメントの行は意味は分かるのですが、 それ以外の細かい意味がよく分かりません。 ・main関数の()内にマネージ配列がこのように入っていると、結果的にどういう動作になるのでしょうか?また、return 0でどうなりますか? ・Application::EnableVisualStyles(); は、「visual スタイル」を有効にするための表記だと聞きましたが、その「visual スタイル」というのはなんなのでしょうか? ・Application::SetCompatibleTextRenderingDefault(false); というのは、スタンドアロンの Windows フォーム アプリケーション用の表記なのですよね?テキスト描画方法?に関係してる、みたいなことを見た気がしますが、具体的にはtrueにするとどうなって、falseにするとどうなるのでしょうか? ・Application::Run(gcnew Form1()); の()内は、Form1.hで実装済みのFormクラスを継承して作られていたrefクラス「Form1」のインスタンスを『無名』で作り、それに対してApplication::Runメソッドを実行する、という意味なのでしょうか?

  • VC++6.0でテンプレートクラスを用いたいのですがエラーが出ます

    'CImageTemplate' : 指定の指定のテンプレート クラスはすでに定義されています と言われてしまいます. まだC++の知識が浅いのですが間違っている箇所がわかれば教えてください. ---------テンプレートのヘッダ(ImageTemplate.h)--------- template <class TempData> class CImageTemplate{ private:   TempData **Data;//データ本体   … protected:   …   InitData(int h, int w); }; ---------テンプレートのcpp(ImageTemplate.cpp)--------- #include "stdafx.h" #include "ImageTemplate.h" template<class TempData>CImageTemplate<TempData>::InitData(int h, int w) {…} ---------呼び出し側のヘッダ(ImageLabel.h)--------- #include "ImageTemplate.h" class CImageLabel{ private:   CImageTemplate < int > Label;   … }; ---------呼び出し側のcpp(ImageLabel.cpp)--------- #include "stdafx.h" #include "ImageLabel.h" #include "ImageTemplate.h" CImageLabel::InitLabel(int h, int w) { if(!Label.CheckData()){ Label.InitData(h, w); } }

  • Windowsフォームの表示

    Visual C++ 2005(Express Edition)で、windowsフォームを3個作りました。 1個目をForm1.h、2個目をForm2.h、3個目をForm3.h、メインプロジェクトは、Sample.cpp, ファイルとします。 Form1にボタンbutton1を作成し、ボタンをクリックしたイベントが発生したとき、Form2を表示させます。 Form2には、ボタンbutton2があります。 Form1と同様に、Form2のボタンをクリックするとForm3の画面が表示されるようにします。 ところが、以下のソースファイルで書いたところ、Form2とForm3は、名前空間に存在しませんという意味のエラーが生じます。 Form2とForm3をインスタンス化し、Show()メッソドで、画面を表示させる方法です。 しかし、Form1とForm2だけのファイルですと、先のソースの書き方で上手くいきます。 何か、書き方が異なるのでしょうか?正しい書き方を教えてくださいますとありがたいです。 なお、C++/CLIの文法に沿った、ソースコードでお願いします。 <ソースファイル> ----Form1.h----------------------------- #pragma once #include "stdafx.h" #include "Form2.h" #include "Form3.h" namespace Sample { 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 { //button1を作成 //省略 #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2^ a = gcnew Form2(); a->Show(); } }; } [EOF] --------------------------------------- ----Form2.h---------------------------- #pragma once #include "Form1.h" #include "Form3.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace Sample { /// <summary> /// IsotopeInput の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form2 : public System::Windows::Forms::Form { //button2を作成 //省略 } #pragma endregion private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { Form3^ b = gcnew Form3(); b->Show(); } }; } [EOF] --------------------------------------- -------Form3.h-------------------------- #pragma once #include "stdafx.h" #include "Form1.h" #include "Form2.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace Sample { /// <summary> /// Result の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form3 : public System::Windows::Forms::Form { //省略 } #pragma endregion }; } [EOF] --------------------------------------------------------- Sample.cpp----------------------------------------------- // Sample.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include "Form1.h" #include "Form2.h" #include "Form3.h" using namespace Sample; [STAThreadAttribute] int main(array<System::String ^> ^args) { // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // メイン ウィンドウを作成して、実行します Application::Run(gcnew Form1()); return 0; } [EOF] ------------------------------------------------------------

  • VC++6.0 DLLの作り方

    VC++6.0でWin32 Dynamic-Link Libraryプロジェクトを作成しようとしました。 練習用に足し算と引き算です。 StdAfx.cpp test99.cpp StdAfx.h ReadMe.txt Basetsd.hなど、いろいろなファイルが出来ています。 これを親プログラムから、#include "StdAfx.h"としてリンクさせたいのですが、 StdAfx.hに、関数を公開しなければならないと思います。 StdAfx.hは、何を行うヘッダーファイルなのか? 何処に何を記述するのがベストでしょうか? VC++の生成した日本語のコメントの意味が今ひとつ理解できません。 以下はtest99.cpp #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } __declspec(dllexport) int WINAPI tasizan(int a,int b) {return (a+b);} __declspec(dllexport) int WINAPI hikizan(int a,int b) {return (a-b);} 以下はStdAfx.cpp #include "stdafx.h" 以下はStdAfx.h #if !defined(AFX_STDAFX_H__D849B748_701B_483D_9457_AA6096D78FB0__INCLUDED_) #define AFX_STDAFX_H__D849B748_701B_483D_9457_AA6096D78FB0__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define WIN32_LEAN_AND_MEAN #include <windows.h> //????????__declspec(dllexport) int WINAPI tasizan(int,int); //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ は前行の直前に追加の宣言を挿入します。 #endif // !defined(AFX_STDAFX_H__D849B748_701B_483D_9457_AA6096D78FB0__INCLUDED_)

  • VC++ 2008で別classからFormがincludeできません

    お世話になります。 タイトルの通り、別ソースで書いてあるクラスでForm1のincludeができません。includeすれば、そのclassの型が使用できるという認識なのですが、何か他にしなければならない事があるのでしょうか…。 環境はVisual C++ 2008 Express Editionです。 以下、ソースです。 [Form1.h] #pragma once #include "testclass.h" namespace testAppli { (略) private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { testclass *aaa = new testclass(); aaa->testfunc(this); } [testclass.h] #pragma once #include "stdafx.h" #include "Form1.h" class testclass { public: void testfunc(Form1^ aaa); }; [testclass.cpp] #include "stdafx.h" #include "testclass.h" void testclass::testfunc(Form1^ aaa) { return; } 以下のコンパイルエラーが出ています。 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\Form1.h(98) : error C2065: 'testclass' : 定義されていない識別子です。 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\Form1.h(98) : error C2065: 'aaa' : 定義されていない識別子です。 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\Form1.h(98) : error C2061: 構文エラー : 識別子 'testclass' 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\Form1.h(99) : error C2065: 'aaa' : 定義されていない識別子です。 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\Form1.h(99) : error C2227: '->testfunc' : 左側がクラス、構造体、共用体、ジェネリック型へのポインタではありません。 1> 型は ''unknown-type'' です。 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\testclass.h(8) : error C2061: 構文エラー : 識別子 'Form1' 1>.\testclass.cpp(4) : error C2065: 'Form1' : 定義されていない識別子です。 1>.\testclass.cpp(4) : error C2065: 'aaa' : 定義されていない識別子です。 1>.\testclass.cpp(5) : error C2448: 'testclass::testfunc' : 関数の定義が間違っています。 1>testAppli.cpp 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\testclass.h(8) : error C2061: 構文エラー : 識別子 'Form1' 1>c:\documents and settings\in flames\my documents\visual studio 2008\projects\testappli\testappli\Form1.h(99) : error C2660: 'testclass::testfunc' : 関数に 1 個の引数を指定できません。

  • [VC++]ヘッダファイルからの関数コール

    Formのボタンクリックイベント(Form.h)から 別ファイル(.cpp)の関数をコールしたいのですがうまくいきません、 どのようにすればよいでしょうか?ご教授お願いします。 1> : error C2144: 構文エラー : 'void' は ')' によって先行されなければなりません。 1> : error C2059: 構文エラー : ')' //----- main1.h #include <stdio.h> #include "Form1.h" namespace A { void main1(); } //----- main1.cpp #include <stdio.h> #include "main1.h" using namespace A; void main1() { //処理 } //----- Form1.h #include <stdio.h> #include "main1.h" void main1(void); namespace A { public ref class Form1 : public System::Windows::Forms::Form { public: private: System::Void btn_Click(System::Object^ sender, System::EventArgs^ e) { //★クリックイベントからmain1の関数コールしたい main1(void); } }; }

  • 別スレッドからメインスレッドのテキストボックスに文字を表示させたい

    Visual C++ 2005 Express Editionを使用している初心者です。 タイトルの方法がどうしてもわからないので教えてください。 うまく説明できないので聞きたいところのコードを載せます。 //SAMPLE.cpp #include "stdafx.h" #include "Form1.h" using namespace SAMPLE; int main(array<System::String ^> ^args) { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); Application::Run(gcnew Form1()); } //Form1.h #include <process.h> #include <vcclr.h> namespace SAMPLE { unsigned __stdcall counter(void *arg); int thread_id1; unsigned dummy; using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public:Form1(void) { InitializeComponent(); //別スレッド起動 thread_id1=_beginthreadex(NULL,0,counter,(void *)1,0,&dummy); } private: System::Windows::Forms::TextBox^ textBox1; private: System::ComponentModel::Container ^components; void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); //省略(コンポーネント初期化) } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; unsigned __stdcall counter(void *arg){ //別スレッド //ここで文字列を記述して上のtextBox1に表示したい return 0; } } 初歩的なことかもしれませんが、 よろしくお願いします。

  • atlbase.h と afxwin.hの同時定義

    こんにちは、honiyonです。  VC++で、MFCを用いてNT用のサービスプログラムを作成しています。  ATL COM AppWizardで、サービスアプリケーションを選択したので、atlbase.hが StdAfx.hに宣言されています。  CString型を使いたいので、該当cppファイルに、afxwin.hをincludeしました。 何故か StdAfx.hより先に afxwin.hをincludeすると、CStringの定義が正しくされないようなので、後にもってきています。 #include "StdAfx.h" #include <AfxWin.h">  この状態だと、atlbase.h, AfxWin.h双方で windows.hをincludeしているようで、AfxWin.hが参照する afxv_w32.hで、以下のようなエラーが発生します。 fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>  この問題を回避するために、atlbase.hやAfxWin.hを改造して回避出来そうですが、それは今後別のものを作る時の支障になりそうなので避けたいです。  その他の方法で回避する事は出来ないでしょうか?  宜しくお願いします(..

  • VC++2010 EE で #ifdef _MSC_VER #endi

    VC++2010 EE で #ifdef _MSC_VER #endif でエラー ソース始まり // _msc_ver.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #ifdef _MSC_VER #include "stdafx.h" #endif int _tmain(int argc, _TCHAR* argv[]) { return 0; } ソース終わり > fatal error C1020: 予期しない #endif です。 と出ます。なぜでしょうか?

  • VC++ 2010 Expressでのビルド

    VC++2010 Expressを使ってC++を書いているのですが、 今までは、使用するクラスファイルを全てエントリーポインのある int _tmain(int argc, _TCHAR* argv[])の上に記述して使っていたのですが、クラスの数が増えてきたので、エントリーファイルと同階層のソースファイルフォルダの下に置くようにしました。この場合、たとえば下記のメイン部分のように、 基底クラスの読み込み順を考量して、#includeしているのですが、エントリポイントのファイルを「右クリック」->「コンパイル」はエラー無しではけるようになったのですが、 F5キーでデバッグを開始してビルドする多くのエラーをはいてしまいます。 とりあえず、全てのクラスファイルをインクルードした形のエントリポインタのファイルのコンパイルはうまくいっているので、その結果を使ってビルドする方法が分かりかねています。 Expressの使い方の問題だと思うので、御経験のある方、御教示願えたらと思います。 <メイン部分> #include "stdafx.h" #include <iostream> using namespace std; #include "point.cpp" #include "canvas.cpp" #include "shape.cpp" #include "rect.cpp" #include "triangle.cpp" #include "square.cpp" int _tmain(int argc, _TCHAR* argv[]) { Canvas c; Shape *shape[3]; Rect r(5, 5, 20, 10); shape[0] = &r; Triangle t(1, 18, 16, 8); shape[1] = &t; Square s(22, 2, 6); shape[2] = &s; for (int i=0; i<3; i++) shape[i]->draw(&c); c.show(); getchar(); return 0; }

専門家に質問してみよう