【问题标题】:no matching function for call to 'std::basic_string<char>::basic_string c++没有匹配函数调用 'std::basic_string<char>::basic_string c++
【发布时间】:2013-11-29 03:45:24
【问题描述】:

嘿,我正在制作一个“制作你自己的冒险游戏!”现在除了必须通过空洞游戏之外,我想制作一个作弊码系统,现在我试图声明一个字符串女巫等于超过 6 个单词我不明白问题是什么我只用了两个单词并且没有出现任何错误,但是当我用超过 2 个单词做同样的事情时,我得到了错误。

Main.cpp|27|error: no matching function for call to 'std::basic_string::basic_string(const char [4], const char [6], const char [5], const char [5],常量字符 [6], 常量字符 [6])'|

这是我的代码:

#include <iostream>
//LVL1
#include "C:\Users\QuestionMark\Desktop\Make Your Own Adventure\LVL1\Dog.h"
#include "C:\Users\QuestionMark\Desktop\Make Your Own Adventure\LVL1\Dream.h"
#include "C:\Users\QuestionMark\Desktop\Make Your Own Adventure\LVL1\GTFO.h"

using namespace std;

int Return();
int Continue();

int main(){

    cout << "Welcome to my 'MAKE YOUR OWN ADVENTURE GAME!!!'\n";
    cout << "Have Fun and enjoy the ride!\n";
    cout << "Would you like to put in a cheat code??\n";
    cout << "Yes or No, Cap Sensitive!\n";
    Return();
    return 0;
}

int Return(){
        std::string y("Yes","No");
        cin >> y;
if(y.compare("Yes")){
        cout << "Please Enter Cheat Code now\n";
        std::string z("Dog","Dream","GTFO","Path","Sword","Weird");
        cin >> z;
    if(z.compare("Dog")){
        Dog();
    }else if(z.compare("Dream")){
        Dream();
    }else if(z.compare("GTFO")){
        GTFO();
    }else if(z.compare("Path")){
        Path();
    }else if(z.compare("Sword")){
        Sword();
    }else if(z.compare("Weird")){
        Weird();
    }else{
    cout << "Invalid Cheat Code\n";
   }

}else if(y.compare("No")){
return Continue();
}else{
    cout << "Invalid Answer!\n";
    Continue();
}
}

int Continue(){

    cout << endl;
    cout << "You wake up and your house is on fire what do you do ??\n";
    cout << "Quick Grab The Dog = 0, GTFO = 1, Go back to sleep = any other number\n";
    int x;
    cin >> x;
    if(x == 0){
         Dog();
    }else if(x == 1){
         GTFO();
    }else{
         Dream();
    }
}

【问题讨论】:

  • 我不确定您要做什么,但请在 std::string 上查找参考。也请注意==
  • 没有带六个参数的 std::string 构造函数。
  • 也没有构造函数采用两个字符串。您碰巧偶然发现的是迭代器对构造函数,如果它运行,它可能会使您的程序崩溃。

标签: c++ string compare adventure


【解决方案1】:

您的问题在于您对字符串 z 的声明,您的代码是:

std::string z("Dog","Dream","GTFO","Path","Sword","Weird");

你的编译器找不到 std::string 的构造函数,它需要 6 个参数,而是尝试

std::string z("any string");

或者因为你即将初始化 z 只是

std::string z;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-14
    • 2015-06-02
    • 2018-12-05
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多