【问题标题】:How do I access the properties of this object?如何访问该对象的属性?
【发布时间】:2015-06-29 21:54:05
【问题描述】:

好的,我已经为此奋斗了一段时间,但我不明白我需要做什么。这是我的 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 对象添加到链表。

【问题讨论】:

  • 你的链表是int的列表;你打算如何在其中存储Dweller
  • typedef int ItemType; 您如何期望这实际上正确转换为 Dweller 实例?

标签: c++ object linked-list


【解决方案1】:

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");
}

【讨论】:

    【解决方案2】:

    从做你想做的事开始。更改要模板化的Node的定义

    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; 
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多