- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当我为链表编译代码时,我得到了一堆 undefined reference 错误。代码如下。我一直在编译这两个语句:
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
LinkedList.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
LinkedList.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
test.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);
}
最佳答案
.h
)而不是源文件(即 .cpp
)。 LinearNode.h:
#ifndef LINEARNODE_H
#define LINEARNODE_H
class LinearNode
{
// .....
};
#endif
LinearNode.cpp:
#include "LinearNode.h"
#include <iostream>
using namespace std;
// And now the definitions
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class LinearNode; // Forward Declaration
class LinkedList
{
// ...
};
#endif
LinkedList.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
关于c++ - undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5293021/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!