简体   繁体   English

n-ary树C ++的错误

[英]errors for n-ary tree C++

This is my first time working with any kind of tree. 这是我第一次使用任何一种树。 I created a tnode class for my tree and now I'm trying to create the class for the tree itself. 我为我的树创建了一个tnode类,现在我正在尝试为树本身创建类。 However I've gotten a couple errors I can't figure out. 但是我遇到了一些我无法弄清楚的错误。

#ifndef Tree_Ntree_h
#define Tree_Ntree_h
// SIZE reprsents the max number of children a node can have
#define SIZE 10
// SEPERATE will allow the program to distinguish when a subtree no longer has children
#define SEPERATOR '@'
#include <iostream>
#include <fstream>

template <typename T>
class tnode{
public:
T value;
tnode *child[SIZE];
tnode() {};
tnode<T> *addChild(T data){
    tnode*temp = new tnode;
    temp -> value = data;
    for (int i=0; i<SIZE; i++)
        temp -> child[i] = NULL;
    return temp;
}
};



template <typename T>
class Ntree{
private:
tnode<T> *root;
T data;
std::string filename;

public:

Ntree(){ root= NULL;}

Ntree( T data){ *root = data;}



inline T getRoot(){return root;}

My errors are in the last three lines. 我的错误在最后三行。 In the last line of my code (getRoot), this is the error: 在我的代码的最后一行(getRoot)中,这是错误:

No viable conversion from 'tnode > *' to 'std::__1::basic_string' 没有可行的从'tnode> *'到'std :: __ 1 :: basic_string'的转换

In the second to last line and the third to last line (*root = data) (root = NULL) this is the error: 在倒数第二行和第三行到最后一行(* root = data)(root = NULL)这是错误:

No viable overloaded '=' 没有可行的重载'='

I don't understand why it is necessary to overload the = operator in this situation. 我不明白为什么在这种情况下需要重载=运算符。

root is a tnode<T> * and getRoot is returning a T object. root是一个tnode<T> *getRoot返回一个T对象。 The compiler doesn't know how to convert one to the other. 编译器不知道如何将一个转换为另一个。 You probably just want to return root->value 您可能只想返回root->value

However, you haven't allocated any space for root and it might be NULL , so you need to determine what to do when it is NULL. 但是,您没有为root分配任何空间,它可能为NULL ,因此您需要确定它为NULL时要执行的操作。

In this line: 在这一行:

Ntree( T data){ *root = data;}

This time you are assigning a T to a tnode<T> , which the compiler doesn't know how to do. 这次你将T分配给tnode<T> ,编译器不知道该怎么做。 You also haven't allocated any memory for root . 您还没有为root分配任何内存。 Instead you probably want todo something like: 相反,你可能想要todo之类的东西:

Ntree( T data){ root = new T; root->value = data;}

Or better still have a tnode constructor that takes a value. 或者更好的是还有一个tnode构造函数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM