(1)CStudentList.h
#pragma once
#include"CStudent.h"
class CStudentList
{
CStudent* head;
public :
CStudentList(int n);
CStudentList();
~CStudentList();
};
(2)CStudentList.cpp
#include "CStudentList.h"
#include"CStudent.h"
#include<iostream>
CStudentList::CStudentList(int n)
{
head=new CStudent("HEAD", 1, 100);
head->next = NULL;
for (int i=0;i<n;i++)
{
CStudent* newNode = new CStudent();
newNode->next = head->next;
head->next = newNode;
}
}
CStudentList::CStudentList()
{
}
(3)CStudent.h
#pragma once
//鏈表節點類
//學生類
//屬性:姓名、性別、成績
//方法:錄入、顯示
class CStudent
{
char name[20];
bool sex; //ture為男,false為女
int score;
public:
CStudent* next;
CStudent(const char p_name[], bool p_sex, int p_score); //鑒于安全和修改加入const
CStudent();
void input();
void show(int method); //method用來確定顯示方式
char* getName() { return name; }
bool getSex() { return sex; }
int getScore() { return score; }
~CStudent();
};
(4)CStudent.cpp
#include "CStudent.h"
#include<cstring>
#include<iostream>
using namespace std;
CStudent::CStudent(const char p_name[], bool p_sex, int p_score)
{
strcpy_s(name, p_name);
sex = p_sex;
score = p_score;
}
CStudent::CStudent()
{
input();
}
void CStudent::input()
{
cout << "請輸入學生姓名:" << endl;
cin >> name;
cout << "請輸入學生性別(1為男,0為女):" << endl;
int isex;
cin >> isex;
sex = isex ? true : false;
cout << "請輸入學生成績:" << endl;
cin >> score;
}
void CStudent::show(int method)
{
switch (method)
{
case 0:
cout << name
<< (sex ? "男" : "女")
<< score << "\t"
<< endl;
break;
case 1:
cout << "姓名:" << name << endl
<< "性別:" << (sex ? "男" : "女") << endl
<< "成績:" << score << endl
<< endl;
break;
default:
break;
}
}
CStudent::~CStudent()
{
}
(5)main.cpp
//撰寫鏈表結點
#include<iostream>
#include "CStudentList.h"
using namespace std;
int main()
{
CStudentList s(2);
//按照需求呼叫不同的顯示格式 語法: s.show(?)
return 0;
}
使用的軟體是visual studio 2019
運行后顯示的錯誤是:
LNK2019 無法決議的外部符號 "public: __cdecl CStudentList::~CStudentList(void)" (??1CStudentList@@QEAA@XZ),該符號在函式 main 中被參考 Student
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/109058.html
標籤:新技術前沿
上一篇:資訊系統分析與設計
下一篇:有大佬會嗎
