C++ “类名未声明”错误
我有这个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的包容性倒退了。您需要包含 .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.