[英]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.