C++ 错误无法从指针转换为指针

问题描述

我被要求编写一个函数,该函数文件中读取并将其数据输入到双向链表中。但是每次我尝试运行该程序时,都会显示以下编译错误

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

这是文件内容

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

这是我的代码

#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,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,I1->University,I1->Age,I1->gender,',') && getline(connections,I2->FirstName,I2->LastName,I2->University,I2->Age,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;
    }

如果有人能帮我解决这个编译错误。谢谢。

解决方法

有两个相似的 struct,称为 Individual。一个在文件范围内,另一个在 opening() 内。由于它们是两种不同的类型,因此您无法将指向它们的指针隐式转换(而且在学习时,几乎可以保证您做错了什么)。

然而,正如@IgorTandetnik 在评论中指出的那样,错误与此无关,请注意:

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

第二种类型不是指针,所以需要解引用:

InsertInWorld(network,*I1);

尽管在这种情况下您可能想要做的是传递引用:

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

也可能是 const

代码中还有其他问题。我建议你在代码审查堆栈交换网站上发布这个。

,

你有这个:

<xsl:template match="/root">
    <root>
        <even>
            <xsl:copy-of select="num[not(. mod 2)]"/>
        </even>
        <odd>
            <xsl:copy-of select="num[boolean(. mod 2)]"/>
        </odd>
    </root>
</xsl:template>

还有这个:

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

该错误告诉您,在预期第一个 struct Individual{ string FirstName,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 }; 类型的某个地方,而不是给出了指向第二个 Individual 类型的指针。因此,您遇到了传递指针而不是结构的问题,而且您还遇到了混合具有相同名称的两种不同类型的问题。