帮助解决c错误
收到此错误:
<前><代码>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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其更改为
“turma1”,等等都是
const char*
类型,而不是char
Change it to
"turma1", etc. are of type
const char*
, notchar
更改
为
Change
to
您的函数期望参数 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
在函数调用中,
"turma1"
和"Joao"
是字符串文字,它们是char
数组 (<在 C++ 中代码>const char)。两个表达式的类型分别为char[7]
和char[5]
。这些类型与char
不兼容,而这正是您在cria_aluno
中声明的turma
和nome
的类型,因此出现错误。在大多数情况下,数组表达式的类型从“
T
的 N 元素数组”隐式转换为“指向T
的指针”。因此,实际传递给cria_aluno
的是两个char *
类型的表达式,而不是char
。因此,您需要将cria_aluno
的声明更改为const char *
而不是char *
?这有助于防止您意外修改指针指向的内容;尝试修改字符串文字的内容会导致未定义的行为。In the function call
"turma1"
and"Joao"
are string literals, which are arrays ofchar
(const char
in C++). The types of the two expressions arechar [7]
andchar [5]
, respectively. These types are not compatible withchar
, which is what you've declaredturma
andnome
to be incria_aluno
, hence the error.In most circumstances, array expressions have their types implicitly converted from "N-element array of
T
" to "pointer toT
". So what actually gets passed tocria_aluno
are two expressions of typechar *
, notchar
. Thus, you need to change the declaration ofcria_aluno
toWhy
const char *
instead ofchar *
? 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.