| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2559 人关注过本帖
标题:不能将struct Node* 的值赋给struct Node *实体?这都什么情况啊
只看楼主 加入收藏
令狐少侠56
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:320
专家分:175
注 册:2014-4-10
结帖率:58.18%
收藏
已结贴  问题点数:40 回复次数:4 
不能将struct Node* 的值赋给struct Node *实体?这都什么情况啊
语句
struct listsNode *pos ;
pos = find(user, hash) ;
这里find函数返回值也是struct listsNode *类型的为什么提示说
不能将struct  Node* 的值赋给struct  Node *实体。

程序代码:
#include <iostream>
#include <stdlib.h>
#include<malloc.h>
#include <string>
#include <vector>
#include <sstream> 

using namespace std;

struct userNode
{
    string  account  ;//账号
    string  password ;//密码
    char    order;//命令符,判断是登陆还是申请
};

struct hashHead
{
    int tableSize;
    struct listNode *theLists;
};

struct listNode
{
    struct userNode userMessage;
    struct listNode *next;
};
struct hashHead*  initHashTable(  int  tableSize  )
{
    tableSize = nextPrime(   tableSize ) ;

    struct hashHead* hash;
    hash = (struct  hashHead* )malloc( sizeof( struct  hashHead  ) )  ;
    hash->tableSize = tableSize ;

    hash->theLists = (struct listNode*) malloc(tableSize* sizeof( struct listNode )  )  ;

    for ( int i = 0; i < tableSize; ++i )
    {
        hash->theLists[i].next = NULL ;
        hash->theLists[i].userMessage.account = "0";//账号密码全部默认为0
        hash->theLists[i].userMessage.password = "0";
        hash->theLists[i].userMessage.order = '0';
    }
    return hash;
}

int nextPrime(  int size )
{
    int i;
    if(size % 2 == 0 ) ++size;
    for ( ; ; size += 2 )
    {
        for (i = 3; i*i < size; ++i)//判断size是否为素数
            if (size % i == 0)  break ;
        if (i*i > size)  return size ;
    }
}


void checkAndOutPut(  struct userNode *user , struct hashHead*  hash )
{
    //将账号后五位数字作为有效数字,用于计算散列址
    int address  ;
    //number = atoi( (user->account+5) ) ;
    address= atoi(  user->account.c_str()   )% hash->tableSize  ;//未截取

    struct listsNode *pos ;
    pos = find(user, hash) ;//出错了??
    if (  pos  )//找到后核对用户信息
    {
        if (user->password == find(user, hash)->userMessage.password)
            cout << "密码正确" << "登录中" << endl;
        else
            cout << "密码错误"<<endl;

    }
    else//pos=NULL时未找到,此时插入改节点
    {
        insertList( user, hash , address ) ;
        cout << "建立改账号" << endl ;
    }

}

void insertList(struct userNode *user, struct hashHead*  hash, int address)
{
    struct listNode *headPos;
    headPos = &hash->theLists[address];

    struct listNode *insertNode;
    insertNode = (struct listNode*)malloc( sizeof( struct listNode  )  )  ; 
    insertNode->userMessage = *user ; //将user内容传给改节点
    
    insertNode->next = headPos ;//头插
    headPos = insertNode ;
}


struct listNode *  find(struct userNode *user, struct hashHead*  hash )
{
    int address;
    //计算散列址
    address = atoi( user->account.c_str() ) % hash->tableSize;

    struct listNode *pos ;
    pos = &hash->theLists[address] ; 
    while ( !pos->next  && pos->userMessage.account != user->account  )//存在下一个节点且不为key
        pos = pos->next  ;
    
    return pos ; 
}

int main()
{
    int N ;
    
    scanf("%d", &N) ;
    struct hashHead* hash;
    hash = initHashTable(10000)  ;

    for (int i = 1; i <= N; ++i)
    {
        userNode *user ;//传引用去掉*
        user = (struct userNode*)malloc(sizeof(struct userNode)) ;
        scanf("%c %s %s", &user->order, &user->account,&user->password  ) ;
        //查找,并输出账号信息,存在,不存在等
        checkAndOutPut(  user )  ;
    }

    return 0  ;
}
2016-10-10 00:02
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:40 
看清楚了,是不能从 listNode* 转化为 listsNode* 吧
2016-10-10 08:13
令狐少侠56
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:320
专家分:175
注 册:2014-4-10
收藏
得分:0 
回复 2楼 rjsp
。。。。改过来了,谢谢。
2016-10-10 16:41
令狐少侠56
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:320
专家分:175
注 册:2014-4-10
收藏
得分:0 
发现代码里输入有问题
看了半天也没看出来啊,
scanf("%c", &user->order);
scanf("%s", &user->account);
scanf("%s", &user->password);
这几句哪里错了?为什么一输入就说出现错误,终止了。


程序代码:
#include <iostream>
#include <stdlib.h>
#include<malloc.h>
#include <string>
#include <vector>
#include <sstream> 

using namespace std;

struct userNode
{
    char    order;//命令符,判断是登陆还是申请
    string  account  ;//账号
    string  password ;//密码
    
};

int main()
{
    int N ;
    
    scanf("%d", &N) ;


    for (int i = 1; i <= N; ++i)
    {
        struct userNode *user ;
        user = (struct userNode*)malloc(sizeof(struct userNode)) ;
        //scanf_s("%c %s %s", &user->order,  &user->account,  &user->password  ) ;
        scanf("%c", &user->order);
        scanf("%s", &user->account);
        scanf("%s", &user->password);
        cout << user->order << user->account << user->password << endl;
    }
    return 0  ;
}
2016-10-10 16:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
你的问题多得一塌糊涂,建议忘掉已学的错误知识,洗清重来

仅就你提的这个问题,有两个错误
第一,user->account 根本就没创建,只是分配了内存而已
第二,scanf的%s能接string*?
2016-10-10 19:38
快速回复:不能将struct Node* 的值赋给struct Node *实体?这都什么情况啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017518 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved