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

About Template In Class Of Struct

بدأه العُبد في 20 مارس 2008 · 10 رد · 1,206 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

أحبائي مرحباً بكم، لدي مشكلة في إعداد برنامج يتعلق الأمر في كود الmain

لدي class of array list وفيه template أريد أن أستضيف من خلاله

struct car
{
	int model;
	long number;
	string brand;
	double mialage;
	double price;
};

وهذا هو الصف:

template <class T>
class ArrayListType 
{
	public:
		void insert(const T & item);
		void remove(const T & item);
		void retreive(int loc, T & item);
		void printAll();
		void printOne();
		bool search(const T & item);
		bool CheckSameBrand(const T & item);
		ArrayListType();
		~ArrayListType();

	private:
		int maxsize;
		int length;
		T *list;
};

جميل :lol: .. انزين أنا كتبت الfunctions بس لما أروح على main functin

وأريد أعمل insert كالتالي يظهر لي خطأ

car c;
				cout << "please Input the Information now:" << endl;
				cout << "car model: ";
				cin >> c.model;
				cout << "car number: ";
				cin >> c.number;
				cout << "car brand: ";
				cin >> c.brand;
				cout << "car mialage: ";
				cin >> c.mialage;
				cout << "car price: ";
				cin >> c.price;

				ArrayListType<car> add;
				add.insert(c);

الخطأ إلي يظهر هو: :wacko:

 error C2784: 'bool __cdecl std::operator ==(const class std::basic_string<_E,_Tr,_A> &,const _E *)' : could not deduce template argument for 'const class std::basic_string
<_E,_Tr,_A> &' from 'struct car'
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'

 error C2784: 'bool __cdecl std::operator ==(const _E *,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'const  *' from 'struct car'
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'

 error C2784: 'bool __cdecl std::operator ==(const class std::basic_string<_E,_Tr,_A> &,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument fo
r 'const class std::basic_string<_E,_Tr,_A> &' from 'struct car'
		while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
 error C2784: 'bool __cdecl std::operator ==(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std:
:allocator<_Ty> &' from 'struct car'
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
 error C2784: 'bool __cdecl std::operator ==(const class std::istreambuf_iterator<_E,_Tr> &,const class std::istreambuf_iterator<_E,_Tr> &)' : could not deduce template arg
ument for 'const class std::istreambuf_iterator<_E,_Tr> &' from 'struct car'
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
error C2784: 'bool __cdecl std::operator ==(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not d
educe template argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'struct car'
		c:\program files\microsoft visual studio\vc98\include\xstring(111) : while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
 error C2784: 'bool __cdecl std::operator ==(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct st
d::pair<_T1,_T2> &' from 'struct car'
		c while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
 error C2676: binary '==' : 'struct car' does not define this operator or a conversion to a type acceptable to the predefined operator
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
 warning C4305: 'return' : truncation from 'const int' to 'bool'
		 while compiling class-template member function 'bool __thiscall ArrayListType<struct car>::search(const struct car &)'
Error executing cl.exe.

assginment_2.obj - 8 error(s), 2 warning(s)

فأرجو المساعدة يا أخوان ما هي الطريقة الصحيحة لتخزين الstruct داخل الـ list

وكيف يمكن للإتصال بالفنكشن من دون حدوث هذه الأخطاء ؟!

شاكر لكم ومقدر :wub:

#3

ماادري اذا هالدرس راح يفيدك ولا لا

/index.php?showtopic=106884

llback.jpg

اشهد ان لا إله إلا الله وان محمدا ً رسول الله

#4

اي ممكن ليش لا :wub:

هذه الأكود إلي طلبتها..

template <class T>
void ArrayListType<T>::insert (const T & item)
{
	int loc;

	if (length == 0)
		list[length++] = item;
	else
		if(length == maxsize)
			cout << "Cannot insert in a full list." << endl;
		else
		{
			loc = search(item);
			if (loc == -1)
				list[length++] = item;
			else
				cout << "The item to be inserted is already in the list"
						"No duplicates are allowed." << endl;
		}
}

template <class T>
bool ArrayListType<T>::search (const T & item)
{
	bool found;
	found = false;

	for (int loc=0; loc < length; loc++)
		if(list[loc] == item)
		{
			found = true;
			break;
		}

		if(found)
			return loc;
		else
			return -1;

}

أنا ما أعتقد أن المشكلة من هذه الأكواد لأن لا يظهر لي المترجم أي خطأ

الخطأ يظهر أثناء إضافة object للصف

