gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:14:53 25 4
gpt4 key购买 nike

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

"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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com