• ベストアンサー

cygwinとUnixのコンパイルの違い。

こんにちは。 UNIXのソースをWindows上でビルドできるようにしています。 Windows上では、cygwinのコンパイラを使用しています。 下記コードがコンパイルが通らず悩んでいます。 vector<TestCls>:: iterator it = &array[2] ; iteratorのitに、arrayの動的配列2番目のアドレスを参照渡し?しています。 UNIXでは、ビルドと動作は確認できたのですが、 CYGWINでは、ビルドができませんでした。 ビルドオプションなど、足りないものがあるのでしょうか? 下記サンプルソースを置きます。 ##################################### ☆☆☆TestCls.h☆☆☆ #ifndef TESTCLS_H_ #define TESTCLS_H_ class TestCls { private: int age ; public : TestCls() ; virtual ~TestCls() ; void setAge(int ax) { age = ax ;} int getAge() { return age ; } } ; #endif /*TESTCLS_H_*/ ##################################### ☆☆☆TestCls.cpp☆☆☆ #include "TestCls.h" TestCls::TestCls() {} TestCls::~TestCls() {} ##################################### ☆☆☆testmain.cpp☆☆☆ #include<vector> #include<iostream> #include "TestCls.h" int main() { using namespace std ; vector<TestCls> array ; int i; for(i=0; i<10; i++) { TestCls clstestCls ; clstestCls.setAge(i+10) ; array.push_back(clstestCls) ; } vector<TestCls>::iterator it = &array[2] ; // ←ここでエラー発生。 while(it != array.end()) { cout << it->getAge() << endl ; ++it ; } return 0 ; } ##################################### ビルドオプションは、 g++ -I"c:\~~\inc" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testmain.d" -MT"src/testmain.d" -o"src/testmain.o" "../src/testmain.cpp" エラーメッセージは、 error: conversion from `TestCls*' to non-scalar type `__gnu_cxx::__normal_iterator<TestCls*, std::vector<TestCls, std::allocator<TestCls> > >' requested

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

  • ベストアンサー
  • rabbit_cat
  • ベストアンサー率40% (829/2062)
回答No.2

基本的に、vectorのイテレータに生ポインタを代入する、なんていう無茶なことをしてはいけません。 そのUnixのコンパイラで使ってるSTLが、「たまたま」、vectorのイテレータとして、生ポインタのtypedefを使うという実装をしていたのでしょう。 UNIX、Windowsの問題ではないです。STLライブラリ(とコンパイラ)の違いです。 vector<TestCls>::iterator it = array.begin(); it += 2; とかやるんでしょうね。

sakuody
質問者

お礼

rabbit_catさん 回答ありがとうございます。 STLライブラリがあるとは、知りませんでした。 UNIXのソースを極力いじりたくない意向があったのですが、 デバッグオプションでビルド時に切り替える事にします。 貴重な教示ありがとうございました。

その他の回答 (2)

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.3

array.begin()+2 でいいような気もします>#2.

sakuody
質問者

お礼

Tacosanさん 回答ありがとうございます。 少ない行で対応したいので、array.begin()+2を採用させて頂きます。 多謝

  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.1

エラーになる方が正常だと思いますが。

sakuody
質問者

お礼

回答ありがとうございます。 なぜ、エラーになるのでしょうか? UNIXでは、正常にビルドと、動作は確認できています。 何が違うのでしょうか?

