C ++,Need错误原因:无法将参数1从'char *'转换为'const char *&' [英] C++, Need Reason for error : cannot convert parameter 1 from 'char *' to 'const char *&'

查看:2128
本文介绍了C ++,Need错误原因:无法将参数1从'char *'转换为'const char *&'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乳清我们不能将指针转换为字符 - > TO->指向常量字符的指针

Whey we cannot Convert pointer to a character ->TO-> a reference to a pointer to a constant character

我有兴趣了解语法错误的原因当我们调用foo_ptr。当foo_char被允许为什么不foo_ptr。

[更新1.]我会很高兴知道 foo_char()工作的原因,为什么 foo_ptr()不工作..当指针进入图片时会发生什么。



[更新2.]
无法在Dev C ++编译器版本4.9.9.2中使用。

I am interested in knowing the reason of syntax error when we call foo_ptr. When foo_char is allowed why not foo_ptr.
[Update 1.] I would be happy in knowing the reason that foo_char() is working, why foo_ptr() is not working .. What happens when pointer come in the picture.

[Update 2.] Didnt work in Dev C++ compiler version 4.9.9.2 too ..

//code
//OS : Win XP
//Env: VC++ 2008 

//NOT ALLOWED
void foo_ptr(const char * & ptr) //reference to a pointer to a constant character         
{         
        return;         
}        


//allowed        
void foo_char(const char & p_Char) //reference to a constant character        
{         
        return;        
}        

int main()        
{        
        char ch = 'd';        
        char *ptr =  "anu";        

        foo_char(ch);         
        foo_ptr(ptr); //NOT ALLOWED syntax error, vc++, 2008        

        return 0;        
}        


推荐答案

>

Suppose you had

void foo_ptr(const char * & ptr)
{         
    ptr = "readonlystring";
}        

您现在称为

    char *ptr;
    foo_ptr(ptr);
    *ptr = 0;

假设没有发生错误。

这基本上是如何得到一个指向一个派生类的指针不能传递给需要引用指向基类的指针的函数?

这篇关于C ++,Need错误原因:无法将参数1从'char *'转换为'const char *&'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