Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;错误无法从指针转换为指针_C++_Pointers_Struct - Fatal编程技术网

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

C++ C++;错误无法从指针转换为指针,c++,pointers,struct,C++,Pointers,Struct,我被要求编写一个函数,从一个文件中读取数据并将其输入到一个双链接列表中。但每次我尝试运行该程序时,都会显示一个编译错误,如下所示: [Error] could not convert 'I1' from 'opening(World&)::Individual*' to 'Individual' [Error] could not convert 'I2' from 'opening(World&)::Individual*' to 'Individual' 这是文件的内容:

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

[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,
    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;
    }
#包括
#包括
#包括
#包括
使用名称空间std;
结构朋友;
结构世界;
结构个体;
结构朋友{
个人*自我;
朋友*下一个;
};
结构个体{
字符串名,
姓,
大学
智力年龄;
布尔性别;
Friend*myFriends;//用作邻接列表以枚举所有朋友
单个*next;//用于由World表示的双链接列表
单个*previous;//用于由World表示的双链接列表
};
结构世界{
个体*头,*尾;
};
世界*初始化列表()
{
返回NULL;
}
void InsertInWorld(世界和网络,个人I){
个人*tmp=新个人;
if(tmp==NULL)
{
出口(1);
}
tmp->FirstName=I.FirstName;
tmp->LastName=I.LastName;
tmp->University=I.大学;
tmp->Age=I.年龄;
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){
网络头=tmp;
network.Tail=tmp;
返回;
}
其他的
{
tmp->next=network.Head;
network.Head->previous=tmp;
网络头=tmp;
返回;
}
}
void DisplayWorld(世界网络)
{
个人*cur=网络头;
while(cur!=NULL)
{
coutFirstName,“#”&&getline(connections,I2->LastName,“#”)&&getline(connections,I2->University,“#”)&&getline(connections,I2->Age,“#”)&&getline(connections,I2->gender,“\n”)
{
InsertInWorld(网络,I1);
InsertInWorld(网络,I2);
}
}
int main()
{
世界网络;
个人*I1=新个人;
network.Head=NULL;
network.Tail=NULL;
开放(网络);
显示世界(网络);
返回0;
}

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

有两个类似的结构称为
Individual
。一个在文件范围内,另一个在
opening()
内。因为它们是两种不同的类型,所以不能隐式地将指针转换为它们(当你在学习时,几乎可以保证你做错了事情)

然而,正如@IgorTandetnik在评论中指出的,错误并不在于此,请注意:

could not convert ... from '...Individual*' to 'Individual'
                                         ^                ^
第二种类型不是指针,因此需要取消对它的引用:

InsertInWorld(network, *I1);
尽管在这种情况下,您可能想做的是传递参考:

void InsertInWorld(World & network, Individual & I) {
也可能是一个常量


代码中还有其他问题。我建议您将此问题发布在代码审阅堆栈交换站点。

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

然而,正如@IgorTandetnik在评论中指出的,错误并不在于此,请注意:

could not convert ... from '...Individual*' to 'Individual'
                                         ^                ^
第二种类型不是指针,因此需要取消对它的引用:

InsertInWorld(network, *I1);
尽管在这种情况下,您可能想做的是传递参考:

void InsertInWorld(World & network, Individual & I) {
也可能是一个常量

代码中还有其他问题。我建议您将此问题发布在代码审阅堆栈交换站点。

您有以下问题:

void opening (World &network )
{
    struct Individual{
    string FirstName,
    LastName,
    University;
    string Age;
    string gender;
    Friend* myFriends;
    Individual* next;
    Individual* previous;
    
};
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
};
还有这个:

void opening (World &network )
{
    struct Individual{
    string FirstName,
    LastName,
    University;
    string Age;
    string gender;
    Friend* myFriends;
    Individual* next;
    Individual* previous;
    
};
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
};
该错误告诉您,在第一个
个体
类型预期出现的地方,会给出指向第二个
个体
类型的指针。因此,在传递指针而不是结构时会出现问题,并且还会出现混淆两个具有相同名称的不同类型的问题。

您有这个问题一:

void opening (World &network )
{
    struct Individual{
    string FirstName,
    LastName,
    University;
    string Age;
    string gender;
    Friend* myFriends;
    Individual* next;
    Individual* previous;
    
};
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
};
还有这个:

void opening (World &network )
{
    struct Individual{
    string FirstName,
    LastName,
    University;
    string Age;
    string gender;
    Friend* myFriends;
    Individual* next;
    Individual* previous;
    
};
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
};

该错误告诉您,在第一个
个体
类型预期出现的地方,会给出指向第二个
个体
类型的指针。因此,在传递指针而不是结构时会出现问题,并且还会出现混淆两个具有相同名称的不同类型的问题。

InsertInWorld
接受
个人
(不带星号)。您正在尝试传递类型为
个人*
(带星号)的变量。与您问题的标题相反,您的代码试图将指针转换为非指针的类型。@IgorTandetnik啊,好吧,但我如何解决这个问题呢?您要么让
InsertInWorld
获取指针,要么在将指针传递到
InsertInWorld
@IgorTandetnik之前取消对它的引用这是新的错误显示
[错误]无法将“I1”从“打开(世界&)”转换::从“个人”到“个人”
请参见@Acorn的答案。您的程序定义了两种不同的、不相关的类型,均命名为
Individual
,然后尝试从一种类型转换为另一种类型。将
Individual
的定义放入
打开
插入世界
获取
Individual
(无星号)。您正在尝试传递类型为
Individual*
(带星号)的变量。与您问题的标题相反,您的代码试图将指针转换为非指针的类型。@IgorTandetnik啊,好吧,但我如何解决这个问题呢?您要么让
InsertInWorld
获取指针,要么在将指针传递到
InsertInWorld
@IgorTandetnik之前取消对它的引用这是新的错误显示
[Error]无法将“I1”从“opening(World&)::Individual”转换为“Individual”