cuốn sách gpt4 ai đã làm

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

In lại Tác giả: Walker 123 更新时间:2023-11-28 02:14:53 26 4
mua khóa gpt4 Nike

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

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

错误出现在chủ yếu()gọi vàodisplay()

#include 
#include
#include

sử dụng không gian tên 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
{
được bảo vệ:

công cộng:
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 tô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;
tôi++;
}
return i;
}
void add(Person *data[], Node*root)
{
int i = fileName(data);
if(root == NULL)
{
root = new Node();
}
for(int l = 0; 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);
}
trở lại;
}

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{
được bảo vệ:
State *root;
công cộng:
treeState()
{
root = NULL;
}
};

khoảng trống chính(){
treePersons T;
T.display(T.root->data,T.root);
}

câu trả lời hay nhất

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

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

让我们看看什么是 treePersons:

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

它包含一个成员:一个节点。一个节点Đúng:

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

của bạn treePersons::display() 函数具有以下签名:

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

你正在传递一个t.root->data(一个Người)和一个t.root(一个Node* )

问题是您正试图将 Người BẰNG Person*[] 传递,这是不会发生的。无法将 Người trở nên Person[],您可能打算让 display 带一个 Person* 指针,它将允许您传递单个 Người hoặc Người 的容器:void display(Person* data, Node* root);

当然,正如@R Sahu 在评论中指出的那样,这样做会导致您遇到一大堆问题(您的大多数函数都采用 Person*[]。这里的解决方案是重新思考您正在做的事情,正如@R Sahu 建议的那样,从大大较小的开始并从那里构建您的程序。

也考虑使用 std::vectơ当你需要容器时,std::unique_ptrhoặc std::shared_ptrnhu cầu指针的地方(否则just use objects!)。还要阅读(真正đọc)编译器输出。它告诉您问题出在哪里,您只需要đọc.

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

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress