通过构造函数初始化char[40]
Initiliaizing char[40] through the constructor
我想初始化对象属性,但它一直说类型不匹配。如何纠正?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class Student{
public:
int nr_ID;
char nazwisko[40];
char imie[40];
double punkty;
Student* next;
Student(int nr_ID, char nazwisko[40], char imie[], double punkty){
this->nr_ID = nr_ID;
this->nazwisko = nazwisko;//HERE
this->imie = imie;//HERE
this->punkty = punkty;
next = NULL;
}
~Student(){}
};
没有数组类型参数这回事。你声明的参数,char nazwisko[40]
,实际上被转换成一个指针类型,char* nazwisko
。现在你可以看到你在尝试给一个数组赋值一个指针。当然不行。
实际上,您根本不能简单地将数组相互赋值。如果需要,必须将元素复制过来。你可以使用C函数strcpy
来做到这一点,它会考虑到参数应该是一个C风格的字符串。如果您想复制整个数组,那么您可能喜欢使用std::copy
。
如果你确实在处理文本字符串,你最好使用标准的std::string
类型来做这件事。它们比char
:
std::string nazwisko;
std::string imie;
// ...
Student(int nr_ID, std::string nazwisko, std::string imie, double punkty){
this->nazwisko = nazwisko;
this->imie = imie;
// ...
}
实际上,您可以省去默认初始化这些成员变量,然后通过使用构造函数成员初始化列表对它们进行赋值的痛苦:
Student(int nr_ID, std::string nazwisko, std::string imie, double punkty)
: nr_ID(nr_ID), nazwisko(nazwisko), imie(imie), punkty(punkty), next(NULL)
{ }
为了完整起见,以下是您可以更接近原始代码的方法(例如,正如有人建议的那样,如果您由于某些令人讨厌的原因不能使用std::string)。这在很大程度上是过度的和非常不典型的代码,但是嘿:)
template <size_t Lnaz, size_t Limi>
Student(int nr_ID, char const (&nazwisko)[Lnaz], char const (&imie)[Limi], double punkty)
: nr_ID(nr_ID), punkty(punkty), next(nullptr)
{
static_assert(Lnaz <= sizeof(this->nazwisko)/sizeof(*this->nazwisko), "too large");
static_assert(Limi <= sizeof(this->imie)/sizeof(*this->imie), "too large");
std::copy(nazwisko, nazwisko+Lnaz, this->nazwisko);
std::copy(imie, imie+Limi, this->imie);
}
注意
- 允许字符串包含嵌入的NUL字符。 技巧是通过引用传递数组,以避免像其他所描述的衰减到指针。
- 同样,模板在那里是因为
"hello"
将是char const (&)[6]
,"bye"
将是char const (&)[4]
-不同的参数类型
在http://liveworkspace.org/code/2rxQrg$1上观看
您可以通过将char
数组替换为std::string
(或std::vector<char>
,如果您的char
数组不意味着包含空终止字符串)来简化问题:
class Student{
public:
int nr_ID;
std::string nazwisko;
std::string imie;
double punkty;
Student* next;
Student(int nr_ID,
const std::string& nazwisko,
const std::string& imie,
double punkty)
: nr_ID(nr_ID), nazwisko(nazwisko), imie(imie), punkty(punkty), next(NULL)
{}
};
要复制c风格的字符数组,需要使用strcpy函数。
在c++中,请使用std::string类。其他一切都是等待发生的错误。我刚刚注意到你有一个下一个指针。如果这不是链表的家庭作业,请考虑使用std::list或std::vector这样的容器。
你不能将一个数组赋值给另一个数组(这看起来像你想要的),也不能将指针赋值给一个数组(这是你正在尝试的)。你需要复制这些元素。
在函数签名上,当调用该函数时,数组将衰减为指针,我通常发现让代码看起来像编译器处理的代码有助于代码的可读性。构造函数签名是:
Student(int nr_ID, char *nazwisko, char *imie, double punkty)
可能需要考虑添加额外的size参数,以确定在第二个和第三个参数中传递给函数的内存块有多大。如果它们是纯字符串,则可以使用strncpy
初始化数组成员:
Student(int nr_ID,
char const *nazwisko, // make it 'const' you don't modify the argument
char const *imie, // same here
double punkty)
: nr_ID(nr_ID), punkty(punkty) // prefer initializer lists
{
strncpy(this->nazwisko, nazwisko, 39); this->nazwisko[39] = 0;
strncpy(this->imie, imie, 39); this->imie[39] = 0;
}
您没有确定nazwisko
存储字符串或字符序列!
我假设它是一个字符序列。(不是c风格的字符串)
使用std::memcpy
或std::copy
Student(int nr_ID, char nazwisko[40], char imie[], double punkty){
//...
std::memcpy(this->nazwisko, nazwisko, 40);
or
std::copy(nazwisko, nazwisko+40, this->nazwisko);
//...
}
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 使用指针初始化 char 数组或字符串 C++
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 对象初始化后在C++中显示 char 数组时的异常行为
- c ++ 如何在构造函数中初始化 char
- 无法使用类型 'const char *' 的左值初始化类型 'char *' 的成员子对象
- 以这种方式初始化的 char 数组是否会自动添加空终止符?
- 为什么 char 可以在 std::array 中初始化为 nullptr,而不是单独初始化?
- 使用 const char* 初始化 const ref 字符串成员时幕后会发生什么
- 如何使用 C++ 中的原语初始化类(如 std::字符串从 const char* 初始化)
- C++ 如何检查 char 变量是否未定义(未初始化)
- 在这种情况下,如何从类型 "const char*" 初始化字段?
- 为什么通过添加字符串文字和char初始化时,为什么不打印字符串
- 从二维数组中的char*初始化c++字符串时,读取大小为1的无效值
- 使用 char 初始化多维数组
- c++中的char初始化构造函数
- 如何从vector char初始化stringstream
- c++11-使用{}构造函数直接从char*初始化std::string
- 用const char初始化2D char数组元素
- 如何从const unsigned char*初始化std::istringstream,无需强制转换或复制