鱼C论坛

 找回密码
 立即注册
查看: 4187|回复: 8

关于Windows CodeBlocks的用法 急急急!!!

[复制链接]
发表于 2012-8-25 11:39:46 | 显示全部楼层 |阅读模式
6鱼币
在CodeBlocks新建了C工程,在a.h中进行一些宏定义和函数原型声明。
在b.c中实现了这些函数(已经#include "a.h" 和extern函数)
在main.c中调用这些函数的时候出现以下错误:
obj\Debug\main.o||In function `main':|
\main.c|30|undefined reference to `getElement'|
\main.c|47|undefined reference to `insertElement'|
\main.c|67|undefined reference to `deleteElement'|
\main.c|87|undefined reference to `updateElement'|
||=== Build finished: 4 errors, 0 warnings ===|


小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-8-25 11:45:44 | 显示全部楼层
贴上代码:
List.h
==================================================
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#define SUCCESS 0
#define FAILURE -1
#define TRUE 0
#define FALSE -1
#define MAXSIZE 20
typedef int ElementType;
typedef int Status;
typedef struct{
    ElementType data[MAXSIZE];
    int length;
}SqList;

Status getElement(SqList,int,ElementType *);
Status insertElement(SqList *,int,ElementType e);
Status deleteElement(SqList *,int,ElementType *);
Status updateElement(SqList *,int,ElementType,ElementType *);
Status execute(char choice);

#endif // LIST_H_INCLUDED

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-25 11:46:51 | 显示全部楼层
继续贴上代码:
action.c
===================================================

#include <stdio.h>
#include <stdlib.h>
#include "List.h"

extern Status getElement(SqList,int,ElementType *);
extern Status insertElement(SqList *,int,ElementType e);
extern Status deleteElement(SqList *,int,ElementType *);
extern Status updateElement(SqList *,int,ElementType,ElementType *);

Status getElement(SqList list,int position,ElementType *e){
    if(list.length==0 || position<1 || position>list.length){
        return FAILURE;
    }
    *e = list.data[position-1];
    return SUCCESS;
}

Status insertElement(SqList *list,int position,ElementType e){
    if(list->length == MAXSIZE){
        return FAILURE;
    }
    if(position <1 || position > list->length+1){
        return FAILURE;
    }
    if(position <= list->length){
        for(int idx=list->length;idx>=position;idx--){
           list->data[idx] = list->data[idx-1];
        }
        list->data[position-1] = e;
        list->length++;
    }
    if(list->length+1 <= MAXSIZE && position == list->length+1){
        list->data[position-1] = e;
        list->length++;
    }
    return SUCCESS;
}

Status deleteElement(SqList *list,int position,ElementType *e){
    if(list->length == 0){
        return FAILURE;
    }
    if(position<1 || position > list->length){
        return FAILURE;
    }
    *e = list->data[position-1];
    if(position < list->length){
        for(int idx=position;idx<list->length;idx++){
            list->data[idx-1] = list->data[idx];
        }
    }
    if(position == list->length){
        list->data[position-1] = NULL;
    }
    list->length--;
    return SUCCESS;
}

Status updateElement(SqList *list,int position,ElementType newValue,ElementType *e){
    Status tempResult = getElement(*list,position,e);
    if(tempResult == FAILURE) return FAILURE;
    list->data[position-1] = newValue;
    return SUCCESS;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-25 11:47:28 | 显示全部楼层
继续贴上代码:
main.c
================================================================

#include <stdio.h>
#include <stdlib.h>
#include "List.h"

int main()
{
    SqList list;
    int i;
    for(i=0;i<MAXSIZE-5;i++){ //数组长度为MAXSIZE,实际存储的线性表长度MAXSIZE-5
        list.data[i] = i+1;
    }
    list.length = MAXSIZE-5;

    ElementType e;
    ElementType newValue;
    int position;
    Status result;
    char choice;

    printf("Enter 'G'(Query) 'I'(Insert) 'D'(Delete) 'U'(Update) to operate list:\n");
    scanf("%c",&choice);
    if(choice>='a' && choice<='z'){
        choice = choice - 32;
    }

    switch(choice){
        case 'G':
            printf("Please enter a integer to find element !\n");
            scanf("%d",&position);
            result = getElement(list,position,&e);
            if(result == SUCCESS){
                printf("Element found: data[%d] = %d",position-1,e);
            }
            if(result == FAILURE){
                printf("Element not found: data[%d]",position-1);
            }
            break;
        case 'I':
            printf("Please enter two numbers,the 1st one is postion,the 2nd is data :\n");
            scanf("%d%d",&position,&e);
            printf("position is: %d ; element is: %d\n\n",position,e);
            printf("The data before inserting is: \n");
            for(i=0;i<list.length;i++){
                printf("%d ",list.data[i]);
            }
            printf("\n");
            result = insertElement(&list,position,e);
            if(result == SUCCESS){
                printf("The data after inserting is: \n");
                for(i=0;i<list.length;i++){
                    printf("%d ",list.data[i]);
                }
                printf("\n");
            }
            if(result == FAILURE){
                printf("Inserting failed !\n");
            }
            break;
        case 'D':
            printf("Please enter a integer to delete element:\n");
            scanf("%d",&position);
            printf("The data before deleting is: \n");
            for(i=0;i<list.length;i++){
                printf("%d ",list.data[i]);
            }
            printf("\n");
            result = deleteElement(&list,position,&e);
            if(result == SUCCESS){
                printf("The data after inserting is: \n");
                for(i=0;i<list.length;i++){
                    printf("%d ",list.data[i]);
                }
                printf("\n");
            }
            if(result == FAILURE){
                printf("Deleting failed !\n");
            }
            break;
        case 'U':
            printf("Please enter two numbers,the 1st one is postion,the 2nd is new data :\n");
            scanf("%d%d",&position,&newValue);
            printf("The data before updating is: \n");
            for(i=0;i<list.length;i++){
                printf("%d ",list.data[i]);
            }
            printf("\n");
            result = updateElement(&list,position,newValue,&e);
            if(result == SUCCESS){
                printf("The data after updating is: \n");
                for(i=0;i<list.length;i++){
                    printf("%d ",list.data[i]);
                }
                printf("\n");
            }
            if(result == FAILURE){
                printf("Updating failed !\n");
            }
            break;
        default:
            printf("Operation undefined !");
            break;
    }
    return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-25 11:48:59 | 显示全部楼层
一共就List.h   action.c  main.c这三个文件,在main.c中函数的时候报错,求大家伸出援助之手
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-25 12:25:51 | 显示全部楼层
这些代码看得有点吓人,我没怎么看。不过这有一个关于CodeBlocks的使用教程,你可以参考下。
CodeBlocks使用教程
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-25 12:26:24 | 显示全部楼层
http://d.1tpan.com/tp0298884227
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-25 15:47:48 | 显示全部楼层
代码没什么问题,只是定义变量的位置出问题了,  在标准C中规定变量定义必须放在所有的执行语句之前!
你只要把
  1. ElementType e;
  2.      ElementType newValue;
  3.      int position;
  4.      Status result;
  5.      char choice;
复制代码
这些变量的定义放在所有的执行语句之前就可以了!
还有for循环语句 里定义的变量
  1. for(int idx=list->length;idx>=position;idx--){
  2.             list->data[idx] = list->data[idx-1];
  3.          }
复制代码
把 int idx 放在所有的执行语句之前!
修改后的工程:
next.zip (223.65 KB, 下载次数: 1)

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-25 21:03:05 | 显示全部楼层
学习学习啦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-24 08:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表