• 締切済み

C++ の map についてです

C++の初心者です。よろしくお願いします。 DirectXを使っていて簡単なゲームを作っていまして、 mapを使っていると、このようなエラーが出てしまいどうしても理由わかりませんでした。 class D2DMap : public GameTexture{ protected: GameTexture** CopyTexture; int MaxMass; int WidthMass; int HeightMass; int WidthMassPixel; int HeightMassPixel; int MassInfo[30][30]; public: D2DMap(); virtual ~D2DMap(); HRESULT LoadMapTexture(int widthmass, int widthmasspixel,int heightmass, int heightmasspixel, const char* FileName, D3DCOLOR color); HRESULT LoadTextMass(const char* FileName); int GetWidthMassPixel(){ return WidthMassPixel;} int GetWidthMassNum(){ return WidthMass;} int GetHeightMassPixel(){ return HeightMassPixel;} int GetHeightMassNum(){ return HeightMass;} int GetMassInfo(int x, int y){ return MassInfo[y][x];} void DrawMap(D3DCOLOR color); void DrawCopyMap(D3DCOLOR color); void SetCopyTex(GameTexture* copy){ CopyTexture= &copy;} void Delete(){} }; map<string , D2DMap*> MapBox; と定義して string Name="--------"; D2DMap map; MapBox.insert( map<string, D2DMap*>::value_type(Name, &map)); としたとこと error C2275: 'std::string' : この型は演算子として使用できません 'std::string' の宣言を確認してください。 error C2059: 構文エラー : '>' error C2039: 'value_type' : '`global namespace'' のメンバではありません というエラーが出てきました。 mapのfind や iterator は可能なのですがinsertの場合エラー となり、理由が全く分かりません。詳しい方アドバイスをお願いしたい のですが、よろしくお願いします。 VC++2008 を使っています。

みんなの回答

  • Yanch
  • ベストアンサー率50% (114/225)
回答No.1

> MapBox.insert( map<string, D2DMap*>::value_type(Name, &map)); の行ですが、コンパイラはmapを変数名として解釈してしまっているように見えますよ。 データ型のmap(std::map)と、変数名のmap(D2DMap map) に同じ名前を使ってるのがよろしくないかもしれませんね。

kaijinu-un
質問者

お礼

ありがとうございました。 まだ試していませんが、早速修正してみたいと思います。

kaijinu-un
質問者

補足

無事解決しました。 名前がmapとかっぶていました。 初歩的なミス…。しかし、気づかなかったので本当にありがとうございました。

