-2

2Dベクトルのサイズを見つけることについて質問があります。私の簡単な質問は、3つの「形状」を格納するベクトルがあり、各形状にはランダムな数のヒットポイント(デカルト座標{x、y、z}として格納)があり、各ヒットポイントにはランダムな色(RGBとして格納)があるということです。各形状のヒットポイントの数を調べたい。私が行った場合:

VectorOfShapes.size() 

答えがわかります

3

これは、ここで前に尋ねられた同様の質問 です。しかし、それは私を助けませんでした。彼らの解決策は

VectorOfShapes[1].size()

それを試してみたところ、次のエラーが発生しました。「エラーC2228:「。size」の左側にはclass / struct/unionが必要 です」。これはうまく答えられました(リンクをたどってください)。それはまだ私を助けません(または私は愚かです)...何かアイデアはありますか?

//----------追加情報

class World {
public:
vector<Flux*>   VectorOfShapes;
void
containers(int obnum, Ray reflect, RGBColor Ll);
void                    
build(void);
void
add_vectorshape(Flux* vectorshape_ptr)
private:
void
delete_VectorOfShapes
}
inline void
World::add_vectorshape(Flux* vectorshape_ptr){
VectorOfShapes.push_back(vectorshape_ptr);
}

void
World::containers(int obnum, Ray reflect, RGBColor Ll)
{
vectorOfShapes[obnum]->push(reflect);
VectorOfShapes[obnum]->push(Ll);
int sizers = VectorOfShapes[0].size(); //this is where the code is giving me errors
}

void                                                
World::build(void) {
Flux* vectorshape_ptr1 = new Flux;
add_vectorshape(vectorshape_ptr1);
Flux* vectorshape_ptr2 = new Flux;
add_vectorshape(vectorshape_ptr2);
Flux* vectorshape_ptr3 = new Flux;
add_vectorshape(vectorshape_ptr3);
}

void
World::delete_VectorOfShapes(void) {
int num_VectorOfShapes = VectorOfShapes.size();

for (int j = 0; j < num_VectorOfShapes; j++) {
    delete VectorOfShapes[j];
    VectorOfShapes[j] = NULL;
}   

VectorOfShapes.erase (VectorOfShapes.begin(), VectorOfShapes.end());
}

    class Flux{

public:
std::vector<Ray> rays;
std::vector<RGBColor>    L;


void push(Ray ray); 
void push(RGBColor Ls) ;   


};
inline  void 
Flux::push(Ray ray) { 
rays.push_back(ray); 
}

inline void 
Flux::push(RGBColor Ls){ 
L.push_back(Ls); 
}   
4

1 に答える 1

1

の宣言を提供するコメントに基づくfluxs

vector<Flux*> fluxs;

fluxsはポインタのベクトルであり、次のことを意味します。

fluxs[i]

属性を設定するために投稿されたコードで行ったように、を使用する必要があるのFlux*ではなく、を返します。の定義がなければ、を取得するためのゲッターがあり、前述のように、には正確に3つの要素があると想定しているため、ヒットポイントの合計を取得するには次のようになります。Flux->Fluxhitpointsfluxsfluxs

int total_hitpoints = fluxs[0]->get_hitpoints() +
                      fluxs[1]->get_hitpoints() +
                      fluxs[2]->get_hitpoints();

編集:

ヒットポイントFlux::rays数が(またはFlux::L)の要素数である場合、次のようになります。

int total_hitpoints = fluxs[0]->rays.size() +
                      fluxs[1]->rays.size() +
                      fluxs[2]->rays.size();
于 2012-04-11T15:12:45.377 に答える