עבור לתוכן

בעיה בקימפול בc++ .

Featured Replies

פורסם

אני לא מבין את הודעת השגיאה , אני לומד עכשיו |Template ווהוספתי סה"כ את התוספת שלו לתוכנית קיימת שכתבתי על class פשוט

וזה ההודעה :

g++ -ansi -Wall -pedantic -error -g *.cc -o out

Undefined first referenced

symbol in file

std::basic_istream<char, std::char_traints<char> >& operator>><int>(std::basic_istream<char, std::char_traints<char> >&, stack<int>&)/var/tmp//ccYIZ3Cd.o

std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, stack<int> const&)/var/tmp//ccYIZ3Cd.o

ld: fatal: Symbol referencing errors. No output written to out

collect2: ld returned 1 exit status

אשמח אם מישהו יכול לעזור לי ...הנה הheder file שלי :

#ifndef Stack_h

#define Stack_h

#include <iostream>

using std::ostream;

using std::istream;

enum Result {Success,Fail};

template <class t>

class stack

{

private:

t *array;

int size, top_index;

void copy (const stack& s);

void clear();

public:

Result push(t &e);

Result pop ();

Result top (t &e) const;

Result print()const; //The const is for Not changing the class object the privet fileds

int get(t i) const {return array;} //ask leon

int Size() const { return size;}

int Top_index()const {return top_index;}

stack();

stack(int size);

stack(const stack& s);

~stack();

// Operators of stack

stack& operator=(const stack& s);

stack& operator+=(const stack& s);

};

template <class t>

ostream& operator<<(ostream& o,const stack<t>& s);

template <class t>

istream& operator>>(istream& is,stack<t>& s);

template <class t>

stack<t> operator+(const stack<t>& s1,const stack<t>& s2);

template <class t>

bool operator==(const stack<t>& s1,const stack<t>& s2);

template <class t>

stack<t>& stack<t>::operator=(const stack<t>& s)

{

if (this==&s)

return *this;

delete [] array;

array=new t[s.size];

int i;

for (i=0;i<s.size;i++)

array=s.array;

size=s.size;

top_index=s.top_index;

return *this;

}

template <class t>

stack<t>& stack<t>::operator+=(const stack<t>& s)

{

//ask leon how to overcmoe it - maybe with stack s1=s and then

int i;

for (i=0;i<s.size;i++)

array+= s.array;

return *this;

}

template <class t>

stack<t>::stack(): size(5), top_index(0)

{

array=new t;

}

template <class t>

stack<t>::stack(int s){

array=new t;

top_index=0;

size=s;

}

template <class t>

stack<t>::~stack(){

delete [] array;

}

template <class t>

stack<t>::stack(const stack<t>& s):

size(s.size),top_index(s.top_index)

{

int i;

array=new t;

for (i=0;i<size;i++)

array=s.array;

}

template <class t>

Result stack<t>::push(t &e)

{

if (top_index==size)

return Fail;

array[top_index]=e;

top_index++;

return Success;

}

template <class t>

Result stack<t>::top(t &e) const

{

e=array[top_index-1];

return Success;

}

template <class t>

Result stack<t>::print()const

{

int i;

for( i=0;i<size;i++){

if (i==0)

cout<<"The Array is:\n";

cout<<array<<" ";

}

cout << "\n";

return Success;

}

template <class t>

void stack<t>::copy(const stack<t>& s){

size=s.size;

top_index=s.top_index;

int i;

for (i=0; i<size; i++)

array=s.array;

}

#endif

מוזר שהקומפיילר לא אומר באיזה קובץ השגיאה.

תודה מראש.

פורסם

לצערי לא למדתי תבניות אבל מהתבוננות ראשונית כדאי להחליף את:

using std::ostream;
using std::istream;

ב-

using namespace std;

פורסם

אין קשר ל- namespace, ודווקא עדיף כמו שאתה (Mnm972) כתבת, כדי לא ללכלך את ה- scope שלך.

הבעיה היא שהכרזת על האופרטורים >> ו- << אבל לא הגדרת אותם.

האמת גם את == ו- + לא ראיתי שהגדרת, אבל יכול להיות שאתה לא מקבל שגיאה עליהם כי אתה לא מנסה להשתמש בהם.

פורסם
  • מחבר

תודה רבה נראה לי שזה באמת זה ..!!!

למרות שזה מוזר המימוש באמת בקובץ אחר... *.cc מן הסתם ,

אבל זה הרעיון במודלריות לא קובץ H בנפרד מקובץ C , אבל אני אבדוק את זה תודה לך .

מיקי.

פורסם

אין קשר ל- namespace, ודווקא עדיף כמו שאתה (Mnm972) כתבת, כדי לא ללכלך את ה- scope שלך.

זה משהו שקשור בתבניות? :s05:

פורסם
  • מחבר

אכן עובד תודה רבה .

רק לשים לתת דגל -pedantic-errors צריך להיות אחד אחרי השני בלי רווח, זה עשה הרבה צרות !!

לא ניתן לקימפול בלי זה.

ביי תודה.

פורסם
אבל זה הרעיון במודלריות לא קובץ H בנפרד מקובץ C

זה קצת בעייתי מבחינת מימוש (של הקומפיילר), ולכן בחלק גדול מהקומפיילרים (הישנים בעיקר) חייבים לממש את הפונקציות בקובץ ה- header.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים