简体   繁体   中英

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

I'm writing a modified version of 20 Questions for my CS2 class and I am having one heck of a time trying to figure out why I'm getting the error.

IC2664 cannot convert argument 1 from 'const char ' to 'Node '

Anyways, any assistance would be appreciated.

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

is incorrect way of creating 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");

is the correct way of constructing shared_ptr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM