“没有构造函数的实例”所有 std 数据类型(映射、向量、堆栈等)的错误

发布于 2025-01-10 20:54:49 字数 3052 浏览 0 评论 0 原文

我遇到了一个奇怪的问题。我认为这可能是由于 Mac 出现问题,但我不确定。本质上,我在 Windows 笔记本电脑上解决了这个 Leetcode 问题,并将代码推送到我的 github 存储库。然后我稍后在我的 mac 上获取该代码,当尝试初始化任何 std:: 数据类型(例如映射、向量或堆栈)时,突然出现此错误。这很奇怪,因为我之前从未收到过此错误,并且使用这些类到目前为止工作得很好。我不太确定发生了什么,有人可以指出我正确的方向来解决这个问题吗?我在下面附上了显示错误的屏幕截图。我环顾四周,找不到其他人有类似的问题。谢谢!

#include <vector>
#include <map>
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdlib.h>

using namespace std;
/**
 * @brief Rules:
 * 1. Open brackets must be closed by the same type of brackets.
 * 2. Open brackets must be closed in the correct order.
 * 
 * Leetcode Challenge can be found here: https://leetcode.com/problems/valid-parentheses/
 */
//correlate the opening character with the closing character.

map<char, char> legend {{'{','}'},{'(',')'},{'[',']'}};//Error
vector<int> vect{0,2,3,4,5};//Error

bool isValid(string s) 
{
    //if s is odd then we know a bracket hasn't been closed. Return false.
    if (s.length() % 2 != 0)
    {
        return false;
    }

    vector<char> stack; //initiate our stack where we'll store opening characters "(, {, ["
    bool stackIsModified = false; //We need to make sure an opening character has been added to the stack at least once.

    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '{' || s[i] == '[' || s[i] == '(')//Check and see if s[i] is an opening character.
        {
            stackIsModified = true;
            stack.push_back(s[i]);
            cout << s[i] << endl;
        }
        
        else if(stack.size() != 0) //See if s[i] is a closing character.
        {
            if (legend[stack.at(stack.size() - 1)] == s[i])
            {
                stack.pop_back();
            }
            else //If s[i] is a closing character that doesn't match the corresponding opening character. Ex: "{)"
            {
                return false;
            }
        }
        else //If s[i] isn't an opening character and the stack is empty then we know there's a mismatch, return false.
        {
            return false;
        }
    }

    if (stack.size() > 0 || !stackIsModified) //Make sure the stack doesn't have remaining opening characters and that the stack has been changed at least once.
    {
        cout << stackIsModified << endl;
        return false;
    }
    
    return true;
}

int main() 
{

    cout << isValid("()))") << endl;//Random test case.
    return 0;
}

这些是我收到的错误(在旁边有错误注释的两行):

no instance of constructor "std::__1::map<_Key, _Tp, _Compare, _Allocator>::map [with _Key=char, _Tp=char, _Compare=std::__1::less<char>, _Allocator=std::__1::allocator<std::__1::pair<const char, char>>]" matches the argument listC/C++(289)
std::__1::vector<int> vect
no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=int, _Allocator=std::__1::allocator<int>]" matches the argument listC/C++(289)

I'm running into a weird problem. I'm thinking this could be possibly due to something wrong with Mac, but I'm not sure. Essentially, I solved this Leetcode problem on my windows laptop and pushed the code to my github repo. Then I fetched that code on my mac later on and all the sudden I get this error when trying to initialize any std:: data types such as a map, vector, or stack. It's weird because I was never receiving this error before and using those classes worked perfectly up to this point. I'm not really sure what's up, could someone point me in the right direction as to how to fix this? I've attached a screenshot below showing the error. I have been looking around and I can't find anyone else with a similar problem. Thanks!

#include <vector>
#include <map>
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdlib.h>

using namespace std;
/**
 * @brief Rules:
 * 1. Open brackets must be closed by the same type of brackets.
 * 2. Open brackets must be closed in the correct order.
 * 
 * Leetcode Challenge can be found here: https://leetcode.com/problems/valid-parentheses/
 */
//correlate the opening character with the closing character.

map<char, char> legend {{'{','}'},{'(',')'},{'[',']'}};//Error
vector<int> vect{0,2,3,4,5};//Error

bool isValid(string s) 
{
    //if s is odd then we know a bracket hasn't been closed. Return false.
    if (s.length() % 2 != 0)
    {
        return false;
    }

    vector<char> stack; //initiate our stack where we'll store opening characters "(, {, ["
    bool stackIsModified = false; //We need to make sure an opening character has been added to the stack at least once.

    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '{' || s[i] == '[' || s[i] == '(')//Check and see if s[i] is an opening character.
        {
            stackIsModified = true;
            stack.push_back(s[i]);
            cout << s[i] << endl;
        }
        
        else if(stack.size() != 0) //See if s[i] is a closing character.
        {
            if (legend[stack.at(stack.size() - 1)] == s[i])
            {
                stack.pop_back();
            }
            else //If s[i] is a closing character that doesn't match the corresponding opening character. Ex: "{)"
            {
                return false;
            }
        }
        else //If s[i] isn't an opening character and the stack is empty then we know there's a mismatch, return false.
        {
            return false;
        }
    }

    if (stack.size() > 0 || !stackIsModified) //Make sure the stack doesn't have remaining opening characters and that the stack has been changed at least once.
    {
        cout << stackIsModified << endl;
        return false;
    }
    
    return true;
}

int main() 
{

    cout << isValid("()))") << endl;//Random test case.
    return 0;
}

And these are the errors I'm recieving (at the two lines that have error commented next to them):

no instance of constructor "std::__1::map<_Key, _Tp, _Compare, _Allocator>::map [with _Key=char, _Tp=char, _Compare=std::__1::less<char>, _Allocator=std::__1::allocator<std::__1::pair<const char, char>>]" matches the argument listC/C++(289)
std::__1::vector<int> vect
no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=int, _Allocator=std::__1::allocator<int>]" matches the argument listC/C++(289)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷鸟归林 2025-01-17 20:54:49

使用 -std=c++11 (或更高版本)。

您正在尝试使用 C++11 中引入的 std::initializer_list 构造函数。
有关更多信息,请参阅 向量地图

Use -std=c++11 (or higher).

You are trying to use constructors from std::initializer_list that were introduced in C++11.
For further information, see the documentation for vectors and maps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文