Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对类外声明的函数的未定义引用_C++_Oop_Queue - Fatal编程技术网

C++ 对类外声明的函数的未定义引用

C++ 对类外声明的函数的未定义引用,c++,oop,queue,C++,Oop,Queue,我们刚刚开始OOP,但由于突然休假,我们得到了一个作业,其中许多概念没有像模板那样教,所以我仍然怀疑我是否正确使用了它。 这是我的第一个问题,如果我的格式不正确,很抱歉 #include<iostream> using namespace std; #include<string> //global error flag for dequeue bool ERR_Flag = false; template<class T> class queue {

我们刚刚开始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;
            }
        }
    }
}
#包括
使用名称空间std;
#包括
//用于出列的全局错误标志
bool ERR_标志=false;
模板
类队列
{
受保护的:
内锋;
内部后部;
国际能力;
T*ele;
公众:
//构造函数来分配内存和初始化数据成员
队列();
bool是空的();
bool已满();
//插入前检查队列是否已满
//如果队列已满,则返回false
//插入元素,否则返回true
bool排队(T数据);
//删除元素并返回的函数
T退出队列();
~queue();
作废打印();
};
模板
deque类:公共队列
{
公众:
布尔推回(T数据);
bool-push_-Front(T数据);
T pop_Front();
T弹回();
};
模板
队列::队列()
{
正面=-1;
后部=-1;
容量=5;
int ele[5];
}
模板
bool队列::isempty()
{
如果(前部==-1和后部==-1)
{
返回1;
}
其他的
{
返回0;
}
}
模板
bool队列::isfull()
{
如果(后部=容量-1)
{
返回1;
}
其他的
{
返回0;
}
}
模板
bool队列::排队(int数据)
{
如果(前部==-1和后部==-1)
{
前端++;
后++;
ele[后部]=数据;
返回1;
}
否则如果(后==容量-1)
{
后++;
ele[后部]=数据;
返回1;
}
其他的
{
返回0;
}
}
模板
int queue::dequeue()
{
如果(前==-1)
{
ERR_标志=0;
}
其他的
{
INTA;
a=电子[前];
返回a;
前面--;
}
}
模板
无效队列::打印()
{
cout>d_选择;
如果(d_选项==1)
{
而(1)
{
cin>>op_选择;
如果(op_选项==1)
{
if(q.isempty())
不能您正在使用从
main
中的
queue
继承的
deque
。因此,当您调用例如
d.isempty()
时,您正在调用
queue
类的成员函数

但是您只为
队列::is_empty()
定义了一个定义,而不是为
队列::is_empty()
定义了一个定义,这是错误消息告诉您的

其他错误消息也有类似的原因

首先,为什么要使用显式专门化?只需为所有类型定义成员函数
T
,例如:

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

您正在使用从
main
中的
queue
继承的
deque
。因此,当您调用例如
d.isempty()
时,您正在调用
queue
类的成员函数

但是您只为
队列::is_empty()
定义了一个定义,而不是为
队列::is_empty()
定义了一个定义,这是错误消息告诉您的

其他错误消息也有类似的原因

首先,为什么要使用显式专门化?只需为所有类型定义成员函数
T
,例如:

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

等等。

这是一个链接器错误。你是如何编译你的程序的?
使用namespace std;
是你的大错误。@S.M.在第一次查看时,这似乎是一个大问题,很容易成为一个问题,但这不是OP出现特定错误的原因。在这种情况下,请参阅我的答案。看起来好像有人已经回答了你的问题,但我注意到了在队列::isfull中,您编写了“if(rear=capacity-1)”,但您想要==there@ScottM代码还有多个问题。例如,某些声明为非空的函数并不总是返回值(例如,
int queue::dequeue()
)@OP用
-Wall-Wextra
标志编译你的程序,你会收到许多警告消息。修复所有这些!这是一个链接器错误。你是如何编译你的程序的?
使用名称空间std;
是你的大错误。@S.M.第一次查看时,这似乎是一个大问题,很容易成为一个问题,但这不是你的原因或者OP在本例中的特定错误,请参阅我的答案。看起来有人已经回答了您的问题,但我在查看您的代码时注意到了一些问题。在队列::isfull中,您编写“if(rear=capacity-1)”,但您想要==there@ScottM代码还有多个问题。例如,某些声明为非空的函数并不总是返回值(例如,
int queue::dequeue()
)等。@OP使用
-Wall-Wextra
标志编译您的程序,您将收到许多警告消息。修复所有这些!啊,我不知道如何使用模板。我现在已经修复了它,正如我上面提到的,除了函数声明外,我无法更改任何内容,因为已经提供了函数定义和main,所以我无法使用命名空间std;进行更改。在为t定义成员函数后,类deque似乎没有从类队列继承。@KaranRamalingam有关您在问题中提到的新错误,请参阅,即您必须在派生类中编写
this->front
而不是
front
,才能访问从属类的成员t基类。啊,我不知道如何使用模板。我现在已经修复了它,正如我前面提到的,除了函数的声明之外,我不能更改任何东西,因为已经提供了函数定义和main,所以我不能使用namespace std;进行更改。在为t定义成员函数之后,类deque似乎没有得到In从类队列中写入。@KaranRamalingam有关您在问题中提到的新错误,请参阅,即您必须在派生类中写入
this->front
,而不是
front
,才能访问我
> 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];
```                 ^~~~

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 std::cout;
using std::endl;
using std::string;
using std::cin;