• 締切済み

VC++2010で配列に文字列を使用する方法

array<String^,3>^ myMat = gcnew array<String^,3>(15,97,3338); このようにするとビルド出来るのですが、 array<String^,3>^ myMat = gcnew array<String^,3>("あ","か","さ"); とすると 1>d:\documents and settings\****\my documents\visual studio 2010\projects\****\Form1.h(220): error C2440: '初期化中' : 'const char [3]' から 'int' に変換できません。 1> この変換が可能なコンテキストはありません。 とエラーが出てしまいます どのようにすれば良いのでしょうか?

みんなの回答

  • osamuy
  • ベストアンサー率42% (1231/2878)
回答No.2

初期化リストと次元サイズ指定を混同しているのでは。 詳しくは、gcnewのヘルプをご覧ください。

  • wormhole
  • ベストアンサー率28% (1621/5656)
回答No.1

>どのようにすれば良いのでしょうか? そもそも何がしたいのかがわからないですが。 C++/CLIはほとんどわかりませんが >array<String^,3>(15,97,3338); Stringの3次元配列で、0次の要素数15, 1次の要素数97, 2次の要素数3338の意味だったと思いますが、 n次の要素数を書くところに文字列を書いて何をしようとしてるのでしょうか。

関連するQ&A

  • VC++2010で配列を使用する方法

    String ^test[10]; とすると 1>d:\documents and settings\my documents\visual studio 2010\projects\Form1.h(233): error C2728: 'System::String ^' : ネイティブ配列はこのマネージ型を含むことはできません とエラーが出ます どのようにすれば良いのでしょうか?

  • String型をchar型配列にしたい

     VC++2005ExpressEdition + PlatformSDKを使用しています.  String型をchar型配列に変換したいと思っています.  自分なりに調べてみたのですが, > String^ str = gcnew String("test"); > char chr[5]; > chr = str->ToCharArray(); などとしても次のようなエラーが出ます. > error C2440: '=' : 'cli::array<Type,dimension> ^' から 'char [5]' に変換できません。  String型からchar型配列への変換はどのようにすればいいのでしょうか.

  • 起動時の引数の取得方法が分からない。

    C++/CLIのFormでの開発なのですが、function(char *str)に起動時の引数を渡したいと考えたのですが、array<System::String ^>をchar型に変える方法が分からず、苦戦しております。 どうやったら良いのでしょうか? エラーは次のように出ます。 CouponPrint.cpp(22): error C2664: 'PtrToStringChars' : 1 番目の引数を 'cli::array<Type> ^' から '__const_String_handle' に変換できません。(新しい機能 ; ヘルプを参照) with [ Type=System::String ^ ] この変換を実行可能なユーザー定義変換演算子がないか、または演算子を呼び出せません。 以下、そのときのソースコード int main(array<System::String ^> ^args) { // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); if(!args) { size_t convertedChars = 0; size_t sizeInBytes = ((args->Length + 1) * 2); const wchar_t *wch = PtrToStringChars(args); char *ch = (char *)malloc(sizeInBytes); errno_t err = 0; err = wcstombs_s(&convertedChars, ch, sizeInBytes, wch, sizeInBytes); if (err != 0) printf_s("wcstombs_s failed! \n"); function(ch); free(ch); } // メイン ウィンドウを作成して、実行します Application::Run(gcnew Form1()); return 0; }

  • 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 個の引数を指定できません。

  • [C++/CLI]マネージコードで配列を作成してネイティブコードの配列にコピーしたい

    Visual C++ 2005のC++/CLIでプログラム作成しています。 C言語で書かれたプログラムを組み込みたい為に、 マネージコードでテキストファイル読み込みをして、2次元配列に格納し、 int型に変換してネイティブコードの配列にコピーしたいのですが、上手く書けません。。 テキスト内容は数字のみで2次元に並んでおり、1文字ずつ配列に入れたいです。 エラー出ているのですが、下記にコードを載せます。 #include<string> #include<vcclr.h> using namespace System; using namespace System::IO; int main(void){ //ファイルオープン StreamReader^ sr = gcnew StreamReader("hoge.txt"); int x, y; //ファイルの内容読込み for(y=0;;y++){ String^ line = sr->ReadLine();   //データ末尾まで読み込んだらループ終了 if(line == nullptr) break;    array<array<String^>^>^ table = gcnew array<array<String^>^>(line->Length);    int X=line->Length;    int Y=table->Length; //読み込んだ内容を表示 Console::WriteLine("<System::String表示>" + table);    //System::Stringのline内容(文字列)をコピーする変数   char tmp[128];   sprintf(tmp,"%s",line);   printf_s("<char配列> tmp=%s\n",tmp); //char*変数の内容表示   array<array<int>^>^ data= gcnew array<array<int>^>(Y,X); //int型に変換 for(x=0;x<X;x++){data[y][x]=atoi(table[y][x]);} } //ファイルのクローズ sr->Close();      return 0; } 汚く読みづらいコードで大変申し訳ありません。。 元はdefineで成分数(X,Y)を宣言していました。 しかし、読み込むファイル内容によって変えたく、arrayでLengthを使いました。 またこれ以外でいい方法がございましたら、どうか教えてください。 どなたか、ご教授よろしくお願いします。

  • vc2005.net 構造体の宣言について

    visual studio(VC++) .net 2005マネージを練習しています。 クラス内に構造体を作りたいのですがうまくいきません。 public ref class Form1:~~ 上記クラス中で public: static STRUCTTEST test = gcnew STRUCTTEST; 上記のように記述したところ '^'この間接指定を型STRUCTTESTで使用出来ません。 と出ます。 マネージだから'^'で良いのではないでしょうか。 ^をとったところ testをマネージtestslusion::Form1のメンバとして定義できません。混合型はサポートされません。 と出ます。マネージでは^が必要なはずなのでこれは分かるのですが・・・。 gcnewをnewにしても^を*にしてもコンパイルエラーが出ます。 これは何故でしょうか。 ちなみに配列を static array<int>^ test = gcnew array<int>(10); このように記述する事は分かりました。

  • 文字列や文字から整数への変換方法について

    文字列(string型)や文字(char型)から整数(int型など)に変換 する時のルールがよく解りません。 たとえば、キーボードから数字を打って、その入力された数字を 数値として整数型の変数に代入したい場合のやり方です。 ちょっとプログラムを作ってみました。 using System; class clmain { private static void Main() { Console.Write("1桁の整数を入れてね "); char ch = char.Parse(Console.ReadLine()); int by1 = (int)ch; int by2 = (int)char.GetNumericValue(ch); Console.WriteLine("by1 = {0}, by2 = {1}, ch = {1}", by1, by2, ch); Console.Write("整数を入れてね "); string st = Console.ReadLine(); /* by = (int)st; コンパイルエラー */ int by3 = int.Parse(st); Console.WriteLine("by3 = {0}, st = {1}", by3, st); } } まず、char型からint型への変換では、  int型変数=(int)char型変数; はコンパイルは通りますけど、実行すると全く違った値が入って しまいます。たとえば char型変数の値が "1" だと、int型変数には 49 が入ります。 int型変数 = (int)char.GetNumericValue(char型変数); と書いてようやく、思い通りの動きをしてくれます。 また、string型からint型への変換では  int型変数=(int)string型変数; はコンパイルエラーになります。 int型変数 = int.Parse(string型変数); とするとコンパイル出来て正しく動きます。 これで質問ですけど、 (1)なぜstring型とchar型で、int型への変換方法が違うのでしょうか? (2)int型変数=(int)char型変数; とすると、上に書いたように、全く 違った(希望しない)値が代入されてしまいます。これは、どういう 動きをしているのでしょうか? また、これはコンパイルエラーに なりませんけど、どういう時にこの書き方をするのでしょうか? 解る方、お願いします。

  • System::String->char*変換でき

    C++/CLIでSystem::String^文字列をchar*に変換する関数を書いたのですが、256文字以上の文字列を投げて、戻値を確認してみると、255文字分しか中身が詰まっていません。元の文字列はアスキーのみです。 #include <msclr/marshal.h> using namespace msclr::interop; static char* toPtChar(const String^ Text) { String^ temp = (String^)Text; msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context(); char* res = ((char*)(context->marshal_as<const char*>(temp))); return res; } この関数をどのように変更すれば、256文字以上変換できるでしょうか?

  • 構造体自体をポインターに入れたい

    構造体自体をポインターに入れて、ポーンターから位置をずらして表示したいものを表示したいのですが、どのようにすればよろしいでしょうか? 現在下記のように書いていますがコンパイルエラーになります static struct{  char *name; //名前  int num; //数 }eat1[5] = {  { "もも" , 1 },  { "みかん" , 2 }, }; static struct{  char *name; //名前  int num; //数 }eat2[5] = {  { "きゅうり" , 1 },  { "ナス" , 2 }, };  int *p;  int mode = 2;  int i;  switch( mode ){   case 1: p = &eat1;   case 2: p = &eat2;  }  for( i = 0; i < 5; i++; ){   printf( "%d\n" , p[i].num );  } エラー内容 c:\documents and settings\user\my documents\visual studio 2008\projects\kami\bese_item_cd.cpp(303) : error C2440: '=' : '<unnamed-type-eat1> (*)[5]' から 'int *' に変換できません。 指示された型は関連がありません。変換には reinterpret_cast、C スタイル キャストまたは関数スタイルのキャストが必要です。 c:\documents and settings\user\my documents\visual studio 2008\projects\kami\bese_item_cd.cpp(304) : error C2440: '=' : '<unnamed-type-eat2> (*)[5]' から 'int *' に変換できません。 指示された型は関連がありません。変換には reinterpret_cast、C スタイル キャストまたは関数スタイルのキャストが必要です。 c:\documents and settings\user\my documents\visual studio 2008\projects\kami\bese_item_cd.cpp(307) : error C2059: 構文エラー : ';' c:\documents and settings\user\my documents\visual studio 2008\projects\kami\bese_item_cd.cpp(308) : error C2228: '.num' の左側はクラス、構造体、共用体でなければなりません 型は 'int' です。

  • VCでストップウォッチを作りたいのですが、うまくいきません。

    こんにちは。私は30代の男性です。 「Visual C++ の初歩(http://www.gulf.or.jp/~damayan/vc/visualc2.htm)」というサイトで、ストップウォッチプロジェクトファイルがダウンロードできたので、コピペしてコンパイルしようと思ったのですが、うまくいきませんでした。 いつも下記のようなエラーメッセージが表示されます。ヘッダーファイルに「stdafx.h」は置いてあるのですが、やり方が間違っているのでしょうか? ちなみに、ダウンロードした「release」ファイルの中のexeファイルを起動すれば、実行はされます。「自分が新規でプロジェクトファイルを作り、ダウンロードしたソースをコピーしてコンパイルを試みても、うまくいかない。」という話です。 よきアドバイスを頂ければと思います。 宜しくお願い致します。 ----- ビルド開始: プロジェクト: abc, 構成: Debug Win32 ------ コンパイルしています... abc.cpp c:\documents and settings\my documents\visual studio 2005\projects\abc\abc\abc.cpp(111) : fatal error C1010: プリコンパイル ヘッダーを検索中に不明な EOF が見つかりました。'#include "stdafx.h"' をソースに追加しましたか? ビルドログは "file://c:\Documents and Settings\My Documents\Visual Studio 2005\Projects\abc\abc\Debug\BuildLog.htm" に保存されました。 abc - エラー 1、警告 0 ========== ビルド: 0 正常終了、1 失敗、0 更新、0 スキップ ==========

専門家に質問してみよう