简体   繁体   English

如何从同一个头文件中定义2个类,而另一个类依赖于另一个类?

[英]How can I define 2 classes from the same header file, while one class depends on the other?

Im currently trying to implement a simple path-finding algorithm and need edges and nodes for it. 我目前正在尝试实现一个简单的路径寻找算法,并需要边缘和节点。 I want to handle the implementation of those in one .h and one .cpp file. 我想在一个.h和一个.cpp文件中处理它们的实现。 Right now I get the error "expected constructor, destructor or type conversion before ...". 现在我得到错误“期望构造函数,析构函数或类型转换之前......”。

I already tried separating both classes into 2 .h and .cpp-files, but that didnt work either. 我已经尝试将两个类分成2个.h和.cpp文件,但这也没有用。 I've tried a lot of solutions provided for that error message, but nothing seems to work and I think there something Im missing right now. 我已经尝试了很多针对该错误消息提供的解决方案,但似乎没有任何工作,我认为我现在缺少一些东西。

My utilites.cpp file looks a bit like that 我的utilites.cpp文件看起来有点像

#include "utilities.h"

//Class Node
//Public

using namespace std;

Node::Node(string name)
{
  this->name = name;
}

//Class Edge
//public

Edge::Edge(Node::Node nSource, Node::Node nTarget, int weight)
{
  this->nSource = nSource;
  this->nTarget = nTarget;
  this->weight = weight;
}

and my utilities.h: 和我的utilities.h:

#ifndef UTILITIES_H
#define UTILITIES_H

#include <string>
#include <list>

class Node
{
public:
  Node(std::string);
  std::string name;
};


class Edge
{
public:
  Edge(Node, Node, int);
  Node nSource;
  Node nTarget;
  int weight;
};

#endif /* end of include guard: UTILITIES_H */

If I just use the Class Node, everything works. 如果我只使用类节点,一切正常。 But if I want implement Class Edge with the Class Node, I'll get the error previously mentioned. 但是,如果我想使用类节点实现Class Edge,我将得到前面提到的错误。 I think it is an easy solve, but I just cant figure it out. 我认为这很容易解决,但我无法弄明白。

I should say that I already tried it with 我应该说我已经试过了

Edge::Edge(Node nSource, Node nTarget, int weight)
{
  this->nSource = nSource;
  this->nTarget = nTarget;
  this->weight = weight;
}

but that just gave me the error "No matching function for call to 'Node::Node()' 但这只是给了我错误“没有匹配函数调用'Node :: Node()'

The problem was that I was missing the curly braces after the default constructor of Node 问题是我在Node的默认构造函数之后缺少花括号

Node(){};

Now it works as intended. 现在它按预期工作。 Thanks for the answers, they made me look at the default constructor closer again... 感谢您的回答,他们让我再次看到默认构造函数...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何定义两个相互依赖的类,最好是在同一个头文件中? - How can I define two inter dependant classes, preferably in the same header file? 两个抽象类,派生自相同的基础 class。 如何访问从一个抽象 class 到另一个的指针 - Two abstract classes, deriving from the same base class. How can I access a pointer from one abstract class to the other 如何使用条件编译从其他文件访问 header 文件中的定义宏? - How can I access the define macro in the header file from other files with Conditional Compilation? 如何让一个类的向量从其他两个类的向量中推回数据? - How can I get one class's vector to push back data from vectors in two other classes? 我可以在 class header 文件中定义一个 class 的 const static 实例吗 - Can I define a const static instance of a class in the class header file 我有两个类需要在同一个 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象 - I have two classes that need to reference each other in the same cpp file, but the first one won't recognize objects of the second class type 在 C++ 中,如何在另一个嵌套类中使用嵌套类类型(两个嵌套类在同一个外部类下) - In C++, how can I use a nested class type in an other nested class (the two nested classes are under the same outer class) 我可以在同一个.CPP文件中声明和定义静态类变量吗? - Can I declare and define static class variable in a same .CPP file? 在 C 或 C++ 中,如何防止 header 文件中先前的#define 影响另一个 Z099FB9953406FC 文件包含的 EEFC 文件? - in C or C++, how can I prevent previous #define in a header file from affecting another header file later included? 如何在单独的头文件的结构中定义char *数组? - How can i define an array of char* in a struct in a separate header file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM