views:

44

answers:

1

I have this cpp file.

dsets.cpp:

   #ifndef DSETS_CPP
   #define DSET_CPP

   //Adds elements to the DisjointSet data structure. This function adds
   //x unconnected roots to the end of the array.
   void DisjointSets::addelements(int x){
   }

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

   int DisjointSets::find(int x){
   return 0;
   }

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

   }

   #endif

and this header file

dsets.h:

   #ifndef DSETS_H
   #define DSET_H
   #include <iostream>
   #include <vector>
   using namespace std;


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

   private:
   vector<int> x;

   };

   #include "dsets.cpp"
   #endif

And I keep getting an error that is saying that "DisjointSets has no been declared"
~
~

+3  A: 

You have your inclusion backwards. You need to include the header (.h) file from the .cpp file, not the other way around like you have it now.

The .cpp file is the file that the compiler is actually going to compile; the .h file is just meant to be included into .cpp files.

In addition, you don't need include guards around the contents of a .cpp file, since you never #include a .cpp file (okay, there might be limited circumstances under which that might be done, but it's not common). You only need guards around the contents of header files.

James McNellis
Comment removed: I'm an idiot.
Mac
@Mac: I don't know what the comment said, but I'm sure you're not an idiot :-)
James McNellis