0

I'm trying to make program that reads from a file and adds to a binary tree, but when I try to compile I get an error:

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

The error appears in the call to display() in 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);
}
Tas
  • 7,023
  • 3
  • 36
  • 51
  • 1
    There too many problems in your code. My suggstion: work through some tutorials on the web or exercises in a text book. As far as the posted program is concerned, I suggest start small, make sure it works, and then keep adding more code. – R Sahu Dec 17 '15 at 05:27
  • Can you point out what needs to be fixed? – Michael Castellanos Dec 17 '15 at 05:42

1 Answers1

2

It's very simple to see what's wrong with your code. You have the following:

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

Let's have a look at what a treePersons is:

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

It contains a single member: a Node. A Node is:

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

Your treePersons::display() function has the following signature:

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

And you are passing a t.root->data (a Person) and t.root (a Node*)

The problem is you are attempting to pass a Person as a Person*[] which just isn't going to happen. There's no way to make that Person into a Person[], and you probably meant to make display take a Person* pointer, which will allow you pass a single Person or a container of Person: void display(Person* data, Node* root);

Of course, doing so will lead you down a big trail of problems as @R Sahu pointed out in the comments (most of your functions take a Person*[]. The solution here is to rethink what you are doing, and as @R Sahu suggests start much smaller and build up your program from there.

Consider also using std::vector when you need containers, and std::unique_ptr or std::shared_ptr where you require pointers (otherwise just use objects!). Also read (really read) the compiler output. It's telling you what the problem is, you just need to read.

Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51