【问题标题】:C++ 'ClassName Not Declared' ErrorC++“未声明类名”错误
【发布时间】:2010-04-21 00:54:20
【问题描述】:

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

而且我不断收到一条错误消息,提示“未声明不相交集”
~
~

【问题讨论】:

  • 你的编译命令是什么样的?
  • 另外,我不知道是不是拼写错误,但用于#ifndef 和#define 的标记应该是相同的。
  • 另外一件事,不要放“using namespace ...;”在头文件中。

标签: c++ compilation undeclared-identifier


【解决方案1】:

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

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

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

【讨论】:

  • @Mac:我不知道评论说了什么,但我敢肯定你不是白痴:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多