I’m getting this error in several methods for several variables (all of which are vectors):
error: ‘parent’ was not declared in this scope
I’ve tried wrapping my method implementations inside of “namespace DisjointSubsets { … }”, but that causes other problems. It seems to only do this for vectors, and I’ve tried adding a “#include vector” at the start of the cpp file, it didn’t change anything.
Here is the header file:
#ifndef UNIVERSE
#define UNIVERSE
#include <vector>
class DisjointSubsets {
public :
DisjointSubsets ( unsigned numberElements = 5 ) ;
unsigned findDS ( unsigned ) ;
bool unionDS ( unsigned , unsigned ) ;
private :
vector<unsigned> parent ;
vector<unsigned> rank ;
unsigned size ;
} ;
#include "DisjointSubsets.cpp"
#endif
And here is an example of one of the methods I wrote in the cpp file (which has no #includes):
unsigned DisjointSubsets::findDS(unsigned index) {
return parent[index];
}
(Changed the method to be non-functional, but still illustrate the kind of line that would cause a problem. Just in case someone else working on the assignment stumbles across this.)
You must use
std::vector<unsigned>instead of justvector<unsigned>to declareparentbecausevectoris declared in thestdnamespace.Therefore you could also use
using namespace std;before declaring the class.However most people I know would discourage you from using the second form in a header file.
See the C++ FAQ for a more elaborate discussion on this topic.