这样编码问题 [英] Coding Problems this way

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

问题描述

我有两个成员结构,一个是日期属性,第二个是bookrecord属性,我想知道当我们为class.im创建该类型的对象时,如何从main中访问它们,im正确与否以及代码中的正确位置我应该在构造函数中为链接列表指定大小(在此处声明大小和初始化的变量.有点困惑.

i have two structs for members one for date attributes and second for bookrecord attributes i want to know how they wil be accessible from main when we create objects of that type for class.im going right way or not and where in my code i should specify the size for linklist in constructor(where to declare variable for size and intialize.a little bit confused.

class singlelink

{
  Private:
  struct date
  {
      char month[8]; 
      int day;
      int year;
  };
  struct bookrecord
  {
    char title[60];
    char name[50];
    date pub;
    string status;
    int num_ofcopies;
    Bookrecord *link;
  }*first;

  Public:
  singlelink()
  {
    first=NULL;
  }
}

推荐答案

首先:C ++是一种形式语言,区分大小写. Public Private (您编写它们的方式)不是关键字,并且Bookrecord 在任何地方都没有声明(但是有bookrecord:您输入错字了吗?)

然后:singlelink 是一个包含指针(first)的类,并且bookrecord 也包含一个指针(link).
我是否假设您希望siglelist 成为通过链接book-record -s形成的列表的管理者?
就重用而言,这不是一个很好的设计,但是从头开始是可行的.

现在,确定singlelink初始化为无链接"的构造函数,但是现在您必须添加更多方法:

-至少您需要和addrecord配合,将bookrecord*推入列表的最前面(指针保持的工作方式是您练习的一部分...)
-那么您需要一个析构函数(~singlelink),该析构函数将删除所有bookrecord 链,并逐个删除bookrecord .
-假设您不需要在它们之间复制和分配singlelist -s,则必须禁用复制和分配操作(只需声明singlelink(const singlelink&)和 singlelinkl& operator=(const singlelink&)为私有).

-到那时,在您的主目录中,只创建一个singlelink变量,而不是使用new创建任何singlelink :: bookrecord并将其指针指向您的add方法.

现在,您的链条是一致的.要访问它,您的单个链接必须有一个方法hat返回一个bookrecord*(链的头部)和一个bookrecord 方法,该方法将bookrecord*返回到下一个.
或者,或者,您需要一个迭代器类(但这是更高级的东西,您尚未达到适当的水平)
First: C++ is a formal language, and it is case sensitive. Public and Private (the way you wrote them) aren''t keywords, and Bookrecord is not declared anywhere (but there is bookrecord: did you made a typo?)

Then: singlelink is a class that contain a pointer (first) and bookrecord contains a pointer as well (link).
Have I to assume you want siglelist to be the manager of the list formed by chaining book-record-s?
Not a good design in term of reuse, but for a start can work.

Now, OK the constructor for singlelink initializing as "linking nothing", but now you''ve to add some more methods:

-at least you need and addrecord, taking a bookrecord* tha pushes the bookrecord in front of the list (how pointer shold work, is part of your exercise...)
-then you need a destructor (~singlelink) that deletes all the bookrecord chain, deleting the bookrecord one by one.
-assuming you don''t need to copy and assign singlelist-s between them, you have to disable the copy and assign operations (just declare singlelink(const singlelink&) and singlelinkl& operator=(const singlelink&) as private).

-At that point, in you main, just create a singlelink variable, than create whatever singlelink::bookrecord with new and give their pointer to your add method.

Right now, you have a consistent chain. To access it, your single link must have a method hat returns a bookrecord* (the head of the chain) and a bookrecord method that return the bookrecord* to the next.
Or, alternatively, you need an iterator class (but that''s a more advanced thing, you are not yet at the proper level)


C ++是区分大小写的语言;因此您的代码不会仅仅因为大小写错误而编译.还有其他导致编译失败的原因.末尾的;"在哪里?这段代码不是C ++的大部分;它什么也没做.指定链表大小的想法(是节点数还是什么?无论如何,无论如何都没有意义)是完全错误的.即使支持,如果类成员中当前集合的大小合适,您仍需要在每个事务中动态增加/减少此数字(添加/插入/删除元素).

—SA
Look, C++ is a case-sensitive language; so you code won''t compile just because of wrong casing. There are other reasons to fail compilation. Where is the '';'' at the end? This code is not very much of C++; and it does nothing. The idea to specify the size of linked list (is it a number of nodes or what? no matter, makes no sense anyway) is totally wrong. Even though the support if the current size of a collection in a class member is good, you need to increment/decrement this number dynamically on each transaction (add/insert/delete an element).

—SA


首先,要清楚您要问或要做什么?仅当您熟悉基本的C ++和指针时,链接列表才易于理解.

结构只是像类的定义.它还需要声明一个对象,以便您可以使用它.

私有访问说明符中的所有内容都不能从类外部访问,甚至不能使用其自己的对象.

这里还有很多要解释的东西,但这将使它成为C ++教程.

解决方案:在Internet上搜索并了解C ++中的结构/类,指针和链表.
First of all, be clear about what you are trying to ask or do? Linked list is simple to understand only if you are familiar with basic C++ and pointers.

A structure is simply a definition like a class. It also needs an object to be declared so that you can use it.

Anything inside Private access specifier cannot be accessed from outside of the class, not even using its own object.

There are lot more things to explain here, but that will make it as a C++ tutorial post.

Solution: Search internet and learn what is structure/class, pointers and linked list in C++.


这篇关于这样编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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