帮助解决c错误

发布于 2024-11-06 09:49:11 字数 1079 浏览 5 评论 0原文

收到此错误:

<前><代码>1>c:\用户\b1021568\文档\视觉

工作室 2010\项目\tarefa42\tarefa
42\main.cpp(112): 错误 C2664: 'cria_aluno':无法转换 参数 2 从 'const char [7]' 到 '字符' 1>没有任何上下文可以进行此转换

尝试编译

int main(void)
{
    Aluno *a[5];

    a[0] = cria_aluno(1, "turma1", "Joao", 7.0, 8.4, 4.3);
    a[1] = cria_aluno(2, "turma2", "Maria", 3.2, 5.1, 10.0);
    a[2] = cria_aluno(3, "turma3", "Rafael", 8.1, 3.2, 4.5);
    a[3] = cria_aluno(4, "turma4", "Jose", 1.3, 7.7, 9.3);
    a[4] = cria_aluno(5, "turma5", "Lais", 4.5, 1.3, 9.9);

    ordena(5, a);
return 0;
}

:这就是我的 cria_aluno 函数:

Aluno *cria_aluno(int mat, char turma, char nome, float p1, float p2, float p3)
{
    Aluno *a;

    a = (Aluno*) malloc(sizeof(Aluno));
    if(a == NULL)
    {
        printf("Memoria insuficiente");
        return NULL;
    }
    a->mat = mat;
    a->turma = turma;
    strcpy(a->nome, nome);
    a->p1 = p1;
    a->p2 = p2;
    a->p3 = p3;
    return a;
}

getting this error:

1>c:\users\b1021568\documents\visual

studio 2010\projects\tarefa42\tarefa
42\main.cpp(112): error C2664:
'cria_aluno' : cannot convert
parameter 2 from 'const char [7]' to
'char'
1> There is no context in which this conversion is possible

when trying to compile this:

int main(void)
{
    Aluno *a[5];

    a[0] = cria_aluno(1, "turma1", "Joao", 7.0, 8.4, 4.3);
    a[1] = cria_aluno(2, "turma2", "Maria", 3.2, 5.1, 10.0);
    a[2] = cria_aluno(3, "turma3", "Rafael", 8.1, 3.2, 4.5);
    a[3] = cria_aluno(4, "turma4", "Jose", 1.3, 7.7, 9.3);
    a[4] = cria_aluno(5, "turma5", "Lais", 4.5, 1.3, 9.9);

    ordena(5, a);
return 0;
}

thats my cria_aluno function:

