未定义参考[重复]

2024-02-26

当我编译链接列表的代码时,我收到一堆未定义的引用错误。代码如下。我一直在编译这两个语句:

g++ test.cpp 

g++ LinearNode.h LinearNode.cpp LinkedList.h LinkedList.cpp test.cpp  

我真的不明白为什么我会收到这些错误,因为我对 C++ 中的类真的很生疏。我真的需要一些帮助。

线性节点.h:

#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>

using namespace std;

class LinearNode
{
    public:
        //Constructor for the LinearNode class that takes no arguments 
        LinearNode();
        //Constructor for the LinearNode class that takes the element as an argument
        LinearNode(int el);
        //returns the next node in the set.
        LinearNode* getNext();
        //returns the previous node in the set
        LinearNode* getPrevious();
        //sets the next element in the set
        void setNext(LinearNode* node);
        //sets the previous element in the set
        void setPrevious(LinearNode* node);
        //sets the element of the node
        void setElement(int el);
        //gets the element of the node
        int getElement();

    private: 
        LinearNode* next;
        LinearNode* previous;
        int element;        
};//ends the LinearNode class

#endif

线性节点.cpp:

#ifndef LINEARNODE_cpp
#define LINEARNODE_cpp
#include<iostream>
#include"LinearNode.h"

using namespace std;

//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
    next = NULL;
    element = 0;
}//ends LinearNode default constructor

//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
    next = NULL;
    previous = NULL;
    element = 0;
}//ends LinearNode constructor

//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
    return next;
}//ends getNext function

//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
    return previous;
}//ends getPrevious function

//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
    next = node;
}//ends the setNext function

//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
    previous = node;
}//ends the setPrevious function

//returns element of the node
int LinearNode::getElement()
{
    return element;
}//ends the getelement function

//sets the element of the node
void LinearNode::setElement(int el)
{
    element = el;
}//ends the setElement function

#endif

链接列表.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
#include"LinearNode.h"

using namespace std;

class LinkedList
{
    public:
        LinkedList();
        void add(int element);
        int removie (int element);

    private:
        int count;
        LinearNode *contents;
};//ends the class linked list

#endif

链表.cpp:

#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP

#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"

using namespace std;

//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
    count = 0;
    contents = NULL;
}//ends the constructor

//adds an element to the front of the linked list
void LinkedList::add(int element)
{
    int found = 0, current = 0;

    while( (found == 0) && (current !=count) )
    {
        if (contents.getElement() == element)
            found = 1;
        else    
        {
            contents = contents.getNext();
            current++;
        }//ends the else statement
    }//ends the while loop

    if (found == 0)
    {
        LinearNode node = new LinearNode(element);
        node.setNext(contents);
        contents.setPrevious(node);
        count++;
    }//ends the found == 0 if statment
}//ends the add function

//this function removes one element from the linked list.
int LinearNode::remove(int element)
{
    int found = 0;

    if (count == 0)
        cout << "The list is empty" << endl;
    else 
    {
        if (contents.getElement() == element)
        {
            result = contents.getElement();
            contents = contents.getNext();
        }//ends the contents.getElement() == element
        else 
        {
            previous = contents;
            current = contents.getNext();
            for (int index = 0; ( (index < count) && (found == 0) )index++)
                if (current.getElement() = element)
                    found = 1;
                else
                {
                    previous = current;
                    current = current.getNext();
                }//ends the else statement 

            if (found == 0)
                cout << "The element is not in the list" << endl;
            else
            {
                result = current.getElement();
                previous.setNext(current.getNext());
            }//ends else statement  

        }//ends the else stamtement

        count--;
    }//ends the else statement of count == 0
    return result;
}//ends the remove function

#endif

测试.cpp:

#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"

using namespace std;

int main()
{

    LinearNode node1, node2, node3, move;
    LinkedList list;    

    node1.setElement(1);
    node2.setElement(2);
    node3.setElement(3);
}   

  1. 通常标头防护用于标头文件(即.h)不适用于源文件(即.cpp ).
  2. 在源文件中包含必要的标准标头和命名空间。

线性节点.h:

#ifndef LINEARNODE_H
#define LINEARNODE_H

class LinearNode
{
    // .....
};

#endif

线性节点.cpp:

#include "LinearNode.h"
#include <iostream>
using namespace std;
// And now the definitions

链接列表.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

class LinearNode; // Forward Declaration
class LinkedList
{
    // ...
};

#endif

链表.cpp

#include "LinearNode.h"
#include "LinkedList.h"
#include <iostream>
using namespace std;

// Definitions

test.cpp源文件没问题。请注意,头文件永远不会被编译。假设所有文件都在一个文件夹中 -

