帮助动态数组结构。 [英] help with dynamic array structure.

查看:58
本文介绍了帮助动态数组结构。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C ++ Primer Plus第5版学习C ++。通过使用''new''来声明一个三个结构的数组来分配内存,我正在为

苦苦挣扎。 (这是最后的编程练习之一
每章
)。我的尝试如下。我收到一个编译错误

表示 -


基本操作数'' - >''没有指针类型''candybar'' ;


// Q-09.cpp - 问题9

#include< iostream>

struct candybar

{

// char brand [20];

std :: string brand; //在这个上下文中 - 必须使用std :: string

浮动重量;

int卡路里;

};

int main()

{

使用命名空间std;

candybar * snack = new candybar [3];
< br $> b $ b snack [0] - > brand =" Mocha Munch" ;; //上面的错误从这里开始

零食[0] - >重量= 2.3;

零食[0] - >卡路里= 350;


零食[1] - >品牌=" Crunchie" ;;

零食[1] - >重量= 1.9;

零食[1 ] - >卡路里= 400;


snack [2] - > brand =" Mars Bar" ;;

snack [2] - >重量= 4.5;

零食[2] - >卡路里= 200;


cout<< endl;

cout<< 小吃品牌: <<零食[0] - >品牌<< endl;

cout<< 小吃重量: <<零食[0] - >重量<< endl;

cout<< 零食卡路里: <<零食[0] - >卡路里<< endl;


cout<< 小吃品牌: <<零食[1] - >品牌<< endl;

cout<< 小吃重量: <<零食[1] - >重量<< endl;

cout<< 零食卡路里: <<零食[1] - >卡路里<< endl;


cout<< 小吃品牌: <<零食[2] - >品牌<< endl;

cout<< 小吃重量: <<小吃[2] - >重量<< endl;

cout<< 零食卡路里: <<零食[2] - >卡路里<< endl;


删除小吃;

返回0;

}


任意帮助将不胜感激,我真的不明白为什么这不应该b
工作。我无数次读了这章,但我一定错过了什么。


Steve Taylor

澳大利亚墨尔本。

解决方案

" Steven Taylor" <是ne ****** @ superjacent.net>在消息中写道

news:42 *********************** @ news.optusnet.com.au

我正在从C ++ Primer Plus第5版学习C ++。通过使用''new''
分配内存来宣布三个结构的数组,我正在努力奋斗。 (这是每章末尾的编程练习之一)。我的尝试如下。我收到一个
编译错误,表明 -

' - >''的基本操作数没有指针类型''candybar''"

