简体   繁体   English

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

[英]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. 我正在为我的CS2类编写20个问题的修改版,并且有一段时间我试图弄清楚为什么我得到这个错误。

IC2664 cannot convert argument 1 from 'const char ' to 'Node ' IC2664无法将参数1从'const char '转换为'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 . 是创建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 是构造shared_ptr的正确方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 C2664:“int printf(const char *const,…)”:无法将参数 2 从“void”转换为“…” - error C2664: 'int printf(const char *const ,…)': cannot convert argument 2 from 'void' to '…' 错误 C2664:无法将参数 1 从“const char [14]”转换为“char” - error C2664: cannot convert argument 1 from 'const char [14]' to 'char' 错误 C2664:MessageBoxW 无法将参数 2 从“const char”转换为“LPCWSTR” - Error C2664: MessageBoxW cannot convert argument 2 from 'const char ' to 'LPCWSTR' C ++ - 错误C2664:'int scanf(const char *,...)':无法将参数1从'int'转换为'const char *' - C++ — error C2664: 'int scanf(const char *,…)' : cannot convert argument 1 from 'int' to 'const char *' 错误C2664:&#39;strcmp&#39;:无法将参数2从&#39;char&#39;转换为&#39;const char *&#39; - error C2664: 'strcmp' : cannot convert parameter 2 from 'char' to 'const char *' :错误C2664:&#39;MessageBoxW&#39;:无法从&#39;const char [40]&#39;转换参数2 - : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [40]' 错误C2664:“发送”:无法将参数2从“ ServerGreeting”转换为“ const char *” - error C2664: 'send' : cannot convert parameter 2 from 'ServerGreeting' to 'const char *' 错误 C2664:“wcscmp”:无法将参数 1 从“CHAR [260]”转换为“const wchar_t *” - error C2664: 'wcscmp' : cannot convert parameter 1 from 'CHAR [260]' to 'const wchar_t *' 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char - Keep getting C2664 error - cannot convert argument from char[10] to char 错误C2664:“ CComboBox :: InsertString”:无法将参数2从“ const char [4]”转换为“ LPCTSTR” - error C2664: 'CComboBox::InsertString' : cannot convert parameter 2 from 'const char [4]' to 'LPCTSTR'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM