首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误C2664:'int (const *const,.):无法将参数2从'void‘转换为’.‘

错误C2664:'int (const *const,.):无法将参数2从'void‘转换为’.‘
EN

Stack Overflow用户
提问于 2020-05-09 02:45:20
回答 2查看 630关注 0票数 0
代码语言:javascript
运行
复制
#include <iostream>
#include <stack>

void convert(std::stack<char> &s, int n,int base)
{
    static char digit[]={
            '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    };
    while(n>0)
    {
        s.push(digit[n%base]);
        n/=base;
    }
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    std::stack<char> s;
    int n=89;
    int base=2;
    convert(s,n,base);
    while(!s.empty())
    {
        printf("%c",s.pop());//this line is can not be compiled.
    }
    return 0;
}

我不明白为何这句话不能编成。

不能将'void‘类型的表达式传递给可变函数;格式字符串中的预期类型是'int'.

EN

回答 2

Stack Overflow用户

发布于 2020-05-09 02:53:58

检查std::stack::pop的签名:它返回void。您应该使用top来获取值,使用pop来删除它。

修正后的代码:

代码语言:javascript
运行
复制
#include <iostream>
#include <stack>

void convert(std::stack<char> &s, int n,int base)
{
    static char digit[]={
            '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    };
    while(n>0)
    {
        s.push(digit[n%base]);
        n/=base;
    }

}


int main() {
    std::cout << "Hello, World!" << std::endl;
    std::stack<char> s;
    int n=89;
    int base=2;
    convert(s,n,base);
    while(!s.empty())
    {
        printf("%c",s.top());
        s.pop();
    }
    return 0;
}

对代码的一般性评论:如果向函数提供负n怎么办?

票数 2
EN

Stack Overflow用户

发布于 2020-05-09 03:11:48

而不是printf s.pop,使用printf s.top(),然后也使用pop s,将这个元素从堆栈中删除

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61691137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档