ثم هذا object لا يقبل إضافة أي function من الpublic ولكن البرايفت يقبل

بس قبوله بالبرايفت ما يفيد لأنه ما عنده تصاريح للتحكم بالبرايفت

الخطأ في طريقتي لهذه الإضافة:

ArrayListType<car> add;
add.insert(c)

أستطيع أن أضيف

ArrayListType<car> add();

code]

وفي هذه الحالة راح يشغل لي البنائة ..

فما هي الطريقة الصحيحة للاتصال بالfunction ؟! وهل له علاقة بال const أو المشكلة من كما يذكر الكمبايلر لا يستطيع تحويل الc - إلي هو object من struct car - إلى array of list ؟! يا ريت أحد يقدر ايجاوبني

شاكر ومقدر

#5
time1 كتب:
ماادري اذا هالدرس راح يفيدك ولا لا

/index.php?showtopic=106884

أشكرك أخي على الدرس الرائع ولكن لا توجد فيه طريقة استضافة struct عبر:

 template<class T>

بحيث انحول عناصر struct إلي دخلها اليوزر إلى داخل list أقصد array موجودة في class

كما ذكرت لك المشكلة التي عندي في الاتصال بالpublic functions الخاصة بالكلاس لما أطرش لها object من نوع struct car كما ورد بالأعلى ..

تم تعديل هذه المشاركة بواسطة العُبد في 20 مارس 2008 في 15:59

#6

السلام عليكم,

أه إنت بتستخدم المعامل == فى مقارنة العنصر, و لكنك مش معرف المعامل دا بالنسبة للعناصر من النوع car.

و لحل هذه المشكلة تعرف المعامل داخل ال struct>

bool operator ==(car& c){
if ( (model != c.model) || (number != c.number)  /*.....till the end of the members....*/ )
    return false;
else
   return true;
}
#7

لحظة أجرب وأشوف

تم تعديل هذه المشاركة بواسطة العُبد في 20 مارس 2008 في 16:44

#8

عزيزي جرب ضفت إلي قلت لي عليه ورحت الmain وكتبت بعد ما يدخل اليوزر البيانات

ArrayListType<car> add;
				add.operator ==(c);

ما طلع لي خطأ أثناء لكن أثناء bulid يطلع هذا الخطأ

Linking...
assginment_2.obj : error LNK2001: unresolved external symbol "public: bool __thiscall ArrayListType<struct car>::operator==(struct car const &)" (??8?$ArrayListType@Ucar@@@@QAE_NABUcar@@@Z)
Debug/assginment_2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

بس على العموم أنا للحين ما عرف ايش فائدة operator == داخل struct أعتقد لازم تكون داخل لclass بعد كذا أروح أتصل فيها من الmain لكن المشكلة كيف أتصل فيه ويش الكود إلي أكتبه حتى يقبل ؟!

أنا أعرف أن أي كلاس أو ستركجر لازم تسوي له object أول

يعني عندي الكلاس

class room
{ 
public:
void print();
room();
private:
int a;
int b;
};

لازم أول حتى أدخل على أي function في الكلاس أول أسوي اوبجكت كذا مثلاً

room ali;
ali.print();
ali.room();
ali.room(4,5);

انزين أنا ألحين عدني نفس لكلاس بس بtemplate

template<class T>
class room
{ 
public:
void print();
room();
private:
int a;
int b;
};

هذا كيف أعمل له اتصال من أي فنكشن بالأخص المين؟

#9

ضبط معي شكراً لك على مجهودك بس يظهر إنذارين ..

#10

متضفش أى حاجة غير الكود اللى أنا كتبهولك دا, لأن مش إنت اللى بتناديه دا الكومبايلر هو اللى بيناديه لما إنت تستخم المعامل == فى statement ال objects فيها من النوع car زى دى>

if(list[loc] == item)

===============================

أما موضوع ال operators فممكن عادى تبقى جوة كلاس أو ستركت مش فارقة (فى السى++ ال class زى ال struct فى كل حاجة "تقريبا") أو ممكن يكون برة كمان كفنكشن مستقلة.

===============================

إيه هما الإنذارين؟

#أسف يا أخت رغد إستعرت بعضا من أسلوبك الجميل فى التنسيق بدون إستئذان :)

#11

ما قصرت أخي عسى الله يوفقك دنيا وآخره،

أنا ضبطت المشكلة كانت عندي مشاكل في بعض الfunction

البرنامج يعمل الآن بنجاح ..

شكراً للجميع.

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