无法将参数1从Person转换为Person * []

时间:2015-12-17 05:05:09

标签: c++ file data-structures binary-tree

我试图制作从文件中读取并添加到二叉树的程序,但是当我尝试编译时出现错误:

  

"错误1' treePersons :: display' :无法从' Node *'转换参数1到人物* []'"

display()

中对main()的调用中显示错误
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Person{
  int social;
  int birthday;
  string first;
  string last;
  string state;
  double balance;
  Person();
  Person(int s, int b, string f, string l, string t, double a)
  {
    social = s;
    birthday = b;
    first = f;
    last = l;
    state = t;
    balance = a;
}
};

struct Node{
    Person data;
    Node *left;
    Node *right;
    Node();
    Node(Person x){
        data = x;
        left = NULL;
        right = NULL;
    }
};

class treePersons
{
protected:

public:
    Node *root;
    treePersons(){
        root = NULL;
}

int fileName(Person *data[])
{
  ifstream fin; 
  fin.open ("dbfile1.txt");  
  if (!fin.is_open())
      cout << "File not found" << endl;
  int i;
  for(i = 0; i<100; i++)
      while(fin.good())
      {
          fin >> data[i]->social >> data[i]->birthday >> data[i]->first >> data[i]->last >> data[i]->state >> data[i]->balance;
          i++;
      }
      return i;
}
void add(Person *data[], Node*root)
{
    int i = fileName(data);
    if(root == NULL)
    {
        root = new Node();
    }
    for(int l = 0; l<i; l++)
    {
    if(data[i]->last == root->data.last)
    {
        if(data[i]->first != root->data.first)
        {
          if(data[i]->first < root->data.first)
          {
            add(data, root->left);
          }
          else if(data[i]->first > root->data.first)
          {
            add(data, root->right);
          }
          else if(data[i]->last == root->data.last && data[i]->first == root     ->data.first)
          {
            cout << "already exists" << endl;
          }
          else if(data[i]->first < root->data.first)
          {
            add(data, root->left);
          }
          else if(data[i]->first > root->data.first)
          {
            add(data, root->right);
          }
        }
    }
    }
}

void printAlphabetically(Node *root)
{
  if (root != NULL) 
  {
    printAlphabetically(root->left);
    cout << root->data.last << endl;
    printAlphabetically(root->right);
  }
return;
}

void display(Person *data[],Node *root)
{
    add(data,root);
    printAlphabetically(root);
};

};

struct State{
    string state;
    Person data;
    State* left;
    State * right;
    State();
    State(Person x)
    {
        data = x;
        left = NULL;
        right = NULL;
    }


};

class treeState{
protected:
    State *root;
public:
    treeState()
    {
        root = NULL;
    }
};

void main(){
    treePersons T;
    T.display(T.root->data,T.root);
}

1 个答案:

答案 0 :(得分:2)

很容易看出您的代码出了什么问题。您有以下内容:

treePersons T;
T.display(T.root->data, T.root);

让我们看看treePersons是什么:

class treePersons
{
    Node *root;
    ...
};

它包含一个成员:NodeNode是:

struct Node
{
    Person data;
    Node *left;
    Node *right;
    ...
};

您的treePersons::display()函数具有以下签名:

void display(Person *data[], Node *root)

您传递的是t.root->dataPerson)和t.rootNode*

问题是,您试图将Person作为Person*[]传递,但这不会发生。没有办法将Person变成Person[],你可能想让display取一个Person*指针,这样你就可以通过一个PersonPerson的容器:void display(Person* data, Node* root);

当然,正如@R Sahu在评论中指出的那样,这样做会引导你解决一大堆问题(你的大部分功能都需要Person*[]。这里的解决方案是重新思考你在做什么,并且正如@R Sahu所建议的那样,开始更多更小并从那里建立你的程序。

在需要容器时考虑使用std::vector,在需要指针时使用std::unique_ptrstd::shared_ptr(否则为just use objects!)。还读取(真正读取)编译器输出。它告诉你问题是什么,你只需要阅读

相关问题