Stroustrup 5.9,练习1 [英] Stroustrup 5.9, exercise 1

查看:83
本文介绍了Stroustrup 5.9,练习1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

------ PROGRAM -----------

/ * Stroustrup,5.9,练习


写一些声明(带初始值)

评论解释我们打算做什么

* /

#include< iostream>

int main()

{


//指向角色的指针

char c1 =''a'' ;

char * pc1 =& c1;


// 10个整数的数组

const int arr_size = 10;

char arr_int [arr_size];


//对10个整数数组的引用

char& r_arr_int = arr_int;


//指向字符串数组的指针

/ * SORRY但我不知道如何呈现数组

字符串* /


//指向char的指针

char c;

char * pc =& c;

char * ppc = pc;


//一个常数整数

const int ci = 7;


//指向常数整数的指针

const int * pci =& ci;


//一个指向整数的常量指针

int j = 8;

int * const cpi =& j;

return 0;

}


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

[arch @ voodo tc ++ pl] $ g ++ 5.9_ex-01.cpp

5.9_ex-01.cpp:在函数''int main()'':

5.9_ex-01.cpp:23:错误:非const引用无效初始化

类型为''char&''来自临时类型''char * ''

[arch @ voodo tc ++ pl] $

----------- ------------------------


我没有使用-ansi -W标志,因为他们发出警告我做了

此刻不需要纠正。我知道这是一个错误:


//对10个整数数组的引用

char& r_arr_int = arr_int;


我找不到任何方法来创建对整数的引用。顺便说一句,所有

声明都可以吗?

解决方案

g ++ 5.9_ex-01.cpp
5.9_ex-01.cpp:在函数''int main()''中:

5.9_ex-01.cpp:23:错误:非const引用的初始化无效

类型''char&''来自临时类型''char *''

[arch @ voodo tc ++ pl]




-----------------------------------


i没有使用-ansi -W标志,因为他们发出警告我做了

此刻不需要纠正。我知道这是一个错误:


//对10个整数数组的引用

char& r_arr_int = arr_int;


我找不到任何方法来创建对整数的引用。顺便说一下,所有

声明都好吗?


* arnuld:


>

i知道这是一个错误:


//对10个整数数组的引用

焦炭和放大器; r_arr_int = arr_int;


我找不到任何方法来创建对整数的引用。顺便说一下,所有

声明都好吗?



上面你有一个对char的引用,用一个

整数数组初始化。


一边是苹果,另一边是橘子。


这是对数组的引用:


int(& r_arr_int)[10] = arr_int;

-

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

Q :为什么这么糟糕?

A:热门帖子。

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


------ PROGRAMME -----------
/* Stroustrup, 5.9, exercises

to write some declarations (with initializers)
comments explain what we intend to do
*/
#include<iostream>
int main()
{

// a pointer to a character
char c1 = ''a'';
char* pc1 = &c1;

// an array of 10 integers
const int arr_size = 10;
char arr_int[arr_size];

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

// a pointer to an array of character strings
/* SORRY but i do not know how to present an array
of character strings */

// a pointer to a pointer to a char
char c;
char* pc = &c;
char* ppc = pc;

// a constant integer
const int ci = 7;

// a pointer to a constant integer
const int* pci = &ci;

// a constant pointer to an integer
int j = 8;
int *const cpi = &j;
return 0;
}

--------- OUTPUT ---------------
[arch@voodo tc++pl]$ g++ 5.9_ex-01.cpp
5.9_ex-01.cpp: In function ''int main()'':
5.9_ex-01.cpp:23: error: invalid initialization of non-const reference
of type ''char&'' from a temporary of type ''char*''
[arch@voodo tc++pl]$
-----------------------------------

i did not use "-ansi -W" flags as they were emitting warnings i did
not need to correct at this moment. i know that this is an error:

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

i dont find any way to create a reference to an integer. BTW, are all
declarations fine ?

解决方案

g++ 5.9_ex-01.cpp
5.9_ex-01.cpp: In function ''int main()'':
5.9_ex-01.cpp:23: error: invalid initialization of non-const reference
of type ''char&'' from a temporary of type ''char*''
[arch@voodo tc++pl]



-----------------------------------

i did not use "-ansi -W" flags as they were emitting warnings i did
not need to correct at this moment. i know that this is an error:

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

i dont find any way to create a reference to an integer. BTW, are all
declarations fine ?


* arnuld:

>
i know that this is an error:

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

i dont find any way to create a reference to an integer. BTW, are all
declarations fine ?

What you have above is a reference to char, initialized with an array of
integers.

Apples on one side, oranges on the other side.

This is a reference to an array:

int (&r_arr_int)[10] = arr_int;
--
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?


这篇关于Stroustrup 5.9,练习1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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