الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

تعريف DataStructur بواسطة C++

بدأه Malik_88 في 1 يناير 2012 · 7 رد · 665 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته اخواني

ياريت تساعدوني كتبت برنامج بمسعدت الأخ بلوتوث لقرائهXML ELMENTS من ملف XML

ألان حاولت تعريف CLASSES لتخزن هذه Elements

نبذه سريعه : For each Component : id+name

and for each component are n-Subcomponent

header.h

#include <string>
#include <iostream>
#include <list>
using namespace std;
#ifndef HEADER_H
#define HEADER_H

class SubComponent;
class Component
{
public:
  int id;
  string name;
  SubComponent *psc;
 list<SubComponent*> pscl;

 list<SubComponent*> getList()
 {
 return pscl;
 }
  void setSubComponent(SubComponent *sub)
  {
      this->psc=sub;
  }



 void  setId(int id)
  {
    this->id=id;
  }

 void  setName(string name)
  {
      this->name=name;
  }
 string getName(){return name;}

  Component(int id,string name)
  {
      this->id=id;
      this->name=name;


  }


};

class SubComponent:public Component
{
public:
    SubComponent(int id,string name):Component(id,name)
    {

        this->id=id;
        this->name=name;
        psc= this;
        pscl.push_back(psc);
    }
};
















#endif

حاولت أطبق على التعرف إلي كتبته ولكن لا استطع التأكد إن كان هذا ما أريد... عن طريق طباعة المعلومات داخل : SubCompoonent

#include "header.h"


using namespace std;
// 2 Component
// with 2 SubComponents

int main(){

Component a = Component(1,"FirstOne");

SubComponent aSub1=  SubComponent(11,"FirstAsUb");
SubComponent aStub2 = SubComponent(12,"SecoundAsUb");

//a.setSubComponent(& aSub1);


//Component b =  Component(2,"SecOne");

cout << a.name<<endl;
cout<<a.id<<endl;


list<SubComponent*> temp =a.getList();



for(list<SubComponent*>::iterator it = temp.begin();it!=temp.end();it++)
{
//    SubComponent sc = (SubComponent) it;
// cout ???!?!?!?


}

return 0;
}
#2

ما هو الفرق بين دور المتغيرين psc و psc1؟

و داخل اللوب لما لا تستخدم

cout << it->getName();

لعرض اﻹسم؟

أيضا أنا ملاحظ أن ال Subcomponent هو نفس ال Component, لما لا تستخدم نفس الكلاس للإثنين؟

1
#3
Blueteeth كتب:

ما هو الفرق بين دور المتغيرين psc و psc1؟

و داخل اللوب لما لا تستخدم

cout << it->getName();

لعرض اﻹسم؟

أيضا أنا ملاحظ أن ال Subcomponent هو نفس ال Component, لما لا تستخدم نفس الكلاس للإثنين؟

شكراً الك أخي على الرد

أنا كنت في الأول أحاول أن اربطSupcomponent لكلComponent ... وبعدين حاولت أن اربط list<Subcomponent>لكل Component

pscl

وهو فقط

pointer

على list>subcomponent>

أنا حاولت استخدم :cout << it->getName();

لكن للأسف ما زبطت : Segmentation Fault

نعم أنا لاحظت هالشي ولكن كف راح أقدر أفرق بينهم أو أقدر أخذنهم في شكل منفصل لأميز subcomponent and component ?

أنا في البرسنج قدرت احدد مينSub/Component ولكن بقي عليه التخزين مع حفظ العلاقات بينهم وهذا إلي حاولت أعملو عن طريق al Pointers

#4
اقتباس
أنا كنت في الأول أحاول أن اربطSupcomponent لكلComponent ... وبعدين حاولت أن اربط list<Subcomponent>لكل Component

pscl

وهو فقط

pointer

على list>subcomponent>

لا أعتقد أنك تحتاج إلا إلى مؤشر على الليست فقط (psc1).

اقتباس
أنا حاولت استخدم :cout << it->getName();

لكن للأسف ما زبطت : Segmentation Fault

أكيد بسبب أن it يؤشر إلى NULL, أفضل أن تستخدم while

اقتباس
نعم أنا لاحظت هالشي ولكن كف راح أقدر أفرق بينهم أو أقدر أخذنهم في شكل منفصل لأميز subcomponent and component ?