関連するQ&A

  • ベクターの初歩について

    ベクターについて勉強し始めた所です。ベクターについて解らない事があるので教えて下さい。 今ベクターに10個数字を登録します。 後で3か5があれば取り除きます。 #include <vector> #include <iostream> int main(){   using namespace std;   vector<int> array1;   for( int i = 0; i < 10; ++i )     array1.push_back( i );   vector<int>::iterator it;   for(it = array1.begin(); it != array1.end(); ){     if(*it == 3 || *it==5)       it = array1.erase(it);     else       ++it;   }   for( it = array1.begin(); it != array1.end(); ++it )     cout << *it << endl;   return 0; } 一応出来たんですが、これが構造体だったらどうしたらいいのでしょう? typedef struct{   int x,y; }xy_t; vector<xy_t> array1; だとして、最初にx,yのそれぞれ10個に適当な値を入れておき、xが3か5ならそれを削除するにはどうしたらいいのでしょうか。 また、一つずつ削除する方法と、remove関数で出来そうな気がするので、もし一括で出来る方法があればそちらも2種類お願いします。 (構造体を用いたベクターの使い方が書いてある参考サイトでも結構です) XP Pro VS2005Pro よろしくお願いします。

  • 外部ファイルでのtemplate関数の実装方法

    外部ファイルでのtemplate関数の実装方法 sub.cppにtemplate関数を実装し、 main.cppで、sub.cppのtemplate関数を呼び出す、 みたいなことをやりたいのですが、 コンパイルは通りますが、リンクエラー?になってしまいます。 以下が上記のサンプルプログラムです。 //main.cpp #include <vector> #include <iostream> using namespace std; template <typename t_ret, typename t_array> t_ret sub_t(const t_array&); int main (int argc, char *argv[]) { vector<double> v; v.push_back(1.1); v.push_back(2.2); cout << sub_t<double, vector<double> >(v) << endl; return 0; } // sub.cpp #include <vector> using namespace std; template <typename t_ret, typename t_array> t_ret sub_t(const t_array& array) { return array[0]+array[1]; } //コンパイルログ main.o: In function `main': main.cpp:(.text+0x134): undefined reference to `double sub_t<double, std::vector<double, std::allocator<double> > >(std::vector<double, std::allocator<double> > const&)' collect2: ld returned 1 exit status make: *** [main] Error 1 どなたか、対処方法を教えてください。 宜しくお願いします。

  • vectorの中にmap

    vectorの中にmapを入れて 添字:ノードID [どのノードから来たのか|それまでのコスト] を表現しようと考えています. #include<iostream> #include<vector> #include<map> #include<list> // MACROS #define UNDEF -1 // PROTOTYPE DCLARE void init_path(std::vector<std::map<char, int> >, int size); int main(void) { //source -> source node // //size -> the number of node // //path -> store path infomation // ex: // path[1]: 1 is node id // char : from node id // cost : how cost from source to here // //adj -> show adjacency list eace node int source; int size; std::vector<std::map<char, int> > path; std::vector<std::list<char> > adj; size = 5; std::cout << "before" << std::endl; init_path(path, size); std::cout << "after" << std::endl; std::map<char, int>::iterator it; for(int i = 0; i < size; i++) { it = path[i].begin(); // std::cout << it->first << ":" << it->second << std::endl; } return 0; } void init_path(std::vector<std::map<char, int> > path, int size) { std::map<char, int> init; init.insert( std::map<char, int>::value_type('-', UNDEF) ); for(int i = 0; i < size ; i++) { path.push_back(init); } return; } *結果 before after Segmentation fault となり初期化をする所までは正常に動いたっぽいのですが どこが悪いのかわかりません(おそらくイテレータあたりかと思うのですが・・・・ 具体的にどうしたらいいのか分からないのでご指導ねがいます.

  • gccでコンパイル時のエラー

    // test.cpp #include <stdio> using namespace std; int main(int argc, char *argv[]) { printf("test\n"); return 0; } 上記コードをコンパイルしたいのですが make -k g++ -g -O2 -Wall -I. -c test.cpp -o test.obj test.cpp:2:17: stdio: No such file or directory test.cpp: In function `int main(int, char**)': test.cpp:7: error: `printf' was not declared in this scope test.cpp:7: warning: unused variable 'printf' make: *** [test.obj] Error 1 make: Target `all' not remade because of errors. となります。 #include <stdio> using namespace std; を #include <stdio.h> // using namespace std; にすれば正常に終了するのですがなぜでしょうか? gccはMingw5.1.6からインストールしたもので、 バージョンはgcc3.4.5です。 Meadow上から実行しました。 回答よろしくお願い致します。

  • C++ VS2005におけるイテレーターの動作

    過去にVisual Studio C++ 6.0 で作成したプロジェクトを、Visual Studio C++ 2005に変換し、ビルドしてみたところ、イテレーターに関する操作をしているところでエラーが出ました。 ---------- ソースの一部抜粋 ここから ------------- vector<char> array; // int型の動的配列 arrayに要素をpush_backする処理 while( it && it != array.end() ) // 末尾要素まで { cout << *it << endl; // *演算子で間接参照 ++it; // イテレータを1つ進める } ---------- ソースの一部抜粋 ここまで ------------- 上記のwile文のところで以下のようなエラーが発生します。 ---------- エラーメッセージ ここから ------------- error C2676: 二項演算子 '&&' : 'std::_Vector_iterator<_Ty,_Alloc>' は、この演算子または定義済の演算子に適切な型への変換の定義を行いません。(新しい動作; ヘルプを参照) 1> with 1> [ 1> _Ty=char, 1> _Alloc=std::allocator<char> 1> ] ---------- エラーメッセージ ここまで ------------- とりあえず、"while( it && it != array.end() ) "を"while( *it!=NULL && it != array.end() ) "とするとエラーはでなくなりましたが、なぜもとのように書いてはダメなのかがさっぱりわかりません。 そもそも、itと*itでは、示す内容が違っているので、これでは解決になっていないのでは、という気もします。 ヘルプを見ても、VS6.0とVS2005でvectorやiteratorの動作が変わっていることが読み取れず、困っています。 どなたかご教授ください。

  • CygwinでSTLの勉強をしていますが・・・

    今C++のSTLの勉強をしています。 本に載っているサンプルプログラムを打って実行しようとしたら エラーがでてしまいました。 エラーの内容はprintとtotalが見つかりませんというエラーです。 コンパイラはcygwinを使ってます。 よろしくお願いします。 /*for_each()アルゴリズム*/ #include<iostream> #include<algorithm> #include<vector> #include<functional> #include<> using namespace std; int main() { int n[]={100,200,300,400,500,600}; int size=sizeof n/sizeof(int),i; vector<int> v; for(i=0;i<size;++i) v.push_back(n[i]); for_each(v.begin(),v.end(),print<int>()); cout<<endl; cout<<(for_each(v.begin(),v.end(),total<int>())).gettotal()<<endl; return 0; }

  • C++ vectorのbeginについて

    VC++2010にて下記コードのビルドは通るのですが、 vVecotrInt, vVectorIntPtrの要素数0のとき、iptr代入時に Debug assertaion Failed! vector iterator not dereferencableとなります。 そもそもbegin()はイテレータなので、ポインタに代入しようとしていることが間違いかと思うのですが。 質問1.int* iptr = ~ ではなく、std:vector<int>::iteratorとすれば、     要素数が0でもエラーがおきません。この違いは何でしょうか? 質問2.そもそもイテレータをポインタに代入して何か得することがあるんでしょうか?     ただイテレータとポインタは同じようなものだと思って、コーディングしてるだけなんでしょうか・・・ コード: // vVectorIntの要素を間接参照して(参照先はint)、そのアドレスをポインタに格納 std::vector<int> vVectorInt; int* iptr = &*vVectorInt.begin(); // ここでvector iterator not dereferencable // vVectorIntPtrの要素を間接参照して(参照先はint*)、ポインタに格納 std::vector<int*> vVectorIntPtr; int* iptr = *vVectorIntPtr.begin(); // ここでvector iterator not dereferencable // vVecotrIntPtr2の要素数0のときでも、イテレータを使えば問題ない std::vector<int*> vVectorIntPtr2; std::vector<int*>::iterator itr = vVectorIntPtr2.begin();

  • 6行ソースをコンパイル可能にしてください

    std::fflush(stdout); という所が間違っているんだと思いますが#include <iostream>は#include <iostream.h>にせずにコンパイルしたいです。 #include <iostream> int main(){ std::cout << "h\n"; std::fflush(stdout); return 0; }

  • プログラミング

    以下のC++で書かれた以下のプログラムのコンパイルができません。理由を教えてください。 #include <iostream> #include <vector> #include <algorithm> istream& read(istream&, std::vector<double>&); double median(std::vector<double>); int main(){ std::vector<double> a; read(std::cin, a); std::vector<double>::iterator itr; while(itr != a.end()){ std::cout << *itr; } std::cout << median(a) << std::endl; } istream& read(istream& is, std::vector<double>& v){ double b; if(is){ while(in >> b) v.push_back(b); } return is; } double median(std::vector<double> a){ int i; if(a.size() == 0) { return -1; } i = a.size() / 2; if(a.size()%2 == 1) return (a[i]); else return ((a[i] + a[i-1])/2); }

  • C++のファイルの分割について教えてください。

    分割したプログラムの書き方を練習中なのですが、 下のようなプログラムを書いて、ビルドしようとしたら失敗します。 どこが悪いのか、教えていただけないでしょうか? (5)\test30\Debug\test30.exe : fatal error LNK1120: 外部参照 1 が未解決です。 1>test30 - エラー 2、警告 0 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== //test30.cpp #include <iostream> #include "sub.h" int main(void) { std::cout<<"メインプログラムです。"<<std::endl; sub1(); sub2(); return 0; } ------------------------------------------------------------- //sub1.cpp #include <iostream> void sub1(void) { std::cout<<"サブ1です。"<<std::endl; } ------------------------------------------------------------- //sub2.cpp #include <iostream> void sub1(void) { std::cout<<"サブ2です。"<<std::endl; } --------------------------------------------------------------- //sub.h void sub1(void); void sub2(void);