1

私はこの主な機能を持っています:

#ifndef MAIN_CPP
#define MAIN_CPP

#include "dsets.h"
using namespace std;

int main(){
DisjointSets s;
s.uptree.addelements(4);
for(int i=0; i<s.uptree.size(); i++)
        cout <<uptree.at(i) << endl;
return 0;
}

#endif

そして、次のクラス:

class DisjointSets
   { 
public:
void addelements(int x);
int find(int x);
void setunion(int x, int y);

private:
vector<int> uptree;

};

#endif

私の実装はこれです:

void DisjointSets::addelements(int x){
        for(int i=0; i<x; i++)
        uptree.push_back(-1);


}

//Given an int this function finds the root associated with that node.

int DisjointSets::find(int x){
//need path compression

if(uptree.at(x) < 0)
        return x;
else
        return find(uptree.at(x));
}

//This function reorders the uptree in order to represent the union of two
//subtrees
void DisjointSets::setunion(int x, int y){

}

main.cpp (g++ main.cpp) のコンパイル時

次のエラーが表示されます。

dsets.h: 関数内 \u2018int main()\u2019: dsets.h:25: エラー: \u2018std::vector > DisjointSets::uptree\u2019 はプライベートです

main.cpp:9: エラー: このコンテキスト内

main.cpp:9: エラー: \u2018class std::vector >\u2019 には \u2018addelements\u2019 という名前のメンバーがありません

dsets.h:25: エラー: \u2018std::vector > DisjointSets::uptree\u2019 はプライベートです

main.cpp:10: エラー: このコンテキスト内

main.cpp:11: エラー: \u2018uptree\u2019 はこのスコープで宣言されていません

何が悪いのか正確にはわかりません。どんな助けでも大歓迎です。

4

2 に答える 2

2

クラスの外部からクラスのプライベート要素にアクセスすることはできません。アップツリーを公開してみるか、DisjointSets を介してアクセスする手段を提供してください。また、addelements() はベクトル アップツリーではなく、クラス DisjointSets のメンバーです。

#ifndef MAIN_CPP
#define MAIN_CPP

#include "dsets.h"
using namespace std;

int main(){
DisjointSets s;
s.uptree.addelements(4); // try s.addelements(4)
for(int i=0; i<s.uptree.size(); i++) // try making uptree public
        cout <<uptree.at(i) << endl;
return 0;
}

#endif
于 2010-04-24T03:31:20.823 に答える
1

uptreeの非公開メンバーですDisjointSets。公開することもできますが、メンバーを公開せずに求める機能を提供する関数を DisjointSets に作成することをお勧めします。

于 2010-04-24T03:47:19.803 に答える