好吧,我已经和我争斗了一段时间,我不明白我需要做什么。这是我的LinkedList标题。
// Non templated version
#pragma once
#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__
// Get access to size_t definitions
#include <cstddef>
using std::size_t;
// A type alias for the stored type. Changing this changes what is stored
typedef int ItemType;
// Nodes for a linked list in C++
class Node
{
// A friend declaration allows LinkedList class to access the Node's private data
friend class LinkedList;
public:
Node(const ItemType& data, Node* next = nullptr);
private:
ItemType _data;
Node* _next;
};
// An linked list for C++
class LinkedList
{
public:
LinkedList();
LinkedList(const LinkedList&);
~LinkedList();
LinkedList& operator=(const LinkedList&);
ItemType pop_front();
ItemType& front();
void push_front(const ItemType& value);
void insert(size_t index, ItemType& data); // Replace these w/ iterators
void remove(size_t index);
size_t getSize() const;
private:
// Helper methods
void copy(const LinkedList &src);
void dealloc();
Node* find(size_t index) const;
// data
size_t _size;
Node *_head;
};
void LinkedList::insert(size_t index, Dweller &data){
Node* temp = this->_head;
while (temp->_next != NULL){
temp = temp->_next;
}
Node newNode = Node(data);
temp->_next = &newNode;
}
#endif
这是我的vault.h文件:
#include <string>
#include <vector>
#include "linked_list.h"
using namespace std;
class Dweller{
public:
Dweller(const string& name, int& strength, int& agility, int& perception);
int getStrength();
int getAgility();
int getPerception();
string getName();
private:
string name;
int strength;
int agility;
int perception;
};
Dweller::Dweller(const string& name, int& strength, int& agility, int& perception){
if ((strength > 10) || (strength < 1)){
cout << "Invalid number." << endl;
}
if ((agility > 10) || (strength < 1)){
cout << "Invalid number." << endl;
}
if ((perception > 10) || (perception < 1)){
cout << "Invalid number." << endl;
}
this->name = name;
this->strength = strength;
this->agility = agility;
this->perception = perception;
}
int Dweller::getStrength(){
return this->strength;
}
int Dweller::getAgility(){
return this->agility;
}
int Dweller::getPerception(){
return this->perception;
}
string Dweller::getName(){
return this->name;
}
class Room{
public:
Room(const string& name, const string& statistic);
void print();
void add(Dweller&);
private:
string name;
string statistic;
LinkedList dwellers = LinkedList();
};
Room::Room(const string& name, const string& statistic){
this->name = name;
this->statistic = statistic;
}
void Room::add(Dweller& person){
dwellers.insert(0, person);
}
我无法编辑的driver.cpp文件。这是一项任务。
// driver.cpp
// Testing driver for Assignment 2
#include <iostream>
#include <locale>
#include <string>
#include "vault.h"
using std::cin;
using std::getline;
using std::cout;
using std::endl;
using std::string;
using std::locale;
using std::tolower;
int main()
{
std::locale loc;
// We create three rooms: Power Generator (Strength), Water Processing (Perception),
// and Diner (Agility)
Room power("Power Generator", "Strength");
Room water("Water Processing", "Perception");
Room diner("Diner", "Agility");
string prompt;
do
{
string charName;
cout << "What is the Dweller's name? ";
getline(cin, charName);
int str = 0, per = 0, agl = 0;
char room;
do
{
cout << "What is the character's Strength [1-10]? ";
cin >> str;
}
while(str <= 0 || str > 10);
do
{
cout << "What is the character's Perception [1-10]? ";
cin >> per;
}
while(per <= 0 || per > 10);
do
{
cout << "What is the character's Agility [1-10]? ";
cin >> agl;
}
while(agl <= 0 || agl > 10);
do
{
cout << "Which room [(P)ower, (W)ater, (D)iner]? ";
cin >> room;
room = tolower(room, loc);
}
while(room != 'p' && room != 'w' && room != 'd');
if(room == 'p')
power.add(Dweller(charName, str, per, agl));
else if(room == 'w')
water.add(Dweller(charName, str, per, agl));
else
diner.add(Dweller(charName, str, per, agl));
cout << "Are there more Dwellers [Y/N]? " << endl;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Flush newlines
getline(cin, prompt);
}
while(tolower(prompt[0], loc) == 'y');
power.print();
water.print();
diner.print();
}
我遇到的问题是我一直无法将const Dweller转换为const ItemType。
我要做的是将Dweller对象添加到链接列表。
答案 0 :(得分:0)
LinkList类有原因吗?如果不是为什么不做这样的事情呢? Vector几乎拥有你需要的一切。除非你需要刷新,否则其他一些建议是不要使用std :: endl。而是使用\ n。
// Get access to size_t definitions
#include <vector>
#include <iostream>
#include <locale>
#include <string>
class Dweller{
public:
Dweller(const std::string name, int strength, int agility, int perception);
int getStrength();
int getAgility();
int getPerception();
std::string getName();
private:
std::string name_;
int strength_;
int agility_;
int perception_;
};
Dweller::Dweller(const std::string name, int strength, int agility, int perception) :
name_{name},
strength_{strength},
agility_{agility},
perception_{perception}
{
if (strength > 10 || strength < 1) {
std::cout << "Invalid number.\n";
}
if ((agility > 10) || (strength < 1)){
std::cout << "Invalid number.\n";
}
if ((perception > 10) || (perception < 1)){
std::cout << "Invalid number.\n";
}
}
int Dweller::getStrength(){
return strength_;
}
int Dweller::getAgility(){
return agility_;
}
int Dweller::getPerception(){
return perception_;
}
std::string Dweller::getName(){
return name_;
}
class Room{
public:
Room(const std::string name, const std::string statistic);
void print();
void add(Dweller&);
private:
std::string name_;
std::string statistic_;
std::vector<Dweller> dwellers_;
};
Room::Room(const std::string name, const std::string statistic) :
name_{name},
statistic_{statistic}
{}
void Room::print()
{
for (auto& iter : dwellers_) {
std::cout << "\nName: " << iter.getName()
<< "\nStrength: " << iter.getStrength()
<< "\nAgility: " << iter.getAgility()
<< "\nPerception: " << iter.getPerception();
}
}
void Room::add(Dweller& person)
{
dwellers_.push_back(person);
}
int main()
{
std::locale loc;
// We create three rooms: Power Generator (Strength), Water Processing (Perception),
// and Diner (Agility)
Room power("Power Generator", "Strength");
Room water("Water Processing", "Perception");
Room diner("Diner", "Agility");
std::string prompt;
do
{
std::string charName;
std::cout << "What is the Dweller's name? ";
std::getline(std::cin, charName);
int str = 0, per = 0, agl = 0;
char room;
do
{
std::cout << "What is the character's Strength [1-10]? ";
std::cin >> str;
} while (str <= 0 || str > 10);
do
{
std::cout << "What is the character's Perception [1-10]? ";
std::cin >> per;
} while (per <= 0 || per > 10);
do
{
std::cout << "What is the character's Agility [1-10]? ";
std::cin >> agl;
} while (agl <= 0 || agl > 10);
do
{
std::cout << "Which room [(P)ower, (W)ater, (D)iner]? ";
std::cin >> room;
room = tolower(room, loc);
} while (room != 'p' && room != 'w' && room != 'd');
if (room == 'p')
power.add(Dweller(charName, str, per, agl));
else if (room == 'w')
water.add(Dweller(charName, str, per, agl));
else
diner.add(Dweller(charName, str, per, agl));
std::cout << "Are there more Dwellers [Y/N]? \n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Flush newlines
std::getline(std::cin, prompt);
} while (tolower(prompt[0], loc) == 'y');
power.print();
water.print();
diner.print();
std::cout << '\n';
system("PAUSE");
}
答案 1 :(得分:0)
首先让你可以做你想做的事。将节点的定义更改为模板
template <class ItemType>
class Node
{
//node, but with a few tweaks to replace the typedef of ItemType with the templating
}
与LinkedList相同
template <class ItemType>
class LinkedList
{
//lots of tweaks to replace node with templated node
...
Node<ItemType> *_head;
};
然后当你需要制作清单时:
LinkedList<Dweller> dwellers;