一個解決方案是轉換"Rosie"為char*using (char*),我很好奇它是否是另一個。
![默認建構式引數錯誤 - 'Autoturism::Autoturism(char *,unsigned int)':無法將引數 1 從 'const char [6]' 轉換為 'char *'](https://img.uj5u.com/2021/10/27/b6724e3a16b34f9884d99500d72e4a34.png)
uj5u.com熱心網友回復:
C 中的字串文字具有用作運算式的常量字符陣列型別,在極少數情況下會轉換為指向其第一個型別字符的指標const char *。但是建構式的第一個引數的型別char *不是const char *. 所以編譯器會報錯。
此外,由于記憶體是動態分配但未洗掉的默認引數,建構式可能會產生記憶體泄漏。
您至少可以通過以下方式宣告建構式
Autoturism( const char * = "", unsigned int = 0 );
由于記憶體是動態分配的,您需要明確定義將洗掉已分配記憶體的解構式、復制建構式和復制賦值運算子,或者將最后兩個定義為已洗掉。
uj5u.com熱心網友回復:
首先,請注意您的默認引數值 ( c = new char[1]()) 是記憶體泄漏,因為建構式稍后不會將new[]'ed 記憶體的所有權交給delete[]它。永遠沒有充分的理由使用new[]'ed 記憶體作為引數的默認值。
"Rosie"是字串文字。它有一個 型別const char[6],在 C 11 及更高版本中不能按原樣分配給非常量char*指標,需要顯式型別轉換(在這種情況下,您應該使用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;
}
否則,如果您真的打算繼續使用 C 風格的字串處理,那么您應該將c引數改為const char*改為(pointer-to-const無論如何它應該是 a ,因為建構式不會修改所指向的資料),例如:
#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;
}
但是,話雖如此,您為什么要在 C 中使用這種舊的 C 樣式字串處理?你應該使用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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337512.html
