C++ “类名未声明”错误

发布于 2024-08-30 00:30:55 字数 984 浏览 11 评论 0原文

我有这个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

我不断收到一个错误,说“DisjointSets 尚未声明”
~

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"
~
~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄暮涼年 2024-09-06 00:30:55

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文