简体   繁体   English

默认构造函数参数错误 - 'Autoturism::Autoturism(char *,unsigned int)':无法将参数 1 从 'const char [6]' 转换为 'char *'

[英]Default Constructor Parameter Error - 'Autoturism::Autoturism(char *,unsigned int)': cannot convert argument 1 from 'const char [6]' to 'char *'

A solution is to convert "Rosie" to char* using (char*) , I am curious if it is another one.一种解决方案是使用(char*)"Rosie"转换为char* ,我很好奇它是否是另一个。

图片

String literals in C++ have types of constant character arrays that used as expressions with rare exceptions are converted to pointers to their first characters of the type const char * . C++ 中的字符串文字具有用作表达式的常量字符数组类型,在极少数例外情况下被转换为指向其类型为const char *的第一个字符的指针。 But the first parameter of your constructor has the type char * instead of const char * .但是构造函数的第一个参数的类型是char *而不是const char * So the compiler issues an error.所以编译器会报错。

Also the constructor can produce a memory leak due to the default argument where a memory is dynamically allocated but not deleted.此外,由于内存是动态分配但未删除的默认参数,构造函数可能会产生内存泄漏。

You could declare the constructor at least the following way您至少可以通过以下方式声明构造函数

Autoturism( const char * = "", unsigned int = 0 );

As the memory is allocated dynamically you need explicitly to define the destructor that will delete the allocated memory and the copy constructor and the copy assignment operator or to define the last two as deleted.由于内存是动态分配的,您需要明确定义将删除已分配内存的析构函数、复制构造函数和复制赋值运算符,或者将最后两个定义为已删除。

First, note that your default parameter value ( c = new char[1]() ) is a memory leak, since the constructor doesn't take ownership of the new[] 'ed memory to delete[] it later.首先,请注意您的默认参数值 ( c = new char[1]() ) 是内存泄漏,因为构造函数不获取new[] 'ed 内存的所有权以稍后delete[]它。 There is never a good reason to use new[] 'ed memory for a parameter's default value.永远没有充分的理由使用new[] 'ed 内存作为参数的默认值。

"Rosie" is a string literal. "Rosie"是一个字符串文字。 It has a type of const char[6] , which in C++11 and later cannot be assigned as-is to a non-const char* pointer, an explicit type-cast is required (in which case, you should use const_cast instead of a C-style cast), eg:它有一种const char[6] ,在 C++11 及更高版本中不能按原样分配给非常量char*指针,需要显式类型const_cast (在这种情况下,您应该使用const_cast而不是 C 风格的演员),例如:

#include <iostream>
#include <string>

class Autoturism
{
    static int nr_autoturisme; // nr_autoturis 'active'
    char* culoare;
    unsigned int a_fabricatie;
    
public:
    Autoturism(char* = nullptr, unsigned int = 0);
    ~Autoturism();

    // TODO: you will also need a copy constructor and a
    // copy assignment operator, per the Rule of 3/5/0:
    // https://en.cppreference.com/w/cpp/language/rule_of_three
    ...
};

int Autoturism::nr_autoturisme{ 0 };

Autoturism::Autoturism(char* c, unsigned int an)
{
    if (!c) c = const_cast<char*>("");

    size_t len = strlen(c);
    culoare = new char[len + 1];
    strcpy_s(culoare, len + 1, c);

    an_fabricatie = an;
    ++nr_autoturism;    

    std::cout << "\nConstructorul a fost apelat !";
}

Autoturism::~Autoturism()
{
    delete[] culoare;

    std::cout << "\nDeconstructorul a fost apelat !";
}

...

int main()
{
    Autoturism a1;
    Autoturism a2(const_cast<char*>("Rosie"), 1999);

    ...

    return 0;
}

Otherwise, if you really intend to stay with C-style string handling, then you should change the c parameter to const char* instead (it should be a pointer-to-const anyway, since the constructor does not modify the data being pointed at), eg:否则,如果您真的打算继续使用 C 风格的字符串处理,那么您应该c参数更改为const char* (无论如何它应该是一个pointer-to-constpointer-to-const ,因为构造函数不会修改所指向的数据),例如:

