0

I am asked to write a function that read from a file and input it's data in a doubly linked list . But every time I try to run the program , a compiling error is displayed with the following :

[Error] could not convert 'I1' from 'opening(World&)::Individual*' to 'Individual'
[Error] could not convert 'I2' from 'opening(World&)::Individual*' to 'Individual'

This is the content of the file :

FName1#LName1#Age1#male#University1,FName2#LName2#Age2#male#University2
FName1#LName1#Age1#male#University1,FName3#LName3#Age3#male#University3
FName7#LName7#Age7#female#University7,FName1#LName1#Age1#male#University1
FName7#LName7#Age7#female#University7,FName2#LName2#Age2#male#University2
FName6#LName6#Age6#female#University6,FName7#LName7#Age7#female#University7
FName4#LName4#Age4#male#University4,FName6#LName6#Age6#female#University6
FName4#LName4#Age4#male#University4,FName5#LName5#Age5#male#University5
FName5#LName5#Age5#male#University5,FName6#LName6#Age6#female#University6

That's my code :

#include <iostream>
#include <fstream>
#include <string.h>
#include <string.h>

using namespace std;

 struct Friend;
 struct World ;
 struct Individual;

struct Friend{
    Individual *self;
    Friend *next;
};

struct Individual{
    string FirstName,
    LastName,
    University;
    int Age;
    bool gender;
    Friend *myFriends;    // used as an adjacency list to enumerate all friends
    Individual *next;     // usedfor the doubly linked list denoted by World
    Individual *previous; // used for the doubly linked list denoted by World
};


struct World {
    Individual *Head, *Tail;
};
 World * InitializeList()
{
    return NULL;
}



void InsertInWorld(World & network, Individual I) {

    Individual* tmp = new Individual;
    if (tmp == NULL)
    {
        exit(1);
    }
    tmp->FirstName = I.FirstName;
    tmp->LastName = I.LastName;
    tmp->University = I.University;
    tmp->Age = I.Age;
    tmp->gender = I.gender;
    tmp->myFriends->next = NULL;
    tmp->myFriends->self = NULL;
    tmp->next = NULL;
    tmp->previous = NULL;

    if (network.Head == NULL || network.Tail==NULL) {
        network.Head = tmp;
        network.Tail = tmp;
        return;
    }
    else
    {
        tmp->next = network.Head;
        network.Head->previous = tmp;
        network.Head = tmp;
        return;
    }

}


void DisplayWorld(World network)
{
    Individual *cur = network.Head;
    
    while(cur!=NULL)
    {
        cout<<cur->FirstName<<" "<<cur->LastName<<endl;
        cur=cur->next;
    }
    
    cout<<endl;
}

void opening (World &network )
{
    struct Individual{
    string FirstName,
    LastName,
    University;
    string Age;
    string gender;
    Friend* myFriends;
    Individual* next;
    Individual* previous;
    
};

struct World {
    Individual *Head, *Tail;
};

    
    Individual *I1 ;
    Individual *I2 ;
    I1 = new Individual;
    I2 = new Individual;
    Individual *cur;
    
//  cur = network->Head;
    
    
    
 ifstream connections;
 connections.open("conn.txt");
  while (   getline(connections, I1->FirstName,'#')  && getline(connections, I1->LastName, '#')  && getline(connections, I1->University,'#')  && getline(connections,I1->Age,'#')  && getline(connections,I1->gender,',') && getline(connections, I2->FirstName,'#')  && getline(connections, I2->LastName, '#')  && getline(connections, I2->University,'#')  && getline(connections, I2->Age,'#')  && getline(connections, I2->gender,'\n')) 
    {
   
        
        InsertInWorld(network, I1);
        InsertInWorld(network, I2);
        
    }
    
    
    
}


    
    
    int main()
    {

        World network;
        Individual *I1=new Individual;
        network.Head=NULL;
        network.Tail=NULL;
        opening(network);
        DisplayWorld(network);
        
        return 0;
    }

If anyone can help me with this compiling error . Thank you.

5
  • InsertInWorld takes Individual (without the star). You are trying to pass a variable of type Individual* (with the star). Contrary to the title of your question, your code attempts to covert a pointer to a type that's not a pointer. Commented Dec 19, 2020 at 15:08
  • @IgorTandetnik Ah okay but how could i fix this Commented Dec 19, 2020 at 15:10
  • Well, you either have InsertInWorld take a pointer, or dereference the pointer before passing it to InsertInWorld. Commented Dec 19, 2020 at 15:11
  • @IgorTandetnik That's the new error display [Error] could not convert 'I1' from 'opening(World&)::Individual' to 'Individual' Commented Dec 19, 2020 at 15:21
  • See the answer by @Acorn. Your program defines two distinct, unrelated types both named Individual, then attempts to convert from one to the other. Drop the definition of Individual inside opening Commented Dec 19, 2020 at 15:32

2 Answers 2

2

There are two similar structs called Individual. One is at file scope, and another is inside opening(). Since they are two different types, you cannot convert pointers to them implicitly (and when you are learning, it is almost a guarantee that you are doing something wrong).

However, as @IgorTandetnik points out in the comment, the error is not about that, notice:

could not convert ... from '...Individual*' to 'Individual'
                                         ^                ^

The second type is not a pointer, so you need to dereference it:

InsertInWorld(network, *I1);

Although what you likely want to do in that case is pass a reference:

void InsertInWorld(World & network, Individual & I) {

Possibly a const one, too.

There are other issues in the code, too. I suggest you post this in the code review stack exchange site.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your comment . I tried to put I as a pointer in the function Insertinworld but the error is still the same
@DaherRoy please re-read the first paragraph in this answer. You have two structs by the same name and your program fails to convert one to the other. Having two different things sharing the same name does not make them the same. So, while you have successfully fixed one of the errors, you still have one and you will need to make sure that the parameter you pass to that function is of the very same type as the function expects, not a type with a similar name.
0

You have this one:

void opening (World &network )
{
    struct Individual{
    string FirstName,
    LastName,
    University;
    string Age;
    string gender;
    Friend* myFriends;
    Individual* next;
    Individual* previous;
    
};

and this one:

struct Individual{
    string FirstName,
    LastName,
    University;
    int Age;
    bool gender;
    Friend *myFriends;    // used as an adjacency list to enumerate all friends
    Individual *next;     // usedfor the doubly linked list denoted by World
    Individual *previous; // used for the doubly linked list denoted by World
};

The error tells you that somewhere where the first Individual type is expected, instead of that a pointer to the second Individual type is given. So you have an issue with a pointer being passed instead of a structure and you also have an issue of mixing up two different types having the same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.