m0_71659698 2022-12-17 10:12 采纳率: 100%
浏览 36
已结题

C++不存在从结构到int 的适当转换函数应当如何解决啊


#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
#include"stdio.h"
struct Score {
    char* no;
    char* name;
    int score;
};
template <typename T>
struct Node {  //双向链表
    struct Node* prev;
    struct Node* next;
    T data;//T *data;
};
template <typename T>
struct Tree {
    struct Tree* left;
    struct Tree* right;
    struct Tree* parent;
    T data;//T *data;
};
template <typename T>
void print_node(Node<T>* pnode) {
    printf("pnode->prev = %X\t", pnode->prev);
    printf("pnode->next = %X\t", pnode->next);
    printf("pnode->data = %d\n", pnode->data);
}
template <typename T>
Node<T>* init_node(T data, Node<T>* prev, Node<T>* next) {
    Node<T>* p = new Node<T>;
    p->data = data;
    p->prev = prev;
    p->next = next;
    return p;
}
Node<int> node0;
void strcopy(char** dest, char* src) {
    char* p;
    if (*dest != NULL) {
        delete* dest;
    }
    p = (char*)malloc(strlen(src) + 1);
    if (p != NULL) {
        *dest = p;
        strcpy_s(*dest, strlen(src) + 1, src);
    }
    return;
}
typedef struct Score* PScore;
typedef struct Node<struct Score*>* PNode;
/*
功能:链表节点插入
参数:
p1:插入位置(p1之前)
p2:待插节点
*/
void insert(struct Node<struct Score*>* p1, struct Node<struct Score*>* p2) {
    if (p1 == NULL) {
        printf("插入位置不能为NULL.\n");
        return;
    }
    if (p2 == NULL) {
        printf("待插节点不能为NULL.\n");
        return;
    }
    //边界情况
    //最左边
    /*
    if (p1->prev == NULL) {
        p2->prev = p1->prev;
        p2->next = p1;
        p1->prev = p2;
        p1->next =
    }
    */

}
void displayScore(struct Score* p_score) {
    printf("%s\t%s\t%d\n", p_score->no, p_score->name, p_score->score);
}
/*
功能:构建成绩节点
*/
struct Score* newScore(char* no, char* name, int score) {
    struct Score* p_Score;
    p_Score = (struct Score*)malloc(sizeof(struct Score));
    if (p_Score == NULL)return NULL;
    p_Score->no = p_Score->name = NULL;
    strcopy(&p_Score->no, no);
    strcopy(&p_Score->name, name);
    p_Score->score = score;
    return p_Score;
}
/*
构建链表节点
*/
struct  Node<struct Score*>* new_Node(char* no, char* name, int score) {
    struct Node<struct Score*>* p_DL_Node;
    struct Score* p_Score;
    //申请struct Score,初始化成绩数据
    p_Score = newScore(no, name, score);

    //申请struct DL_Node<struct Score*>
    p_DL_Node = (struct Node<struct Score*>*) malloc(sizeof(struct Node<struct Score*>));
    if (p_Score != NULL && p_DL_Node != NULL) {
        //初始化链表节点
        p_DL_Node->prev = p_DL_Node->next = NULL;
        p_DL_Node->data = p_Score;
    }
    return p_DL_Node;
}
int main()
{
    struct Score score = { NULL,NULL,0 };
     //score = scores[0]; //结构体可以整体赋值
    char m[9] = "zhangsan";
    strcopy(&score.name, m);
    strcopy(&score.no, m);
    score.score = 80;
    printf("no=%s\tname=%s\tscore=%d\n", score.no, score.name, score.score);
    char n[5] = "张三";
    strcopy(&score.name, n);
    printf("no=%s\tname=%s\tscore=%d\n", score.no, score.name, score.score);
    char s[7] = "李四光";
    strcopy(&score.name, s);
    printf("no=%s\tname=%s\tscore=%d\n", score.no, score.name, score.score); 
    /*
    FILE *fp = fopen("data.txt", "w");
    if (fp == NULL) {
        printf("文件写打开失败!");
        return 1;
    }
    //fwrite()//二进制写
    fprintf(fp, "%s", "大家好!");
    fclose(fp);
    */
    FILE* fp2 = fopen("data.txt", "r");
    char strNo[100], strName[100];
    int score;
    if (fp2 == NULL) {
        printf("文件读打开失败!");
        return 1;
    }
    struct Score _score = { NULL,NULL,0 };//局部指针变量,其初值一般不为NULL,
    struct Node<struct Score*>* p_head = NULL, * p_end = NULL;
    struct Node<struct Score*>* p_new, * p_DL_Node;
    while (!feof(fp2)) {
        fscanf(fp2, "%s\t%s\t%d\n", strNo, strName, &score);
        p_new = new_Node(strNo, strName, score);
        //第一个节点
        if (p_head == NULL) {
            p_head = p_end = p_new;
        }
        else { //非第一个节点
            p_end->next = p_new;
            p_new->prev = p_end;
            p_end = p_end->next;
        }
        /*
        printf("%s\t%s\t%d\n", strNo, strName, score);
        strcopy(&_score.no, strNo);
        strcopy(&_score.name, strName);
        _score.score = score;
        //printf("%s\t%s\t%d\n", _score.no, _score.name, _score.score);
        displayScore(&_score);
        */

    }
    p_DL_Node = p_head;
    while (p_DL_Node != NULL) {
        displayScore(p_DL_Node->data);
        p_DL_Node = p_DL_Node->next;
    }

    p_DL_Node = p_end;
    while (p_DL_Node != NULL) {
        displayScore(p_DL_Node->data);
        p_DL_Node = p_DL_Node->prev;
    }
    fclose(fp2);
    getchar();
    return 0;
}

img


该如何解决里面的报错呀?

  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2022-12-17 10:26
    关注

    你这个new_Node函数的 第三个入参,是int呀,你咋把 结构体score 传进去???
    改成 score.score

    img

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 修改了问题 12月17日
  • 修改了问题 12月17日
  • 展开全部

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?