c++ - 如何修复“clang: error: linker command failed with exit code 1 (use -v to see invocation)”错误?
问题描述
我有一个程序应该按名称创建一个排序的链表。但是,运行它后,输出给我一个错误:架构 x86_64 的未定义符号:
"StudentList::insertNode(Student)", referenced from:
buildList(StudentList&) in main-acc0ea.o
"StudentList::StudentList()", referenced from:
_main in main-acc0ea.o
"StudentList::~StudentList()", referenced from:
_main in main-acc0ea.o
"StudentList::displayList(double) const", referenced from:
_main in main-acc0ea.o
"StudentList::displayList(double, double) const", referenced from:
_main in main-acc0ea.o
"StudentList::displayList() const", referenced from:
_main in main-acc0ea.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的代码:
主文件
#include <iostream>
#include "StudentList.h"
using namespace std;
void buildList(StudentList &);
int main()
{
// Define a StudentList object
StudentList list;
buildList(list); // insert data into the list
list.displayList();
double gpa;
cout << "Enter a gpa: ";
cin >> gpa;
list.displayList(gpa);
double from, to;
cout << "Enter a gpa range: ";
cin >> from >> to;
list.displayList(from, to);
system("pause");
return 0;
}
void buildList(StudentList &list)
{
// Define and initialize an array of Student objects
Student s[10] =
{ { 2.3, "Tom" },{ 2.5, "John" },{ 3.1, "Paul" },{ 3.9, "Linda" },{ 3.6, "Bob" },{ 2.7, "Ann" },{ 4.0, "Mary" },
{ 3.2, "Andy" },{ 0, "#" } };
//Insert data from array into the linked list
for (int i = 0; s[i].name != "#"; i++)
{
list.insertNode(s[i]);
}
}
学生列表.h
#ifndef STUDENTLIST_H
#define STUDENTLIST_H
#include<string>
struct Student
{
double gpa;
std::string name;
};
class StudentList
{
private:
// Declare a structure for the list
struct ListNode
{
Student stu; // The value in this node
ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
int count; // To keep track of the number of nodes in the list
public:
StudentList(); // Constructor
~StudentList(); // Destructor
// Linked list operations
int getCount() const { return count; }
void insertNode(Student);
void displayList() const;
void displayList(double) const;
void displayList(double,double) const;
/* Write your code here */
};
#endif
学生列表.cpp:
#include <iostream> // For cout and NULL
#include "StudentList.h"
using namespace std;
//**************************************************
// Constructor
// This function allocates and initializes a sentinel node
// A sentinel (or dummy) node is an extra node added before the first data record.
// This convention simplifies and accelerates some list-manipulation algorithms,
// by making sure that all links can be safely dereferenced and that every list
// (even one that contains no data elements) always has a "first" node.
//**************************************************
StudentList::StudentList()
{
head = new ListNode; // head points to the sentinel node
head->stu.gpa = -1;
head->stu.name = "";
head->next = NULL;
count = 0;
}
//**************************************************
// displayList shows the value
// stored in each node of the linked list
// pointed to by head.
//**************************************************
void StudentList::displayList() const
{
ListNode *pCur; // To move through the list
// Position pCur: skip the head of the list.
pCur = head->next;
// While pCur points to a node, traverse the list.
cout << endl;
while (pCur)
{
// Display the value in this node.
cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
// Move to the next node.
pCur = pCur->next;
}
cout << endl;
}
//**************************************************
// The insertNode function inserts a node with
// stu copied to its value member.
//**************************************************
void StudentList::insertNode(Student dataIn)
{
ListNode *newNode; // A new node
ListNode *pCur; // To traverse the list
ListNode *pPre; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->stu = dataIn;
// Initialize pointers
pPre = head;
pCur = head->next;
// Find location: skip all nodes whose gpa is less than dataIn's gpa
while (pCur != NULL && pCur->stu.name < dataIn.name)
{
pPre = pCur;
pCur = pCur->next;
}
// Insert the new node between pPre and pCur
pPre->next = newNode;
newNode->next = pCur;
// Update the counter
count++;
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
StudentList::~StudentList()
{
}
void StudentList::displayList(double aboveGPA) const
{
ListNode *pCur; // To move through the list
// Position pCur: skip the head of the list.
pCur = head->next;
// While pCur points to a node, traverse the list.
cout << endl;
while (pCur)
{
if (pCur->stu.gpa >= aboveGPA)
{
// Display the value in this node.
cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
}
// Move to the next node.
pCur = pCur->next;
}
cout << endl;
}
void StudentList::displayList(double gpa1, double gpa2) const
{
ListNode *pCur; // To move through the list
// Position pCur: skip the head of the list.
double upperLimit, loweLimit;
if (gpa1 > gpa2)
{
upperLimit = gpa1;
loweLimit = gpa2;
}
else
{
upperLimit = gpa2;
loweLimit = gpa1;
}
pCur = head->next;
// While pCur points to a node, traverse the list.
cout << endl;
while (pCur)
{
if (pCur->stu.gpa >= loweLimit && pCur->stu.gpa<=upperLimit)
{
// Display the value in this node.
cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
}
// Move to the next node.
pCur = pCur->next;
}
cout << endl;
}
我不知道我的代码是错误的还是这个问题:
Visual Studio Code clang 错误:链接器命令失败,Mac 上的退出代码为 1
我尝试在我的任务 json 中添加“${fileDirname}/*.cpp”,但它仍然给我同样的错误。这个错误还有其他解决方案吗?
解决方案
链接器找不到它抱怨的符号。可能是因为它没有查看 StudentList 对象文件。
尝试找出它是如何被调用的,并检查是否将 StudentList 对象文件提供给它。如果不是,请找出原因。
推荐阅读
- c# - Twilio MVC,我如何使电话号码/消息动态
- scrapy - Scrapy从解析中传递了一页
- google-chrome - 如何从无头 chrome 浏览器访问 PDF
- excel - VBA excel用户表单复选框问题
- php - php 文件不接受 css
- c# - C# 随机 if 语句和标签追加
- directx - 是否有 Dx11-Dx12 方法来支持包含 SV_COVERAGE 的几何着色器
- sql-server - 如何在我的连接字符串中使用 SQL Server 应用程序角色
- vue.js - 使用 Vue2Leaflet 重新排序图层
- firebase - PhoneCredential 必须是有效的电话凭据