関連するQ&A

  • C++について。

    現在”猫でもわかるプログラミング”のC++編をSDKと共に勉強している身です。 現在第22章、第23章を勉強中です。 22章 http://www.kumei.ne.jp/c_lang/cpp/cpp_22.htm 23章 http://www.kumei.ne.jp/c_lang/cpp/cpp_23.htm 作業環境はVisual Studio 2005.net C++です 22章でのプログラムを作成し、実行した結果エラーが出てしまいました。 ソースです #include <iostream> class xy_position { int x; int y; public: xy_position(int x = 0, int y = 0){ xy_position::x = x; xy_position::y = y; } int X() const {return x;} int Y() const {return y;} }; ostream& operator << (ostream& o, const xy_position& p) { return o << "(" << p.X() << "," << p.Y() << ")"; } int main(void) { xy_position a(50, 60), b; std::cout << a << b << std::endl; return 0; } (17):error C2143: 構文エラー : ';' が '&' の前にありません。 (17):error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません (17):error C2065: 'o' : 定義されていない識別子です。 (17):error C2059: 構文エラー : 'const' (18):error C2143: 構文エラー : ';' が '{' の前にありません。 (18):error C2447: '{' : 対応する関数ヘッダーがありません (旧形式の仮引数リスト?) (26):error C2679: 二項演算子 '<<' : 型 'xy_position' の右オペランドを扱う演算子が見つかりません (または変換できません)。 このようなエラーが出てしまいました。 もちろんソースは全て同様に書いています。 この”猫でも”で使用しているコンパイラと異なるために出たエラーでしょうか? それに単に cout << のように記述するとエラーが出てしまい、 std::cout << のように記述しなければ通りません。 また、エラーとは別の質問になってしまいますが、プログラム中に int X() const {return x;} という記述がありますが、このconstの意味が分かりません。 単純に return x が変更不可能という意味でしょうか? 次に23章についての質問です。 ここでもソースは同じなのに以下のようなエラーが出てしまいます。 ソースです。 #include <iostream> int main(void) { int x=10, y=15, z=20; std::cout << "16進表示" << std::endl; std::cout.setf(ios::hex); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; std::cout.unsetf(iostream::hex); std::cout << "8進数" << std::endl; std::cout.setf(ios::oct); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; return 0; } (8):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (8):error C2065: 'hex' : 定義されていない識別子です。 (13):error C2653: 'iostream' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2065: 'oct' : 定義されていない識別子です。 これも何か設定をしなければいけないのでしょうか? なにぶんC++は・・・というかオブジェクト指向の言語は初心者なもので疑問も多いですorz

  • マップとアルゴリズム

    またまた質問です。 以下の処理をさせるとエラーが出ます。 どなたか見ていただけますか? 環境はWindows XP, Visual Studio.NETです。 -----処理部分------------------ map<char, int> m; for(i=0; i<10; i++){  m.insert(pair<char, int>('A'+i, i)); } reverse(m.begin(), m.end()); map<char, int>::iterator p; p = m.begin(); while( p != m.end() ){  cout << p->first << " " << p->second << endl;  p++; } ------以下エラーログ--------------- C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(16) : error C2166: 左辺値は const オブジェクトに指定されています。 C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(46) : コンパイルされたクラスのテンプレートのインスタンス化 'void std::swap<const _Ty1>(const _Ty &,const _Ty &)' の参照を確認してください with [ _Ty1=int, _Ty=int ] C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(45): クラス テンプレートのメンバ関数 'void std::pair<_Ty1,_Ty2>::swap(std::pair<_Ty1,_Ty2>::_Myt &)' のコンパイル中 with [ _Ty1=const int, _Ty2=int ] ClusteringMain.cpp(45) : コンパイルされたクラスのテンプレートのインスタンス化 'std::pair<_Ty1,_Ty2>' の参照を確認してください with [ _Ty1=const int, _Ty2=int ] C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(16) : error C2166: 左辺値は const オブジェクトに指定されています。

  • C++

    以下にソースを提示します。少々長いことをお許しください。 #include <iostream> #include <string> using namespace std; class Aru{ private: int data; public: Aru(int d): data(d){}; int get_data() const { return data; }; }; class Betu{ private: Aru *a; public: Betu(); ~Betu(); Betu (int x); void input(); void show() const; int get_data() const { return a->get_data(); } }; Betu::Betu(int xv):a(0){ a = new Aru(xv); } Betu operator+(Betu& aa, const Betu& bb){ int data = aa.get_data() + bb.get_data(); Betu temp(data); return temp; } Betu::Betu():a(0){} Betu::~Betu(){ delete a; } void Betu::input(){ int d; delete a; cout << "数字を入力してください" << endl; cin >> d; a = new Aru(d); } void Betu::show() const{ if(a==0) return ; cout << "データ:" << get_data() << endl; } int main() { Betu one; Betu two; Betu three; one.input(); two.input(); three = one + two; three.show(); } コンパイルは普通に警告も出ずに通ります。 しかし実行時にエラー・・・・・ まだC++学習のみでエラーを取ることができません。 おそらくoperatorの部分が影響していると思うのですが・・・ ちなみにこのプログラムはoperatorの練習のために作ったプログラムのため、意味のないものになっていますが気にしないでください。 よろしくお願いします。

  • std::mapのメモリアロケータについて

    こんにちは。 質問させてください。 現在VisualStudio2008 Academic EditionでC++のプログラムを書いています。 vectorで使用可能なアロケータを作ったのですが、mapでそのアロケータを利用する方法がわかりません。 // vectorで自作アロケータを利用 std::vector< char, my_allocator<char> > myVec; // mapで自作アロケータを利用 std::map< int, float, std::less< int >, my_allocator< std::pair< int, float > > > myMap; vectorの場合はコンパイルできるのですがmapの場合は error C2664: 'my_allocator<_type>::allocator(const my_allocator<_type> &)' : 1 番目の引数を 'my_allocator<_type>' から 'const my_allocator<_type> &' に変換できません。 というエラーが出てコンパイルできません。 ご教授願います。

  • C++の問題で・・

    C++の問題で・・ 参考書に「簡易的な文字列クラスStringを作成せよ。」という問題があり作りました。 いかにそのコードを示します。今回の質問の内容に関係ないとおもうところや、インクルードなどは省かせていただきます。 環境は Visual studio 2008です。OSはXPです。 class String{     int len; //文字列の長さ     char *s; //文字列の先頭文字へのポインタ public:     String(const char *);     int length()const{return len;} //長さを求める     operator const char * ()const{return s;}     bool operator==(String &a)const{return strcmp(this->s, a);}     char *operator+(String&)const; }; char * String::operator +(String &a)const {     char *memory = new char[this->len + a.len + 1];     memory[0] = '\0';     return strcat(strcat(memory, this->s), a); } String::String(const char *p): s(const_cast<char *>(p)), len(strlen(p)){} String::String(const String &x) {     s = x.s;     len = x.len; } inline std::ostream& operator<<(std::ostream &s, String &x) {     return s << static_cast<const char *>(x); } int main() {     String a("My name is Paul");     String b("My name is Paul");     String c("My name");     String d(" is Paul");     cout << "a = " << a << "\n";     cout << "b = " << b << "\n";     cout << "c = " << c << "\n";     cout << "d = " << d << "\n";     cout << "a == b " << (a == b) << "\n";     cout << "a == c " << (a == c) << "\n";     cout << "c + d = " << (c + d) << "\n"; }     このようなプログラムなのですが、上記の char * String::operator +(String &a)const {     char *memory = new char[this->len + a.len + 1];     memory[0] = '\0';     return strcat(strcat(memory, this->s), a); } ところで、 memory[0] = '\0'; を除くと文字列を出力した結果をみると、先頭にいらない言葉が入っています。 僕の場合は x9My name is Paul と表示されます。文字化け・・ではないのですが、ゴミのようなものが・・ どうしてこのようなことが起こるか、どこでゴミが入ってしまうのか教えてほしいです。 稚拙なプログラムで申し訳ないです。 もし、間違っている場所や、もっと簡単にかけるようなところがあれば、ご指摘いただくとありがたいです。 よろしくお願いします!

  • C++初心者です

    C++初心者です どうも行き詰ってしまったのでまた投稿させていただきました。 文字数が足りないため前回の投稿から何を作ろうとしているか見ていただけると嬉しいです。 http://okwave.jp/qa/q5993632.html どうか間違っているところご指摘お願いできないでしょうか。 最後の部分は根本的にできていないので助けていただけると幸いです #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <map> using namespace std; class Cards { string Name; string Explain; public: Cards(const string e):Explain(e){} const string GetExplain() const {return Explain;} }; class Game { public: static const int KindsOfCards = 4; virtual Cards* SetCard(); }; Cards* Game::SetCard() { switch(rand() % KindsOfCards) { case 1: return new Square; case 2: return new Half; case 3: return new Reverse; case 4: return new Plus; } } class Square : public Cards { public: Square() : Cards("「二乗」 (二乗する)"){} virtual void Excute(); }; class Half : public Cards { public: Half() : Cards("「半分」 (2分の1にする)"){} virtual void Excute(); }; class Reverse : public Cards { public: Reverse() : Cards("「反転」 (符号を反転する)"){} virtual void Excute(); }; class Plus : public Cards { public: Plus() : Cards("「Plus17」", "(17を加える)"){} virtual void Excute(); }; void Square::Excute() { Now[i+1] = Now[i]*Now[i]; return Now[i+1]; } void Half::Excute() { Now[i+1] = Now[i]/2; return Now[i+1]; } void Reverse::Excute() { Now[i+1] = Now[i]*(-1); return Now[i+1]; } void Plus::Excute() { Now[i+1] = Now[i]+(17); return Now[i+1]; } int main() { int Level; Level = 3; srand((unsigned) time(NULL)); map<int,int> Target; for(int i = 0; i <= Level; i++) { Target[i]; } int Now; int T(int t) { public: Cards* c = SetCard(); Target[0](int t0){t0 = rand() % 100;} Target[0] = Now; Target[1](int t1){t1 = c->Excute();} target[2](int t2){t2 = c->Excute();} Target[3](int t3){t3 = c->Excute();} target[3] = t; } cout << "●所有しているカード●" << endl; for(int i = 1; i <= Level; i++) { cout << i << ":" << c->GetExplain() << endl; } cout << "目 標 値:" << T << endl; cout << "現在の数値:" << Now << endl; }

  • shared_ptr クラスについて

    shared_ptrクラスを使いたいのですが、使えません、どうしてでしょうか?ソースはこれです。 #include<iostream> #include <string> #include <fstream> #include<memory> using namespace std; class SMonster{ string name; int power; public: SMonster(); SMonster(int p); ~SMonster(){ }; void SetPower(int p); int GetPower(SMonster& t)const; void walk(const string& str); int GetPoint(void)const; }; class B {}; class D : public B {}; int main(void) { shared_ptr<D> sp0(new D); SMonster m(200); SMonster n(100); std::cout<<m.GetPower(m)<<std::endl; std::cout<<n.GetPower(n)<<std::endl; ShowWindow(10); }

  • MingwでC++のソースがコンパイルできない。

    質問があるのですがよろしくお願いしします。 プラットフォームは Windows HOME です。 MinGWでC++のhelloworld.cppという名前のプログラム ------ここから----------- #include <iostream> using namespace std; int main(){ cout <<"Hello World!!\n"; return 0; } -------ここまで----------- を、gcc helloworld.cpp とWindowsのコマンドプロンプトから打ってコンパイルしようとしたのですが、 undefined reference to std string::size() const undefined reference to std string::operator[](using int) const undefinrd reference to std::cout undefinrd reference to std::basic_ostream<char, std::char traits<char> >&, char const*) undefinrd reference to std::ios_base::Init::() undefinrd reference to std::ios_base::~Init::() などのエラーが出て、コンパイルできません・・ hello.cという名前のC言語プログラム --------ここから------ #include<stdio.h> int main(void){ printf("Hello GCC World on Windows!!\n"); } ------ここまで------- は、gcc hello.c で正常にコンパイルでき、生成したexeファイルも正常に動きます。 また、eclipse CDT上からc++プログラムをMinGWでコンパイルすることもできるのですが・・ なぜかWindowsのコマンドプロンプトからc++プログラムをコンパイルしようとするとエラーが出てしまいます。(C言語のプログラムでもエラーが出ることが時々あります。) どなたか詳しい方いらっしゃいましたらご教授頂けると幸いです。 よろしくお願いします。

  • C++のSTL mapを使用するとセグメンテーション違反となる

    こんにちは。C++でSTLのmapの簡単な使用テストを以下のプログラムで行っており、動作確認ができました。 /* Assoc_array.c */ #define MAIN // (#include省略) using namespace std; namespace Usefuls { class Assoc_array_str { private: map<string, string> _h; public: void set(string key, string val); string get(string key); }; void Assoc_array_str::set(string key, string val){ _h.insert(pair<string, string>(key, val)); } string Assoc_array_str::get(string key){ map<string, string>::iterator p; p = _h.find(key); return p->second; } } #ifdef MAIN int main(){ Usefuls::Assoc_array_str assoc; assoc.set("Konnichiwa", "Hello"); cout << assoc.get("Konnichiwa") << endl; return 0; } #endif /* ここまで */ しかし、これを以下のように他のファイルから呼び出すと(先頭の#define MAINを#undef MAINに変えます)、「セグメンテーション違反です」というメッセージが出てしまいます。 /* Aa_test1.c */ #include <iostream> using namespace std; namespace Usefuls { class Assoc_array_str { public: void set(string, string); string get(string); }; } int main(){ Usefuls::Assoc_array_str assoc; assoc.set("Konnichiwa", "Hello"); cout << assoc.get("Konnichiwa") << endl; return 0; } /* ここまで */ コンパイル方法は以下の通りです。 g++ -c Assoc_array.c g++ Aa_test1.c Assoc_array.o また、使用プラットホームはLinuxのCentOS 4.3です。 原因が分かる方、ご回答をよろしくお願いします。

  • C/C++関数間でのStringクラスの扱い

    以下のようなコードを実行してみましたが思い通りに動いてくれません. "sample"という文字列がstrへとコピーされると思ったのですが. stringクラスのc_str()メソッドはconst char*だと言っているので無理矢理キャストしたのが原因でしょうか.stringクラスは記憶領域を自動で変更してくれるのではないのですか.それともこの挙動は仕様ですか. -------- 以下コード -------- #include <iostream> #include <string> using namespace std; int func(char *); int main(void) {     string str("");     func((char *)str.c_str());     cout << "String: " << str << endl;     return EXIT_SUCCESS; } int func(char *buf) {     buf = "sample";     return 0; } -------- 以上コード --------

専門家に質問してみよう