C2664 无法将参数 1 从 'const char *' 转换为 'Node*'
C2664 cannot convert argument 1 from 'const char *' to 'Node*'
我正在为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 的正确方法
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串