通过构造函数初始化char[40]

Initiliaizing char[40] through the constructor

本文关键字:char 初始化 构造函数      更新时间:2023-10-16

我想初始化对象属性,但它一直说类型不匹配。如何纠正?

#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::memcpystd::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);
        //...
    }