访问“结构”的问题使用“ - >” [英] problem with accessing a "struct" using "->"

查看:50
本文介绍了访问“结构”的问题使用“ - >”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

--------计划-----------

/ * Stroustrup,5.6结构


声明:

这个程序*尝试*以3个部分执行此操作:


1.)它创建一个名为jd的struct。 ,类型为地址。

2.然后将值添加到jd

3.),最后它打印jd的值。


* /


#include< iostream>

#include< vector>


struct address;

void fill_addr(address); //将值分配给类型为
的结构
" address"

void print_addr(address *); //打印地址结构


结构地址{

char * name;

char * country;

};


int main()

{

地址jd; //地址 struct

fill_addr(jd);

print_addr(jd);


返回0;

}

struct * fill_addr(地址* jd)

{

jd.name =" Niklaus Wirth" ;;

jd.country =" Switzerland";


返回jd;

}


void print_addr(地址* p)

{

使用std :: cout;

使用std :: endl;

cout << p->名称

<< ''\ n''

<< p-> country

<< endl;

}


----------- OUTPUT --------------

[arch @ voodo tc ++ pl] $ g ++ -ansi -pedantic -Wall -Wextra

5.6_structures.cpp

5.6_structures.cpp :在函数''int main()''中:

5.6_structures.cpp:30:错误:无法将''地址''转换为''地址*''

参数''1''''void print_addr(address *)''

5.6_structures.cpp:在全球范围内:

5.6_structures.cpp:36 :error:'*''之前的预期标识符

5.6_structures.cpp:在函数''int * fill_addr(address *)''中:

5.6_structures。 cpp:38:错误:请求''jd''中的成员''name'',

属于非类型''地址*''

5.6_structures.cpp:39:错误:请求''jd''中的成员''country'',

这是非类型''地址*''

5.6_structures.cpp:41:错误:无法转换'地址*''到''int *''在

返回

[arch @ voodo tc ++ pl] $

i知道30行处的错误意思是但我无法纠正它。我用不同的方式尝试使用地址来尝试
和地址*但它

不起作用:-(

解决方案

g ++ -ansi -pedantic -Wall -Wextra

5.6_structures.cpp

5.6_structures.cpp:在函数''int main()''中:

5.6_structures.cpp:30 :错误:无法将''地址''转换为''地址*''

参数''1''''void print_addr(address *)''

5.6_structures.cpp:在全球范围内:

5.6_structures.cpp:36:错误:''*''标记之前的预期标识符

5.6_structures.cpp:In function''int * fill_addr(address *)'':

5.6_structures.cpp:38:错误:请求''jd''中的成员''name'',

属于非类型类型''地址*''

5.6_structures.cpp:39:错误:请求''jd''中的成员''country'',

这是非类型的''地址*''

5.6_structures.cpp:41:错误:无法转换'地址*''到''int *''在

返回

[arch @ voodo tc ++ pl]




i知道第30行的错误。意思是但我无法纠正它。我用不同的方式尝试使用地址来尝试
和地址*但它

不起作用:-(


* arnuld:


>

i知道第30行的错误意味着但我无法纠正它。我想用不同的方式尝试b $ b地址和地址*但是它

不起作用:-(



尝试地址运算符,&& 。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


-------- PROGRAMME -----------
/* Stroustrup, 5.6 Structures

STATEMENT:
this programmes *tries* to do do this in 3 parts:

1.) it creates a "struct", named "jd", of type "address".
2. it then adds values to "jd"
3.) in the end it prints values of "jd".

*/

#include<iostream>
#include<vector>

struct address;
void fill_addr(address); // assigns values to a struct of type
"address"
void print_addr(address*); // prints an address struct

struct address {
char* name;
char* country;
};

int main()
{
address jd; // an "address" struct
fill_addr(jd);
print_addr(jd);

return 0;
}
struct* fill_addr(address* jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerland";

return jd;
}

void print_addr(address* p)
{
using std::cout;
using std::endl;
cout << p->name
<< ''\n''
<< p->country
<< endl;
}

----------- OUTPUT --------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
5.6_structures.cpp
5.6_structures.cpp: In function ''int main()'':
5.6_structures.cpp:30: error: cannot convert ''address'' to ''address*''
for argument ''1'' to ''void print_addr(address*)''
5.6_structures.cpp: At global scope:
5.6_structures.cpp:36: error: expected identifier before ''*'' token
5.6_structures.cpp: In function ''int* fill_addr(address*)'':
5.6_structures.cpp:38: error: request for member ''name'' in ''jd'', which
is of non-class type ''address*''
5.6_structures.cpp:39: error: request for member ''country'' in ''jd'',
which is of non-class type ''address*''
5.6_structures.cpp:41: error: cannot convert ''address*'' to ''int*'' in
return
[arch@voodo tc++pl]$
i know the error at "line 30" means but i am not able to correct it. i
tried with different ways of using "address" and "address*" but it
does not work :-(

解决方案

g++ -ansi -pedantic -Wall -Wextra
5.6_structures.cpp
5.6_structures.cpp: In function ''int main()'':
5.6_structures.cpp:30: error: cannot convert ''address'' to ''address*''
for argument ''1'' to ''void print_addr(address*)''
5.6_structures.cpp: At global scope:
5.6_structures.cpp:36: error: expected identifier before ''*'' token
5.6_structures.cpp: In function ''int* fill_addr(address*)'':
5.6_structures.cpp:38: error: request for member ''name'' in ''jd'', which
is of non-class type ''address*''
5.6_structures.cpp:39: error: request for member ''country'' in ''jd'',
which is of non-class type ''address*''
5.6_structures.cpp:41: error: cannot convert ''address*'' to ''int*'' in
return
[arch@voodo tc++pl]



i know the error at "line 30" means but i am not able to correct it. i
tried with different ways of using "address" and "address*" but it
does not work :-(


* arnuld:

>
i know the error at "line 30" means but i am not able to correct it. i
tried with different ways of using "address" and "address*" but it
does not work :-(

Try the address operator, "&".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于访问“结构”的问题使用“ - &gt;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