c++ - C++ 类中的 undefined reference

标签 c++ class reference undefined

大家好,我尝试使用两个队列实现一个堆栈,但是,我遇到了一个问题,指出未定义对类 my_queue 的引用

错误是(我只包括了一些,因为所有的都有 undefined reference )

my_stack.cpp:(.text+0x15): undefined reference to `my_queue::my_queue()'
my_stack.cpp:(.text+0x25): undefined reference to `my_queue::my_queue()'
my_stack.cpp:(.text+0x31): undefined reference to `my_queue::my_queue()'
my_stack.cpp:(.text+0x3d): undefined reference to `my_queue::my_queue()'
/tmp/ccNs1Fuy.o: In function `my_stack::push(int)':
 `my_queue::dequeue()'

my_stack 的代码如下。我还有两个 my_stack 类和 my_queue 类的头文件,但我认为它们没有错误。

#include<iostream>
#include<vector>
#include"my_stack.h"
#include"my_queue.h"

my_stack::my_stack(){
    my_queue q1;
    my_queue q2;

}

void my_stack::push(int n){
    q1.enqueue(n);
}

int my_stack::pop(){
    for(int i =0; i<q1.size()-1; i++){
        int temp = q1.dequeue();
        q2.enqueue(temp);
    }//dequeue every element in the q1 except the last element and enqueue to q2
    int x = q1.dequeue();// dequeue the last item of q1


    return x;
}

void my_stack::is_empty(){
    if(q1.size() == 0){
    std::cout<< "the stack is empty"<<'\n';
    }

    else{
    std::cout<< "the stack is not empty"<<'\n';
    }
}

int main(){
    my_stack m;
    m.push(4); 
    m.push(3);
    int yo = m.pop();
    std::cout<<yo<<'\n';
    m.is_empty();

return 0;
}

谢谢你们,很抱歉提出没有组织的问题。

最佳答案

因为我们看不到头文件,所以我在这里做了一个假设,但看到评论似乎您的链接器错误可能是由于未实现的函数造成的。

我假设您的 header “my_queue.h”包含声明而不是实现。您似乎缺少的是函数的实现,这就是您遇到链接器错误的原因。 你有一些 my_queue.cpp 吗?如果是这样,那么您应该链接它,因为否则您的程序不知道这些功能是实现的,但预计会使用它们。这是联动错误的原因

关于c++ - C++ 类中的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001044/

相关文章:

c++ - 类型推导失败 : shared_ptr of derived template to base template as function argument

c++ - 没有 "using"的模板基类的访问属性

c# - C# 中的命名空间类冲突 - 寻找建议

javascript - 在 for 循环中运行类中的方法

c++ - map 和 unordered_map 包含指向 double 的指针?

c++ - C++ 线程类中的错误导致它在退出时消耗 100% CPU

c++ - 嵌套类对象的工作?

class - scala类的类型参数中的冒号是什么

c++ - 为什么此类中的复制构造函数、operator= 和引用存在编译问题

c++ - 创建右值并通过转发将其传递给函数