Aluno *cria_aluno(int mat, char turma, char nome, float p1, float p2, float p3)
{
    Aluno *a;

    a = (Aluno*) malloc(sizeof(Aluno));
    if(a == NULL)
    {
        printf("Memoria insuficiente");
        return NULL;
    }
    a->mat = mat;
    a->turma = turma;
    strcpy(a->nome, nome);
    a->p1 = p1;
    a->p2 = p2;
    a->p3 = p3;
    return a;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别再吹冷风 2024-11-13 09:49:11

将其更改为

Aluno *cria_aluno(int mat, const char* turma, const char* nome, float p1, float p2, float p3)

“turma1”,等等都是 const char* 类型,而不是 char

Change it to

Aluno *cria_aluno(int mat, const char* turma, const char* nome, float p1, float p2, float p3)

"turma1", etc. are of type const char*, not char

若水般的淡然安静女子 2024-11-13 09:49:11

更改

Aluno *cria_aluno(int mat, char turma, char nome, float p1, float p2, float p3)

Aluno *cria_aluno(int mat, const char* turma, const char* nome, float p1, 
                  float p2, float p3)
{
   Aluno *a = (Aluno*) malloc(sizeof(Aluno));
   if(a == NULL)
   {
      printf("Memoria insuficiente");
      return NULL;
   }
   a->mat = mat;
   a->turma = malloc(strlen(turma)+1);
   strcpy(a->turma, turma);
   a->nome = malloc(strlen(nome)+1);
   strcpy(a->nome, nome);
   a->p1 = p1;
   a->p2 = p2;
   a->p3 = p3;
   return a;
}

Change

Aluno *cria_aluno(int mat, char turma, char nome, float p1, float p2, float p3)

to

Aluno *cria_aluno(int mat, const char* turma, const char* nome, float p1, 
                  float p2, float p3)
{
   Aluno *a = (Aluno*) malloc(sizeof(Aluno));
   if(a == NULL)
   {
      printf("Memoria insuficiente");
      return NULL;
   }
   a->mat = mat;
   a->turma = malloc(strlen(turma)+1);
   strcpy(a->turma, turma);
   a->nome = malloc(strlen(nome)+1);
   strcpy(a->nome, nome);
   a->p1 = p1;
   a->p2 = p2;
   a->p3 = p3;
   return a;
}
情栀口红 2024-11-13 09:49:11

您的函数期望参数 2 和 3 为 char 类型,而不是 char 指针(char*,通常用作“字符串”)。

在您的主函数中,您使用 char* 类型(字符串)作为参数 2 和 3 调用 cria_aluno,这就是错误的原因。

首先,您需要决定要在 Aluno 结构中存储什么。让我们以turma为例:

如果你想存储单个字符,你应该在结构体和函数中使用char作为turma的类型。另外,在函数调用中,您应该使用单个字符作为参数 2,例如:'a'。要复制此字符,您应该使用简单的复制:a->turma = turma;

如果您希望存储字符串,则应使用 char[x] (其中 x 是最大字符串长度 + 末尾的 \0)作为结构中 turma 的类型。在函数中,您应该使用 char* (const char* 会更好)。在函数调用中,您可以使用字符串(例如:“example”)。要复制此属性,您应该使用 strcpy。

将 turma 以字符串模式存储在结构中的另一种方法是将结构中的类型更改为 char*。然后,在需要时分配内存。

祝你好运

阿米尔

Your function expect as parameter 2 and 3 char type and not char pointer (char*, usually used as "string").

In you main function, you called cria_aluno with char* type (string) as parameter 2 and 3, that is the cause of your error.

First you need to decide what you wish to store in Aluno structure. Lets take turma as example:

If you wish to store a single character, you should use char as the type of turma in the structure and in the function. Also, in the function call, you should use a single character as parameter 2, for example: 'a'. To copy this character, you should use a simple copy: a->turma = turma;

If you wish to store a string, you should use char[x] (where x is the max string length + \0 at the end) as the type of turma in the structure. In the function, you should use char* (const char* will be better). In the function call, you can use a string (example: "example"). To copy this attribute, you should use strcpy.

Another way to store turma in your structure as string mode, is change the type to char* in the structure. Then, when needed, allocate the memory.

Good luck

Amir

梦与时光遇 2024-11-13 09:49:11

在函数调用中,

a[0] = cria_aluno(1, "turma1", "Joao", 7.0, 8.4, 4.3); 

"turma1""Joao"字符串文字,它们是 char 数组 (<在 C++ 中代码>const char)。两个表达式的类型分别为char[7]char[5]。这些类型与 char 不兼容,而这正是您在 cria_aluno 中声明的 turmanome 的类型,因此出现错误。

在大多数情况下,数组表达式的类型从“T 的 N 元素数组”隐式转换为“指向 T 的指针”。因此,实际传递给 cria_aluno 的是两个 char * 类型的表达式,而不是 char。因此,您需要将 cria_aluno 的声明更改为

Aluno *cria_aluno(int mat, const char *turma, const char *nome, float p1, float p2, float p3)

const char * 而不是 char *?这有助于防止您意外修改指针指向的内容;尝试修改字符串文字的内容会导致未定义的行为。

In the function call

a[0] = cria_aluno(1, "turma1", "Joao", 7.0, 8.4, 4.3); 

"turma1" and "Joao" are string literals, which are arrays of char (const char in C++). The types of the two expressions are char [7] and char [5], respectively. These types are not compatible with char, which is what you've declared turma and nome to be in cria_aluno, hence the error.

In most circumstances, array expressions have their types implicitly converted from "N-element array of T" to "pointer to T". So what actually gets passed to cria_aluno are two expressions of type char *, not char. Thus, you need to change the declaration of cria_aluno to

Aluno *cria_aluno(int mat, const char *turma, const char *nome, float p1, float p2, float p3)

Why const char * instead of char *? This helps protect you from accidentally modifying the contents of what the pointer points to; attempting to modify the contents of a string literal leads to undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文