我有一个程序,应该创建一个按名称排序的链接列表。但是,在运行它之后,输出会给出一个错误:架构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)我的代码:
Main.cpp
#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]);
}
}StudentList.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 */
};
#endifStudentList.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 error: linker command failed with exit code 1 on Mac
我尝试在我的任务json中添加"${fileDirname}/*.cpp“,但是它仍然给了我同样的错误。对此错误还有其他解决方案吗?
发布于 2020-10-12 07:14:39
链接器找不到它抱怨的符号。可能是因为它没有查看StudentList对象文件。
试着找出它是如何被调用的,并检查StudentList对象文件是否提供给它。如果没有,找出原因。
https://stackoverflow.com/questions/64313086
复制相似问题