1

我有这个 cpp 文件。

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

和这个头文件

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

而且我不断收到一个错误,说“没有声明不相交集 ”
~~

4

1 回答 1

3

你的包容性倒退了。您需要包含 .cpp 文件中的标头 (.h) 文件,而不是像现在这样。

.cpp 文件是编译器实际要编译的文件;.h 文件只是为了包含在 .cpp 文件中。

此外,您不需要在 .cpp 文件的内容周围包含保护,因为您从来没有#include.cpp 文件(好吧,可能在有限的情况下可以这样做,但这并不常见)。您只需要保护头文件的内容。

于 2010-04-21T00:57:19.327 回答