2010-12-07 6 views
0

vector<vector<clickable> >という種類のベクターに問題があります。コンパイラは、push_backが変数の宣言と同じ関数で行われている限り、その上にvector<clickable>をプッシュバックしても問題ないと思われますが、変数が.hファイルで宣言され、push_backがクラスの別の関数で実行されます。構造体の2Dベクトルを正しくプッシュする際に問題が発生する(C++)

次の例では、同じ関数で宣言されたばかりのvector<vector<clickable> >でpush_backを呼び出し、.hファイルで宣言されたpush_backを呼び出す点を除き、ループはまったく同じにする必要があります。どのような作品の

例(これは、main関数からですが、それは任意の関数で動作します。):

vector<vector<clickable> > clicks; 
for(int i = 0; i < 10; i++){ 
    vector<clickable> click; 
    for(int j = 0; j < 10; j++){ 
      click.push_back(clickable(Rect(Point(50,50),5,10),"blar")); 
    } 
    clicks.push_back(click); 
} 

動作しないものの例:

Gui.h:

#include <vector> 
//... 
struct clickable { 
    Rect rect; 
    string msg; 
    bool visible; 
    clickable(Rect rectangle, string message){ 
      rect = rectangle; 
      msg = message; 
      visible = true; 
    } 
}; 
//... 
class Gui{ 
    public: 
    //... 
    void load_environment(); 
    //... 
    private: 
    vector<vector<clickable> > ship; 
    //... 
} 

Gui.cpp:

#include "Gui.h" 
//... 
void Gui::load_environment(){ 
    for(int i = 0; i < 10; i++){ 
      vector<clickable> click; 
      for(int j = 0; j < 10; j++){ 
        click.push_back(clickable(Rect(Point(50,50),5,10), 
              "blar")); 
      } 
      ship.push_back(click); 
    } 
} 
//... 

私はそれが何人かの演算子に過負荷をかけることと何か関係があるかもしれないと思うが、それが問題の原因であるかどうか、あるいはもしあれば私が過負荷をかけなければならないことは、

編集 はここでエラーのテキストです:

Gui.cpp:47: error: no matching function for call to ‘std::vector<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::push_back(std::vector<clickable, std::allocator<clickable> >&)’ /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Alloc = std::allocator<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]

+0

どのようなエラーが表示されますか? – EboMike

答えて

2

エラーメッセージと言い、実際には、それが実際にvector<vector<string> >である上に、あなたがpush_backにしようとしているベクトルを考えています、期待通りにvector<vector<clickable> >ではなく

おそらくクリーンな再構築が必要です。

+0

うわー、私は本当に馬鹿だった。私は 'vector >'を 'ship'とも呼んでいました(あまりにも多くの情報を避けるために貼り付けたコードから省略しました)。まだ何も使っていないので、忘れてしまったと思います。悲しいことに、私は〜24時間このことについて困惑しています。 – scott77777

関連する問題