g++ LinearNode.cpp LinkedList.cpp test.cpp -o exe.out
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未定义参考[重复] 的相关文章

  • 是否有一个好的习惯用法来处理替代输出流?

    我想编写一个简单的程序 根据传递给它的选项 可执行文件将输出打印到屏幕或文件 程序很简单 include
  • 如何使用 VS2022 中的新控制台应用程序模板访问命令行参数

    我想知道如何访问命令行参数 因为这是在Program cs通过 Visual Studio 2022 中控制台应用程序的新模板创建文件 See https aka ms new console template for more infor
  • 在列表中查找匹配项的最简洁方法

    在列表中查找内容的最佳方式是什么 我知道 LINQ 有一些不错的技巧 但我们也可以获取有关 C 2 0 的建议 让我们对这个常见的代码模式进行最佳重构 目前我使用这样的代码 mObjList is a List
  • 如何将异常对象序列化为 xml 字符串

    我想要类似的东西 try code here catch Exception ex stringXML Exception toXML 这样 stringXML 的值就是
  • 为什么在 OpenCV 中访问该矩阵时出现内存错误?

    我只是想写入给定大小的矩阵 当我在 Valgrind 中运行该程序时 出现内存错误 如下所示 主要 cpp include
  • 如何自定义 ASP.Net Core 模型绑定错误?

    我只想从我的 Web API Asp net Core 2 1 返回标准化的错误响应 但我似乎不知道如何处理模型绑定错误 该项目刚刚从 ASP NET Core Web 应用程序 gt API 模板创建 我有一个简单的操作定义为 Route
  • ASP.NET 数据集 getdataBy 无法启用约束。一行或多行包含违反非空、唯一或外键约束的值

    你好 我有一个非常简单的网络表单 我在此表单上有一个按钮和一个网格视图 以及一个包含链接表 bill docket docket bill 等的数据集 在按钮上单击我使用以下代码 protected void button click ob
  • 通过 EUSART PIC18F45K80 打印消息

    我正在尝试向 Docklight 发送串行消息 但始终收到空值 我正在使用带有 XC8 MPLAB X 的 PIC18F45K80 我的代码中的所有内容似乎都是正确的 但我想我错了 我该如何修复它 include
  • 使用 Process.Start() 打开文件夹时访问被拒绝异常

    我有一个 C 中的 winforms 应用程序 我必须在其中打开某个文件夹 我用 System Diagnostics Process Start pathToFolder 这会导致以下异常 System ComponentModel Wi
  • 为什么我在这段代码中不断得到两个相同的随机值? [复制]

    这个问题在这里已经有答案了 可能的重复 为什么我的随机数生成器在 C 中不是随机的 https stackoverflow com questions 932520 why does it appear that my random num
  • 在 C++ 中初始化指针

    可以在声明时将指针分配给值吗 像这样的东西 int p 1000 是的 您可以在声明时初始化指向值的指针 但是您不能这样做 int p 1000 是个地址运算符 并且您不能将其应用于常量 尽管如果可以 那会很有趣 尝试使用另一个变量 int
  • Docker 不遵循构建目录中的符号链接

    我正在对一个应用程序进行 Docker 化 其中涉及通过 Clang 将二进制文件与其他 C 文件链接 我们维护二进制文件的符号链接版本 因为它们在整个代码库中使用 我的 Docker 构建目录包含整个代码库 包括源文件以及这些源文件的符号
  • Ajax 函数在重定向后不保存滚动位置

    正如标题所述 我编写了一个 ajax 函数 该函数应该滚动到用户在重定向之前所在的位置 我写了一个alert对于测试场景 它确实触发了 但滚动不断回到顶部 我在这里做错了什么 JavaScript ajax type GET url Adm
  • C++ 中类型信息何时向后流动?

    我刚刚看了 Stephan T Lavavej 的演讲CppCon 2018关于 类模板参数推导 在哪里某个点 https youtu be H ut6j1BYU t 941他顺便说 在 C 中 类型信息几乎永远不会向后流动 我不得不说 几
  • C# 或 Windows 相当于 OS X 的 Core Data?

    我迟到了 现在才开始在 OS X Cocoa 中使用 Core Data 它令人难以置信 并且确实改变了我看待事物的方式 C 或现代 Windows 框架中是否有等效的技术 即拥有可免费保存 数据管理 删除 搜索的托管数据类型 还想知道Li
  • 将华氏温度转换为摄氏度的 C 程序始终打印零

    我需要一些关于用 C 语言将华氏温度转换为摄氏度的程序的帮助 我的代码如下所示 include
  • 在标准 C 中将 int 转换为 string

    我是 C 新手 我正在寻找一个可以调用函数进行转换的示例int串起来 我发现itoa但这不是标准 C 的一部分 我还发现sprintf str d aInt 但问题是我不知道所需的 str 的大小 因此 我如何传递输出字符串的正确大小 有多
  • WPF DataGrid 选定项

    我有一个 DataGrid 用户可以通过在最后一行输入数据来添加项目 我还有一个按钮可以删除当前选定的项目 但是 当选择最后一行 空 用于添加新项目 时 最后选定的项目将保留在 SelectedItem 中 因此 如果我打开窗口 选择最后一
  • 清理 TPL 中的 CallContext

    根据我使用的是基于 async await 的代码还是基于 TPL 的代码 我在逻辑清理方面得到了两种不同的行为CallContext 我可以设置和清除逻辑CallContext如果我使用以下异步 等待代码 正如我所期望的 class Pr
  • File.Move 的原子性

    我想将目录中的文件重命名为原子事务 该文件不会更改目录 该路径作为 NTFS 文件系统的 UNC 路径提供 可能位于服务器 03 或 08 上 File Move 对于这些目的来说是原子的吗 例如 它要么成功完成 要么失败 以使原始文件仍然

随机推荐