// Q-09.cpp - 问题9
#include< iostream>
struct candybar
// char char [20];
std :: string品牌; //在这个上下文中 - 必须使用std :: string
浮动权重;
int卡路里;
};
int main()
{
使用namespace std;
candybar * snack = new candybar [3];

snack [0] - > brand =" Mocha Munch" ;; //上面的错误从这里开始
零食[0] - >重量= 2.3;
零食[0] - >卡路里= 350;



全部替换 - >经营者。运算符。


snack是一个指针,但使用下标运算符取消引用

指针。因此snack [0]是candybar类型,而不是指向candybar的指针。

-

John Carson




John Carson写道:

" Steven Taylor" <是ne ****** @ superjacent.net>在消息中写道
新闻:42 *********************** @ news.optusnet.com.au

我正在从C ++ Primer Plus第5版学习C ++。通过使用''new''
分配内存来宣布三个结构的数组,我正在努力奋斗。 (这是每章末尾的编程练习之一)。我的尝试如下。我收到一个
编译错误,表明 -

' - >''的基本操作数没有指针类型''candybar''"

// Q-09.cpp - 问题9
#include< iostream>
struct candybar
// char char [20];
std :: string品牌; //在这个上下文中 - 必须使用std :: string
浮动权重;
int卡路里;
};
int main()
{
使用namespace std;
candybar * snack = new candybar [3];

snack [0] - > brand =" Mocha Munch" ;; //上面的错误从这里开始
零食[0] - >重量= 2.3;
零食[0] - >卡路里= 350;



全部替换 - >经营者。运算符。

snack是一个指针,但使用下标运算符取消引用
指针。因此snack [0]是candybar类型,而不是指向candybar的指针。




感谢你的hlep John,它肯定编译好了。


Steve。


Steven Taylor写道:


我正在从C ++ Primer Plus第5版学习C ++。通过使用''new'来分配内存来声明三个结构的数组,我正在努力奋斗。 (这是每章最后的编程练习之一)。我的尝试如下。我收到编译错误
表示 - ' - >''的基本操作数没有指针类型''candybar''"

> // Q-09.cpp - 问题9
#include< iostream>
struct candybar
// char char [20];
std :: string品牌; //在这个上下文中 - 必须使用std :: string
浮动权重;
int卡路里;
};
int main()
{
使用namespace std;
candybar * snack = new candybar [3];

snack [0] - > brand =" Mocha Munch" ;; //上面的错误从这里开始




你的问题就在这里。

这是用C ++定义数组索引的方式


a [index]相当于*(a_as_ptr + index)


其中a_as_ptr表示指向数组第一个元素的指针。


你想写的是:


零食指针

零食+指数0到指针0的索引偏移

*(零食+ 0)对象在索引偏移到该指针

(*(零食+ 0))。品牌指数对象的品牌组件

偏移到该指针


由于定义了数组索引的方式,您可以替换最后一个表达式的部分。


(* (零食+ 0))。品牌相当于


零食[0]。品牌


简而言之:

即使小吃是指针,小吃[0]也不再是指针类型,但已经
数组中的对象。因此,您不能使用 - >,而只需使用''。''操作直接访问组件




-

Karl Heinz Buchegger
kb******@gascad.at


I''m learning C++ from C++ Primer Plus 5th edition. I''m struggling at
the moment with declaring an array of three structures by using ''new'' to
allocate memory. (this is one of the programming exercises at the end
of each chapter). My attempt is as per below. I get a compile error
indicating that -

"base operand of ''->'' has no pointer type ''candybar'' "

// Q-09.cpp -- Question 9
#include <iostream>
struct candybar
{
//char brand[20];
std::string brand; //in this context - must use std::string
float weight;
int calories;
};
int main()
{
using namespace std;
candybar * snack = new candybar[3];

snack[0]->brand = "Mocha Munch"; // above error starts here
snack[0]->weight = 2.3;
snack[0]->calories = 350;

snack[1]->brand = "Crunchie";
snack[1]->weight = 1.9;
snack[1]->calories = 400;

snack[2]->brand = "Mars Bar";
snack[2]->weight = 4.5;
snack[2]->calories = 200;

cout << endl;
cout << "Snack brand : " << snack[0]->brand << endl;
cout << "Snack weight : " << snack[0]->weight << endl;
cout << "Snack calories : " << snack[0]->calories << endl;

cout << "Snack brand : " << snack[1]->brand << endl;
cout << "Snack weight : " << snack[1]->weight << endl;
cout << "Snack calories : " << snack[1]->calories << endl;

cout << "Snack brand : " << snack[2]->brand << endl;
cout << "Snack weight : " << snack[2]->weight << endl;
cout << "Snack calories : " << snack[2]->calories << endl;

delete snack;
return 0;
}

Any help would be appreciated, I really can''t see why this shouldn''t
work. I read the chapter umpteen times but I must be missing something.

Steve Taylor
Melbourne, Australia.

解决方案

"Steven Taylor" <ne******@superjacent.net> wrote in message
news:42***********************@news.optusnet.com.a u

I''m learning C++ from C++ Primer Plus 5th edition. I''m struggling at
the moment with declaring an array of three structures by using ''new''
to allocate memory. (this is one of the programming exercises at
the end of each chapter). My attempt is as per below. I get a
compile error indicating that -

"base operand of ''->'' has no pointer type ''candybar'' "

// Q-09.cpp -- Question 9
#include <iostream>
struct candybar
{
//char brand[20];
std::string brand; //in this context - must use std::string
float weight;
int calories;
};
int main()
{
using namespace std;
candybar * snack = new candybar[3];

snack[0]->brand = "Mocha Munch"; // above error starts here
snack[0]->weight = 2.3;
snack[0]->calories = 350;


Replace all of the -> operators with the . operator.

snack is a pointer, but using the subscript operator dereferences the
pointer. Thus snack[0] is of type candybar, not pointer to candybar.
--
John Carson




John Carson wrote:

"Steven Taylor" <ne******@superjacent.net> wrote in message
news:42***********************@news.optusnet.com.a u

I''m learning C++ from C++ Primer Plus 5th edition. I''m struggling at
the moment with declaring an array of three structures by using ''new''
to allocate memory. (this is one of the programming exercises at
the end of each chapter). My attempt is as per below. I get a
compile error indicating that -

"base operand of ''->'' has no pointer type ''candybar'' "

// Q-09.cpp -- Question 9
#include <iostream>
struct candybar
{
//char brand[20];
std::string brand; //in this context - must use std::string
float weight;
int calories;
};
int main()
{
using namespace std;
candybar * snack = new candybar[3];

snack[0]->brand = "Mocha Munch"; // above error starts here
snack[0]->weight = 2.3;
snack[0]->calories = 350;



Replace all of the -> operators with the . operator.

snack is a pointer, but using the subscript operator dereferences the
pointer. Thus snack[0] is of type candybar, not pointer to candybar.



Thanks for your hlep John, it certainly compiled ok.

Steve.


Steven Taylor wrote:


I''m learning C++ from C++ Primer Plus 5th edition. I''m struggling at
the moment with declaring an array of three structures by using ''new'' to
allocate memory. (this is one of the programming exercises at the end
of each chapter). My attempt is as per below. I get a compile error
indicating that -

"base operand of ''->'' has no pointer type ''candybar'' "

// Q-09.cpp -- Question 9
#include <iostream>
struct candybar
{
//char brand[20];
std::string brand; //in this context - must use std::string
float weight;
int calories;
};
int main()
{
using namespace std;
candybar * snack = new candybar[3];

snack[0]->brand = "Mocha Munch"; // above error starts here



Your problem is here.
It is the way array indexing is defined in C++

a[index] is equivalent to *(a_as_ptr+index)

where a_as_ptr denotes a pointer to the first element of the array.

What you wanted to write was:

snack pointer
snack+0 index offset of index 0 to that pointer
*(snack+0) object at the index offset to that pointer
(*(snack+0)).brand the brand component of the object at the index
offset to that pointer

Due to the way array indexing is defined you can replace parts of the last expression.

(*(snack+0)).brand is equivalent to

snack[0].brand

So in short:
Even if snack is a pointer, snack[0] is no longer of pointer type, but is already
the object in the array. Therefore you don''t use ->, but simply access the component
directly with a ''.'' operation.

--
Karl Heinz Buchegger
kb******@gascad.at


这篇关于帮助动态数组结构。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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