1

我是 C 编程新手。我尝试创建一个小而简单的程序来从文件 calculs.x 中添加两个整数

这里,文件calculs.x的内容

/* calculs.x*/

struct data_in {
    int arg1;
    int arg2;
};
typedef struct data_in data_in;

struct result_int {
    int result;
    int errno;
};

struct result_float {
    int result;
    int errno;
};

typedef struct result_int result_int;
typedef struct result_float result_float;

program CALCULS{
    version VERSION_UN{
        void CALCULS_NULL(void) = 0;
        result_int ADD (data_in) = 1;
        result_int SUB(data_in) = 2;
        result_int MUL(data_in) = 3;
        result_float DIV (data_in) = 4;
    } = 1;
} = 0x20000001;

我第一次为客户创建了一个文件 calculs.c:

#include <rpc/rpc.h>
#include "calculs.h"

int main(int argc, char *argv[]) {
    int buffer[256];
    struct data_in input;
    struct result_int *output;
    CLIENT *cl;

    if (argc != 2) {
        printf("usage: client hostname_of_server\n");
        exit(1);
    }

    /*Etablir le lien vers le serveur distant
     * cl = clnt_create(server, PROG, VERS, prot);
     */
    cl = clnt_create(argv[1], CALCULS, VERSION_UN, "tcp");
    if (cl == NULL) {
        clnt_pcreateerror(argv[1]);
        exit(1);
    }

    input.arg1 = 5;
    input.arg2 = 5;

    output = add_1(&input, cl);
    if (output == NULL) {
        clnt_perror(cl, argv[1]);
        exit(1);
    }
    printf("the result field is %d\n", output->result);
    printf("the errno field is %d\n", output->errno);

    clnt_destroy(cl);

    return 0;
}

我没有收到任何错误编译这个文件,但是对于另一个 rcalculs.c 文件,我无法编译。这是文件内容rcalculs.c:

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp) {
    int buffer;
    struct result_int result;
    int a = data.arg1;
    int b = data.arg2;
    buffer = a+b;
    result.result = buffer;
    result.errno =0;
    return result;
}

编译的消息错误是

rcalculs.c:11:13: erreur: conflicting types for ‘add_1’
In file included from rcalculs.c:9:0:
calculs.h:46:22: note: previous declaration of ‘add_1’ was here
rcalculs.c: In function ‘add_1’:
rcalculs.c:19:5: erreur: incompatible types when returning type ‘struct result_int’ but ‘struct result_int *’ was expected

你能帮我解决这个问题吗?

4

3 回答 3

1

如下使用您的 calculs.x。

/* calculs.x*/

struct data_in {
    int arg1;
    int arg2;
};
typedef struct data_in data_in;

struct result_int {
    int result;
    int errno;
};

struct result_float {
    int result;
    int errno;
};

typedef struct result_int result_int;
typedef struct result_float result_float;

program CALCULS{
    version VERSION_UN{
        void CALCULS_NULL(void) = 0;
        result_int ADD (data_in) = 1;
        result_int SUB(data_in) = 2;
        result_int MUL(data_in) = 3;
        result_float DIV (data_in) = 4;
    } = 1;
} = 0x20000001;

对于您的客户 calculs.c 用户

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp);

int main(int argc, char *argv[]) {
    int buffer[256];
    struct data_in input;
    struct result_int *output;
    CLIENT *cl;

    if (argc != 2) {
        printf("usage: client hostname_of_server\n");
        exit(1);
    }

    /*Etablir le lien vers le serveur distant
     * cl = clnt_create(server, PROG, VERS, prot);
     */
    cl = clnt_create(argv[1], CALCULS, VERSION_UN, "tcp");
    if (cl == NULL) {
        clnt_pcreateerror(argv[1]);
        exit(1);
    }

    input.arg1 = 5;
    input.arg2 = 5;

    output = add_1(&input, cl);
    if (output == NULL) {
        clnt_perror(cl, argv[1]);
        exit(1);
    }
    printf("the result field is %d\n", output->result);
    printf("the errno field is %d\n", output->errno);

    clnt_destroy(cl);

    return 0;
}

rcalculs.c 的内容:

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp) {
    int buffer;
    struct result_int result;
    int a = data.arg1;
    int b = data.arg2;
    buffer = a+b;
    result.result = buffer;
    result.errno =0;
    return &result;
}

让我知道它是否有效。

于 2015-11-25T04:23:28.547 回答
0

You are defining the function add_1 as returning a pointer to a result_int when you say

result_int *add_1(struct data_in data, struct svc_req *rqstp) {

in your file rcalculs.c

However, inside that function you define

struct result_int result;

and when you return a value, you do

return result;

If you don't want an error, you would have to do

return &result;

The problem with that is that your result variable was created locally inside the function (on the stack) - so that it will become invalid once the function returns. A quick and ugly "fix" for this would be to define your variable as static (meaning that 1- it will continue to exist after the function returns, but 2- your function is no longer thread safe: if it gets called from two different places there is no knowing what value will be referenced).

You did not post your .h file so I am guessing a little bit, but I think the following change to rcalculs.c would "fix" your problem (but note the comment above... this is NOT considered good programming practice)

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp) {
    int buffer;
    static struct result_int result;
    int a = data.arg1;
    int b = data.arg2;
    buffer = a+b;
    result.result = buffer;
    result.errno =0;
    return &result;
}

Let me know if that works. Note - this is intended just to help you understand the error - I DO NOT RECOMMEND this as program structure for a "real" program.

Bonne chance!

于 2013-10-07T04:29:12.177 回答
0
erreur: conflicting types for ‘add_1

这个错误是因为你没有声明 add_1() 函数。并且当您对 add_1() 进行函数调用时,它会将其视为声明。

在前面添加这一行main()

  result_int *add_1(struct data_in data, struct svc_req *rqstp);

rcalculs.c:19:5:erreur:返回类型“struct result_int”但预期为“struct result_int *”时类型不兼容

  result_int * add_1(struct data_in data, struct svc_req *rqstp) {
    int buffer;
    struct result_int result; //here declare pointer
    int a = data.arg1;
    int b = data.arg2;
    buffer = a+b;
    result.result = buffer; //change here
    result.errno =0;        //change here 
    return result; //here you are returning struct result_int type but required is struct result_int * type 
   }

返回指针。并在您的功能中进行相应的更改。


output = add_1(&input, cl); //here you are passing reference of input but you just need to pass input.
于 2013-10-07T04:31:28.007 回答