C2664无法转换参数1来自' const char *'到节点*'

时间:2015-11-21 03:35:02

标签: c++ visual-studio

我正在为我的CS2课程编写20个问题的修改版本,我有一点时间试图弄清楚为什么我会收到错误。

  

IC2664无法转换参数1来自' const char '到'节点'

无论如何,任何帮助将不胜感激。

#include "LetMeGuess.h"
#include<iostream>
#include<memory>
#include<string>
#include<fstream>
#include<vector>
struct LetMeGuess::Node {
    Node(std::string data) : data(data), no(nullptr), yes(nullptr) {};
    std::string data;
    std::shared_ptr<Node>no;
    std::shared_ptr<Node>yes;
};
void LetMeGuess::letMeGuess() {
    std::cout << "***Let Me Guess!***\nLoading questionArchive.txt ..." << std::endl;
    readInFile();
    std::cout << "Done!" << std::endl;
    askQuestions();
    std::cout << "Saving questionArchive.txt ..." << std::endl;
    readOutFile(root);
    std::cout << "Done! Goodbye!" << std::endl;
}
void LetMeGuess::readInFile() {
    root = std::shared_ptr<Node>("Is it an animal?");
    root->no = std::shared_ptr<Node>("Toaster");
    root->yes = std::shared_ptr<Node>("Elephant");
    //std::vector<Node> myStack;
    //std::shared_ptr<Node> newNode;
    //std::string nextInfo;
    //std::string nextID;
    //fin.open("questionArchive.txt");
    //while (!fin.eof) {
    //  fin >> nextID;
    //  if (nextID == "A") {
    //      fin >> nextInfo;
    //      Node newNode(nextInfo);
    //      myStack.push_back(newNode);
    //  }
    //  else {
    //      fin >> nextInfo;
    //      Node newNode = Node(nextInfo);
    //      Node newNode->yes = myStack.back();
    //  }
    //}
}
std::string LetMeGuess::readOutFile(std::shared_ptr<Node>curr) {
    curr = root;
    std::string outString;
    fout.open("questionArchive.txt");
    if (!curr) return outString;
    outString += readOutFile(curr->no);
    outString += readOutFile(curr->yes);
    if (curr->no == nullptr || curr->yes == nullptr) {
        outString += "A ";
    }
    else {
        outString += "Q ";
    }
    outString += curr->data;
    outString += "/n";
    fout << outString;
    return outString;
}
bool LetMeGuess::stillAsking(std::shared_ptr<Node> temp){
    if (temp->no == nullptr || temp->yes == nullptr) return false;
    else return true;
}
bool LetMeGuess::playAgain(char ans) {
    switch (ans) {
    case'Y':
    case'y':
        return true;
    case'N':
    case'n':
    default:
        std::cout << "y/n not selected, starting a new game anyways." << std::endl;
        return true;
    }
}
void LetMeGuess::insertNewQuestion(std::shared_ptr<Node>& curr, std::string newQuest, std::string objThought) {
    curr->no = std::make_shared<Node>(curr->data);
    curr->yes = std::make_shared<Node>(objThought);
    curr->data = newQuest;
}
void LetMeGuess::askQuestions() {
    std::shared_ptr<Node> current = root;
    char answer;
    char plyAgn = 'y';
    std::string thoughtObject;
    std::string newQuestion;
    while (playAgain(plyAgn)) {
        while (stillAsking(current)) {
            std::cout << current->data;
            std::cin >> answer;
            switch (answer) {
            case'y':
            case'Y':
                current = current->yes;
                break;
            case'n':
            case'N':
                current = current->no;
                break;
            default:
                std::cout << "Please enter y/n." << std::endl;
                break;
            }
        }
        std::cout << "Let me guess, you're thinking of a(n)" << current->data << "?" << std::endl;
        std::cin >> answer;
        switch (answer) {
        case 'y':
        case'Y':
            std::cout << "I win!" << std::endl;
            break;
        case'n':
        case'N':
            std::cout << "What where you thinking of?" << std::endl;
            std::cin >> thoughtObject;
            std::cout << "What could I have asked to know you were thinking of a(n) " + thoughtObject + " and not a(n) " + current->data + "?" << std::endl;
            std::cin >> newQuestion;
            insertNewQuestion(current, newQuestion, thoughtObject);
            break;
        default:
            std::cout << "Please enter y/n." << std::endl;
            break;
        }
        std::cout << "Would you like to play again? (y/n)" << std::endl;
        std::cin >> plyAgn;
    }
}

1 个答案:

答案 0 :(得分:0)

root = std::shared_ptr<Node>("Is it an animal?");
root->no = std::shared_ptr<Node>("Toaster");
root->yes = std::shared_ptr<Node>("Elephant");

是创建shared_ptr的错误方法。

root = std::make_shared<Node>("Is it an animal?");
root->no = std::make_shared<Node>("Toaster");
root->yes = std::make_shared<Node>("Elephant");

是构建shared_ptr

的正确方法