أنا في البرسنج قدرت احدد مينSub/Component ولكن بقي عليه التخزين مع حفظ العلاقات بينهم وهذا إلي حاولت أعملو عن طريق al Pointers

ستحتفظ بالعلاقات ما بينهم, ﻷن داخل كلاس component ستستخدم مؤشر إلى ليست من نفس النوع.

class component{
private:
    list<component*> subs;
    ....
}
1
#5

سلام عليكم

مشكور أخي على المساعده :

#include <string>
#include <iostream>
#include <list>
using namespace std;
#ifndef HEADER_H
#define HEADER_H


class Component
{
public:
  int id;
  string name;

 list<Component*> pscl;

 list<Component*> getList()
 {
 return pscl;
 }
  void setSubComponent(Component *sub)
  {
      pscl.push_back(sub);
  }



 void  setId(int id)
  {
    this->id=id;
  }
 int getID(){return id;}

 void  setName(string name)
  {
      this->name=name;
  }
 string getName(){return name;}

  Component(int id,string name)
  {
      this->id=id;
      this->name=name;


  }
  Component(){}


};

#include "header.h"


using namespace std;

// 2 Component
// with 2 SubComponents

int main(){

Component a = Component(1,"FirstOne");

Component aSub1=  Component(11,"FirstSub");
Component aStub2 = Component(12,"SecoundSub");


a.setSubComponent(&aSub1);
a.setSubComponent(&aStub2);







list<Component*> temp =a.getList();



list<Component*>::iterator it = temp.begin();
while(it != temp.end()) {




    cout <<it->getName()<<endl;



  it++;


}




return 0;
}

يظهر الخطء التالي .... بس أنا إتأكدت إنه الست فيها عنصرين

main.cpp: In function ‘int main()’:
main.cpp:36:16: error: request for member ‘getName’ in ‘* it.std::_List_iterator<_Tp>::operator-> [with _Tp = Component*, std::_List_iterator<_Tp>::pointer = Component**]()’, which is of non-class type ‘Component*’
#6

ال iterator عبارة عن مؤشر يشير على مؤشر آخر من النوع component* و لذلك لكى تستخدم دوال ال component يجب عليك عمل dereference أولا

(*it) -> getName()
#7

يسلمو اديك يا أمير أنا حاولت قبل هيك مع ال-* ولكن نست القوسين ... ما شاء الله عنك الأن ظهرت عندي المشكله الثانيه في البرسر

// list to save the Components
 list<Component> listComponent;
//list<Component> subList;


void parse_element(TiXmlElement* e){
    Component component1 = Component();
    int id;
    string  name,nameOfelem;

    nameOfelem =(string) (e->Value()); // castring to string

  if(nameOfelem=="Component"){
// copying  the attributes from the element e (in our case Model)


    name=e->Attribute("name");
    id = atoi(e->Attribute("id"));

// Objects creating
    Component component1 = Component(id,name);
cout << " COMP"<<endl;
    // store the object in the list

 listComponent.push_back(component1);



 // Iterate through children if it has any and make as befor
      if (!e->NoChildren()){

        TiXmlElement* curr_child =(e->FirstChildElement());

        do{

            parse_element(curr_child);

            TiXmlNode* next_child= e->IterateChildren(curr_child);
            curr_child = next_child? next_child->ToElement() : NULL;
          }while(curr_child);

      }// end of if(!e->noChildren)
}else
  {if (nameOfelem == "Subcomponent")

      {
          id=atoi(e->Attribute("id"));
          name=e->Attribute("name");
          Component sub1=Component (id,name);

 component1.setSubComponent(&sub1)
 cout << " SUB"<<endl;
       ,this component its not what i created with the rigths value , its the component with the initial values, i tryed to make "tempCom" ...but dosnot work!;  // almshkleh hooooon


      }
  }
}// end parser

المشكله إنهsubcomponent ما بتخزنو في الكمبننت الحاليه وإنما في الأولى (الابتدائيه)

إن شاء الله تكون فهمتني أخي الكريم وبارك الله فيك على مجهودك ومساعدتك

#8

لأن الدالة parse_element() لا تستقبل أي معلومات عن ال component الأب فبالتالى لا تطبق العلاقة بشكل صحيح بين الأب و أبناؤه. لعلاج المشكلة يجب أن تمرر للدالة مؤشر على الأب الذى ستضيف إليه ال component الحالى التى تقوم بعمل parsing له.

مواضيع مشابهة