Неопределенная ссылка на функцию, объявленную вне класса - PullRequest
0 голосов
/ 28 марта 2020

Мы только что начали OOP, но из-за внезапного отпуска мы получили задание, в котором многие понятия не преподавались как шаблоны, поэтому я все еще скептически отношусь к тому, правильно ли я его использовал или нет. И это мой первый вопрос здесь, так что извините, если мое форматирование неверно.

#include<iostream>
using namespace std;
#include<string>
//global error flag for dequeue
bool ERR_Flag = false;
template<class T>
class queue
{
    protected:
    int front;
    int rear;
    int capacity;
    T *ele;
    public:
    //constructor to allocate memory and initialize data members
    queue();
    bool isempty();
    bool isfull();
    //Check if queue is full before insertion
    //if queue is full return false
    // insert element and return true otherwise
    bool enqueue(T data);
    //funtion to remove an element and return
    T dequeue();
    ~queue();
    void print();
};
template<class T>
class deque:public queue<T>
{
    public:
    bool push_Back(T data);
    bool push_Front(T data);
    T pop_Front();
    T pop_Back();
};
template<>
queue<int> :: queue()
{
    front = -1;
    rear = -1;
    capacity = 5;
    int ele[5];
}
template<>
bool queue<int> :: isempty()
{
    if(front == -1 && rear == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
template<>
bool queue<int> :: isfull()
{
    if(rear = capacity - 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
template<>
bool queue<int> :: enqueue(int data)
{
    if(front == -1 && rear == -1)
    {
        front++;
        rear++;
        ele[rear] = data;
        return 1;
    }
    else if(rear == capacity - 1)
    {
        rear++;
        ele[rear] = data;
        return 1;
    }
    else
    {
        return 0;
    }
}
template<>
int queue<int> :: dequeue()
{
    if(front == -1)
    {
        ERR_Flag = 0;
    }
    else
    {
        int a;
        a = ele[front];
        return a;
        front--;
    }
}
template<>
void queue<int> :: print()
{
    cout<<ele[front];
}
template<>
bool deque<string> :: push_Front(string data)
{
    if(front == -1 && rear == -1)
    {
        front = 0;
        rear = 0;
        ele[front] = data;
        return 1;
    }
    else if(rear == capacity - 1)
    {
        return 0;
    }
    else
    {
        for(int i = rear; i >= 0; i--)
        {
            ele[i+1] = ele[i];
        }
        rear++;
        ele[front] = data;
        return 1;
    }
}
template<>
bool deque<string> :: push_Back(string data)
{
    if(front == -1 && rear == -1)
    {
        front = 0;
        rear = 0;
        ele[0] = data;
        return 1;
    }
    else if(rear == capacity - 1)
    {
        return 0;
    }
    else
    {
        rear++;
        ele[rear] = data;
        return 1;
    }
}
template<>
string deque<string> :: pop_Front()
{
    if(front == -1)
    {
        ERR_Flag = 0;
    }
    else
    {
        string s;
        s = ele[front];
        front++;
        return s;
    }
}
template<>
string deque<string> :: pop_Back()
{
    if(front == -1)
    {
        ERR_Flag = 0;
    }
    else
    {
        string s;
        s = ele[rear];
        rear--;
        return s;
    }
}
int main()
{
    int d_Choice;
    int op_Choice;
    deque<string> d;
    queue<int> q;
    cin>>d_Choice;  
    if(d_Choice==1)
    {
        while(1)
        {
            cin>>op_Choice;
            if(op_Choice==1)
            {
                if(q.isempty())
                cout<<"Queue is empty"<<endl;
                else
                cout<<"Queue is not empty"<<endl;
            }
            else if(op_Choice==2)
            {
                if(q.isfull())
                cout<<"Queue is full"<<endl;
                else
                cout<<"Queue is not full"<<endl;
            }
            else if(op_Choice==3)
            {
                int data;
                cin>>data;
                if(!q.enqueue(data))
                cout<<"Queue full insertion not possible";
            }
            else if(op_Choice==4)
            {
                q.dequeue();
                if(ERR_Flag)
                cout<<"Queue is empty";
            }
            else if(op_Choice==5)
            {
                q.print();
            }
            else if(op_Choice==6)
            {
            break;
            }
        }
    }
    else if(d_Choice==2)
    {
        string s_Data;
        while(1)
        {
            cin>>op_Choice;
            if(op_Choice==1)
            {
                if(d.isempty())
                cout<<"Queue is empty"<<endl;
                else
                cout<<"Queue is not empty"<<endl;
            }
            else if(op_Choice==2)
            {
                if(d.isfull())
                cout<<"Queue is full"<<endl;
                else
                cout<<"Queue is not full"<<endl;
            }
            else if(op_Choice==3)
            {
                cin>>s_Data;
                if(!d.push_Back(s_Data))
                cout<<"Queue full insertion not possible";
            }
            else if(op_Choice==4)
            {
                cin>>s_Data;
                if(!d.push_Front(s_Data))
                cout<<"Queue full insertion not possible";              

            }
            else if(op_Choice==5)
            {
                d.pop_Back();
                if(ERR_Flag)
                cout<<"Queue is empty";
            }
            else if(op_Choice==6)
            {
                d.pop_Front();
                if(ERR_Flag)
                cout<<"Queue is empty";
            }
            else if(op_Choice==7)
            {
                d.print();              
            }
            else if(op_Choice==8)
            {
                break;
            }
        }
    }
}

Ошибка:

/tmp/ccw2hdxf.o: In function `main':
main.cpp:(.text+0x6c5): undefined reference to `queue::isempty()'
main.cpp:(.text+0x719): undefined reference to `queue::isfull()'
main.cpp:(.text+0x8df): undefined reference to `queue::print()'
main.cpp:(.text+0x90d): undefined reference to `queue::~queue()'
main.cpp:(.text+0x962): undefined reference to `queue::~queue()'
/tmp/ccw2hdxf.o: In function `deque<std::string>::deque()':
main.cpp:(.text._ZN5dequeISsEC2Ev[_ZN5dequeISsEC5Ev]+0x14): undefined reference to `queue::queue()'
/tmp/ccw2hdxf.o: In function `deque<std::string>::~deque():
main.cpp:(.text._ZN5dequeISsED2Ev[_ZN5dequeISsED5Ev]+0x14): undefined reference to `queue::~queue()'
collect2: error: ld returned 1 exit status

Я могу изменять только объявления функций, так как другие вещи были дано в вопросе.

РЕДАКТИРОВАТЬ: После определения функций-членов для T, я получаю следующие ошибки. И я не могу ничего изменить, кроме объявления для функций-членов, так как определения для функций-членов и основной функции уже предоставлены. КОД:

#include<iostream>
using namespace std;
#include<string>
//global error flag for dequeue
bool ERR_Flag = false;
template<class T>
class queue
{
    protected:
    int front;
    int rear;
    int capacity;
    T *ele;
    public:
    //constructor to allocate memory and initialize data members
    queue();
    bool isempty();
    bool isfull();
    //Check if queue is full before insertion
    //if queue is full return false
    // insert element and return true otherwise
    bool enqueue(T data);
    //funtion to remove an element and return
    T dequeue();
    ~queue();
    void print();
};
template<class T>
class deque:public queue<T>
{
    public:
    bool push_Back(T data);
    bool push_Front(T data);
    T pop_Front();
    T pop_Back();
};
template<typename T>
queue<T> :: queue()
{
    front = -1;
    rear = -1;
    capacity = 5;
    T ele[5];
}
template<typename T>
bool queue<T> :: isempty()
{
    if(front == -1 && rear == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
template<typename T>
bool queue<T> :: isfull()
{
    if(rear == capacity - 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
template<typename T>
bool queue<T> :: enqueue(T data)
{
    if(front == -1 && rear == -1)
    {
        front++;
        rear++;
        ele[rear] = data;
        return 1;
    }
    else if(rear == capacity - 1)
    {
        rear++;
        ele[rear] = data;
        return 1;
    }
    else
    {
        return 0;
    }
}
template<typename T>
T queue<T> :: dequeue()
{
    if(front == -1)
    {
        ERR_Flag = 0;
    }
    else
    {
        T a;
        a = ele[front];
        return a;
        front--;
    }
}
template<typename T>
void queue<T> :: print()
{
    cout<<ele[front];
}
template<typename T>
bool deque<T> :: push_Front(T data)
{
    if(front == -1 && rear == -1)
    {
        front = 0;
        rear = 0;
        ele[front] = data;
        return 1;
    }
    else if(rear == capacity - 1)
    {
        return 0;
    }
    else
    {
        for(int i = rear; i >= 0; i--)
        {
            ele[i+1] = ele[i];
        }
        rear++;
        ele[front] = data;
        return 1;
    }
}
template<typename T>
bool deque<T> :: push_Back(T data)
{
    if(front == -1 && rear == -1)
    {
        front = 0;
        rear = 0;
        ele[0] = data;
        return 1;
    }
    else if(rear == capacity - 1)
    {
        return 0;
    }
    else
    {
        rear++;
        ele[rear] = data;
        return 1;
    }
}
template<typename T>
T deque<T> :: pop_Front()
{
    if(front == -1)
    {
        ERR_Flag = 0;
    }
    else
    {
        T s;
        s = ele[front];
        front++;
        return s;
    }
}
template<typename T>
T deque<T> :: pop_Back()
{
    if(front == -1)
    {
        ERR_Flag = 0;
    }
    else
    {
        T s;
        s = ele[rear];
        rear--;
        return s;
    }
}
int main()
{
    int d_Choice;
    int op_Choice;
    deque<string> d;
    queue<int> q;
    cin>>d_Choice;  
    if(d_Choice==1)
    {
        while(1)
        {
            cin>>op_Choice;
            if(op_Choice==1)
            {
                if(q.isempty())
                cout<<"Queue is empty"<<endl;
                else
                cout<<"Queue is not empty"<<endl;
            }
            else if(op_Choice==2)
            {
                if(q.isfull())
                cout<<"Queue is full"<<endl;
                else
                cout<<"Queue is not full"<<endl;
            }
            else if(op_Choice==3)
            {
                int data;
                cin>>data;
                if(!q.enqueue(data))
                cout<<"Queue full insertion not possible";
            }
            else if(op_Choice==4)
            {
                q.dequeue();
                if(ERR_Flag)
                cout<<"Queue is empty";
            }
            else if(op_Choice==5)
            {
                q.print();
            }
            else if(op_Choice==6)
            {
            break;
            }
        }
    }
    else if(d_Choice==2)
    {
        string s_Data;
        while(1)
        {
            cin>>op_Choice;
            if(op_Choice==1)
            {
                if(d.isempty())
                cout<<"Queue is empty"<<endl;
                else
                cout<<"Queue is not empty"<<endl;
            }
            else if(op_Choice==2)
            {
                if(d.isfull())
                cout<<"Queue is full"<<endl;
                else
                cout<<"Queue is not full"<<endl;
            }
            else if(op_Choice==3)
            {
                cin>>s_Data;
                if(!d.push_Back(s_Data))
                cout<<"Queue full insertion not possible";
            }
            else if(op_Choice==4)
            {
                cin>>s_Data;
                if(!d.push_Front(s_Data))
                cout<<"Queue full insertion not possible";              

            }
            else if(op_Choice==5)
            {
                d.pop_Back();
                if(ERR_Flag)
                cout<<"Queue is empty";
            }
            else if(op_Choice==6)
            {
                d.pop_Front();
                if(ERR_Flag)
                cout<<"Queue is empty";
            }
            else if(op_Choice==7)
            {
                d.print();              
            }
            else if(op_Choice==8)
            {
                break;
            }
        }
    }
}

ОШИБКА:

> main.cpp: In member function ‘bool deque<T>::push_Front(T)’:
> main.cpp:113:8: error: ‘front’ was not declared in this scope
     if(front == -1 && rear == -1)
        ^~~~~
>main.cpp:113:23: error: ‘rear’ was not declared in this scope
     >if(front == -1 && rear == -1)
                       ^~~~
>main.cpp:117:9: error: ‘ele’ was not declared in this scope
         >ele[front] = data;
         ^~~
>main.cpp:120:21: error: ‘capacity’ was not declared in this scope
     >else if(rear == capacity - 1)
                     ^~~~~~~~
>main.cpp:128:13: error: ‘ele’ was not declared in this scope
             >ele[i+1] = ele[i];
             ^~~
>main.cpp:131:9: error: ‘ele’ was not declared in this scope
         >ele[front] = data;
         ^~~
>main.cpp: In member function ‘bool deque<T>::push_Back(T)’:
>main.cpp:138:8: error: ‘front’ was not declared in this scope
     >if(front == -1 && rear == -1)
        ^~~~~
>main.cpp:138:23: error: ‘rear’ was not declared in this scope
     >if(front == -1 && rear == -1)
                       ^~~~
>main.cpp:142:9: error: ‘ele’ was not declared in this scope
         >ele[0] = data;
         ^~~
>main.cpp:145:21: error: ‘capacity’ was not declared in this scope
     >else if(rear == capacity - 1)
                     ^~~~~~~~
>main.cpp:152:9: error: ‘ele’ was not declared in this scope
         >ele[rear] = data;
         ^~~
>main.cpp: In member function ‘T deque<T>::pop_Front()’:
>main.cpp:159:8: error: ‘front’ was not declared in this scope
     >if(front == -1)
        ^~~~~
>main.cpp:166:13: error: ‘ele’ was not declared in this scope
         >s = ele[front];
             ^~~
>main.cpp: In member function ‘T deque<T>::pop_Back()’:
>main.cpp:174:8: error: ‘front’ was not declared in this scope
     >if(front == -1)
        ^~~~~
>main.cpp:181:13: error: ‘ele’ was not declared in this scope
         >s = ele[rear];
             ^~~
>main.cpp:181:17: error: ‘rear’ was not declared in this scope
         >s = ele[rear];
```                 ^~~~

1 Ответ

2 голосов
/ 28 марта 2020

Вы используете deque<string>, который наследуется от queue<string> в main. Поэтому, когда вы вызываете, например, d.isempty(), вы вызываете функцию-член класса queue<string>.

Но вы определили определение только для queue<int>::is_empty(), а не для queue<string>::is_empty(), что и является сообщение об ошибке говорит вам.

Другие сообщения об ошибках имеют аналогичные причины.

Почему вы используете явные специализации в первую очередь? Просто определите функции-члены для всех типов T, например:

template<typename T>
bool queue<T> :: isempty()
{
    if(front == -1 && rear == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

вместо

template<>
bool queue<int> :: isempty()
{
    if(front == -1 && rear == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Как упоминалось в комментариях к вопросу, using namespace std; также может легко вызвать проблемы здесь. Уже есть шаблоны классов с именами queue и deque в пространстве имен std::, и если они будут включены, станет неоднозначным, относятся ли эти имена к вашим классам или к тем, которые указаны в std::. Не используйте using namespace std; и импортируйте только те имена, которые вам нужны, с объявлениями using, например:

using std::cout;
using std::endl;
using std::string;
using std::cin;

и т. Д.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...