如何从一个函数传递一个struct并将其从头文件返回到main [英] How to pass and return a struct from a function to main from header file
问题描述
您好,我需要一些帮助,我正在尝试从我的头文件调用一个函数到我的main,这个函数返回一个结构。
这里的问题是我没有得到如何调用一个从它返回结构的函数,我尝试了一些东西,但没有任何工作。
输出
Hello, I need some help with this, I'm trying to call a function from my header file to my main, this function return a structure.
The problem here is that I don't get how to call a function that returns a structure from it, I have tried some things but nothing has worked.
Output
error: request for member 'Insert' in 'root', which is of pointer type 'TREE::tree*' (maybe you meant to use '->' ?)
root = root.Insert(root, 9);
error: request for member 'Print' in 'root', which is of pointer type 'TREE::tree*' (maybe you meant to use '->' ?)
root.Print(root);
头文件
The header file
#ifndef _LibArb_H_
#define _LibArb_H_
struct Node{
int val;
Node *left, *right;
};
using namespace std;
namespace TREE
{
class tree
{
private:
Node *node;
public:
tree();
~tree();
Node* Insert(Node *node, int val) // Returns a struct
{
if(node == NULL) // If no nodes then create a new one
{
node = new Node;
node->val = val;
node->right = node->left = NULL;
}
else if(node->val > val)
node->left = Insert(node->left, val); // Insert value on the left node
else
node->right = Insert(node->right, val); // Insert value on the right node
return node; // Return the value of node
}
void Print(Node *node)
{
if (node)
{
cout << "Val: " << node->val << endl;
cout << "Left: " << node->left << endl;
cout << "Right: " << node->right << endl;
Print(node->left);
Print(node->right);
}
}
};
}
# endif // _LibArb_H_
#include <iostream>
#include "LibArb.h"
using namespace std;
using namespace TREE; // From the header file
int main()
{
tree *root = NULL; // Create new root from the class tree of the header file
root = root.Insert(root, 9); // New node with the number 9 on it
root.Print(root); // Print all the nodes from the tree
return 0;
}
我的尝试:
root = root-> Insert(root,9);失败
插入(root,9);失败
What I have tried:
root = root->Insert(root, 9); Fail
Insert(root, 9); Fail
推荐答案
tree *root = NULL; // Create new root from the class tree of the header file
root = root.Insert(root, 9); // New node with the number 9 on it
第一个语句创建一个指针并将其设置为NULL,因此它不指向任何内容。然后尝试在NULL指针上调用 Insert
方法,该方法将始终失败。你需要初始化 root
,然后你可以使用它,但是 - >
引用,而不是dot;因此:
The first statement creates a pointer and sets it to NULL, so it does not point to anything. You then try and call the Insert
method on a NULL pointer which will always fail. You need to initialise root
and then you can use it, but by ->
reference, not dot; thus:
tree *root = new tree(); // Create new tree pointer
// and as root is a pointer we need to use the -> reference type
root = root->Insert(root, 9); // New node with the number 9 on it
但是,正如您的代码所示,现在将覆盖根指针,因此它将丢失。我怀疑Insert方法需要一些工作,或者树类中的节点
变量应该公开。
However, as your code stands, this will now overwrite the root pointer so it will be lost. I suspect the Insert method needs some work, or the node
variable in the tree class should be made public.
tree *root = new tree(); // Create new tree pointer
// and as root is a pointer we need to use the -> reference type
root->node = root->Insert(root, 9); // New node with the number 9 on it
您还应该从头文件中删除类的实现,只留下声明。
You should also remove the implementation of your classes from the header file and leave just the declarations.
这篇关于如何从一个函数传递一个struct并将其从头文件返回到main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!