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

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

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

"Error 1 'treePersons::display' : cannot convert parameter 1 from 'Node *' to 'Person *[]'"

错误出现在main()中调用display()

#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);
}

最佳答案

查看您的代码有什么问题非常简单。你有以下内容:

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

让我们看看什么是 treePersons:

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

它包含一个成员:一个节点。一个节点是:

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

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

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

你正在传递一个t.root->data(一个Person)和一个t.root(一个Node* )

问题是您正试图将 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!)。还要阅读(真正阅读)编译器输出。它告诉您问题出在哪里,您只需要阅读

关于c++ - 无法将参数 1 从 Person 转换为 Person *[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326979/

相关文章:

c++ - 在 tree.hh 中移动任意节点及其子节点作为子节点

c++ - 加载带有 362 项目的解决方案后,Win XP 上的 VS2005 SP1 崩溃,没有任何错误、日志或跟踪

excel - 带文件路径的间接函数

c++ - 使用数组 : It's crashing on run-time 的单链表

c++ - 使用指向类的指针访问成员变量

c++ - 使用 CreateDirectory() 捕获 ERROR_ALREADY_EXISTS 和类似错误

c++ - 在 CodeGear C++ Builder 上有比 TMemo 更快的组件吗?

file - 尝试使用 io.WriteString 写入文件时出现 Golang "Access is denied"错误

c++ - 使用标准 C++ 复制大型二进制文件

algorithm - 最小生成树怕负权重吗?