C2664 无法将参数 1 从 'const char *' 转换为 'Node*'

C2664 cannot convert argument 1 from 'const char *' to 'Node*'

本文关键字:char 转换 Node const 参数 C2664      更新时间:2023-10-16

我正在为CS2类编写20个问题的修改版本,我花了很长时间试图弄清楚为什么会出现错误。

IC2664无法将参数1从"const char"转换为"Node"

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

#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;
    }
}
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 的正确方法