#include <iostream>
#include <string>

class Autoturism
{
    static int nr_autoturisme; // nr_autoturis 'active'
    char* culoare;
    unsigned int a_fabricatie;
    
public:
    Autoturism(const char* = "", unsigned int = 0);
    ~Autoturism();

    // TODO: you will also need a copy constructor and a
    // copy assignment operator, per the Rule of 3/5/0:
    // https://en.cppreference.com/w/cpp/language/rule_of_three
    ...
};

int Autoturism::nr_autoturisme{ 0 };

Autoturism::Autoturism(const char* c, unsigned int an)
{
    if (!c) c = "";

    size_t len = strlen(c);
    culoare = new char[len + 1];
    strcpy_s(culoare, len + 1, c);

    an_fabricatie = an;
    ++nr_autoturism;    

    std::cout << "\nConstructorul a fost apelat !";
}

Autoturism::~Autoturism()
{
    delete[] culoare;

    std::cout << "\nDeconstructorul a fost apelat !";
}

...

int main()
{
    Autoturism a1;
    Autoturism a2("Rosie", 1999);

    ...

    return 0;
}

But, with that said, why are you using this old C-style string handling in C++ at all?但是,话虽如此,您为什么要在 C++ 中使用这种旧的 C 样式字符串处理? You should be using std::string instead (you are already including the <string> header), just let it deal with all of the memory management for you, eg:应该使用std::string代替(您已经包含<string>标头),只需让它为您处理所有内存管理,例如:

#include <iostream>
#include <string>

class Autoturism
{
    static int nr_autoturisme; // nr_autoturis 'active'
    std::string culoare;
    unsigned int a_fabricatie;
    
public:
    Autoturism(const std::string & = "", unsigned int = 0);

    // std:string is already compliant with the Rule of 3/5/0,
    // so the compiler's auto-generated destructor, copy constructor,
    // and copy assignment operator will suffice...
};

int Autoturism::nr_autoturisme{ 0 };

Autoturism::Autoturism(const std::string &c, unsigned int an)
{
    culoare = c;
    an_fabricatie = an;
    ++nr_autoturism;    

    std::cout << "\nConstructorul a fost apelat !";
}

int main()
{
    Autoturism a1;
    Autoturism a2("Rosie", 1999);

    ...

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法在初始化时将“const unsigned char (*)[7]”转换为“const unsigned char (*)[]” - error: cannot convert 'const unsigned char (*)[7]' to 'const unsigned char (*)[]' in initialization 无法将参数从const char *转换为char * - cannot convert parameter from `const char *` to `char *` C ++ - 错误C2664:'int scanf(const char *,...)':无法将参数1从'int'转换为'const char *' - C++ — error C2664: 'int scanf(const char *,…)' : cannot convert argument 1 from 'int' to 'const char *' .cpp:23:错误:无法将参数&#39;1&#39;的&#39;std :: string&#39;转换为&#39;const char *&#39;到&#39;int atoi(con​​st char *)&#39; - .cpp:23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’ 错误 C2664:“int printf(const char *const,…)”:无法将参数 2 从“void”转换为“…” - error C2664: 'int printf(const char *const ,…)': cannot convert argument 2 from 'void' to '…' 无法将参数 1 从“const char [6]”转换为 Object - Cannot convert argument 1 from 'const char [6]' to Object 无法转换 &#39;std::basic_string<char> &#39; 到 &#39;const char*&#39; 作为参数 &#39;1&#39; 到 &#39;int system(const char*)&#39; - cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)' 累积<int> (&amp;name[0], &amp;name[length]) 给出错误无法将参数 1 从 &#39;char *&#39; 转换为 &#39;const int *&#39; - accum<int>(&name[0], &name[length]) gives error cannot convert parameter 1 from 'char *' to 'const int *' 无法从 const char* 转换为 const char *&amp; - cannot convert from const char* to const char *& 无法将参数1从'const char [5]'转换为'LPCTSTR' - Cannot convert argument 1 from 'const char [5]' to 'LPCTSTR'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM