C ++链接列表创建 - 传入数组

时间:2021-07-30 07:21:57

I am working on creating a linked list in c++, and I can't figure out how to pass an array as an argument in the constructor, or if thats even legal syntax.

我正在用c ++创建链表,我无法弄清楚如何在构造函数中将数组作为参数传递,或者甚至是合法的语法。

This is the error I get:

这是我得到的错误:

CheckTextFile.cpp: In constructor ‘Node::Node(char*, int)’:
CheckTextFile.cpp:19: error: incompatible types in assignment of ‘char*’ to ‘char [0u]’
CheckTextFile.cpp: In constructor ‘Node::Node(char*, int, Node*)’:
CheckTextFile.cpp:24: error: incompatible types in assignment of ‘char*’ to ‘char [0u]’

Here is my code:

这是我的代码:

class Node{
public:
    int length;
    char data[];
    Node * next;
    Node(char x[], int y){
        data = x;
        length = y;
        next = NULL;
    }
    Node(char x[], int y, Node * z){
        data = x;
        length = y;
        next = z;
    }

};

4 个解决方案

#1


3  

Your argument passing is fine. However, your:

你的论点传递很好。但是,你的:

char data[];

declares an array with no size, so it's not surprising that the compiler refuses to generate code to put anything in there. Perhaps try:

声明一个没有大小的数组,因此编译器拒绝生成代码以放置任何内容并不奇怪。也许试试:

std::string data;

This is assuming that your x[] represents a NUL-terminated C string.

这假设您的x []表示NUL终止的C字符串。

After you do that, learn about the member initialisation syntax for constructors.

执行此操作后,请了解构造函数的成员初始化语法。

#2


1  

You are passing an pointer to the first element of the array, and actually that is correct.

您正在传递指向数组的第一个元素的指针,实际上这是正确的。

The compiler is complaining about assignment inside the constructor:

编译器抱怨构造函数内的赋值:

data = x;

You cannot assign arrays as such, not like objects, you will have to copy each element from the source array to the target array.
Either using a looping construct or using std::copy.

您不能像这样分配数组,而不是像对象一样,您必须将每个元素从源数组复制到目标数组。使用循环结构或使用std :: copy。

A trivial way(efficient would be to use std::copy) of doing so:

这样做的一个简单的方法(有效的方法是使用std :: copy):

Node(char x[], int y)
{
     for(int i = 0;i<y; ++i)
     { 
         data[i] = x[i];
     }
     length = y;
     next = 0;
}

Or Simply

std::copy(x,x+y,data);

And it will compile cleanly.

它会干净地编译。

On a side note, you are much better off using std::string rather than using char arrays.

另外,使用std :: string而不是使用char数组会更好。

#3


0  

First of all, in C++, you can't have an array of unspecified size. Also, you can use a pointer instead of an array or std:string.

首先,在C ++中,您不能拥有未指定大小的数组。此外,您可以使用指针而不是数组或std:string。

#4


0  

class Node{
public:
    int length;
    char *ptr_data;  //pointer 
    Node * next;
    Node(char *x, int y){
        ptr_data = x;  //pointer assignment
        length = y;
        next = NULL;
    }
    Node(char *x, int y, Node * z){
        ptr_data = x;    //pointer assignment
        length = y;
        next = z;
    }
};

#1


3  

Your argument passing is fine. However, your:

你的论点传递很好。但是,你的:

char data[];

declares an array with no size, so it's not surprising that the compiler refuses to generate code to put anything in there. Perhaps try:

声明一个没有大小的数组,因此编译器拒绝生成代码以放置任何内容并不奇怪。也许试试:

std::string data;

This is assuming that your x[] represents a NUL-terminated C string.

这假设您的x []表示NUL终止的C字符串。

After you do that, learn about the member initialisation syntax for constructors.

执行此操作后,请了解构造函数的成员初始化语法。

#2


1  

You are passing an pointer to the first element of the array, and actually that is correct.

您正在传递指向数组的第一个元素的指针,实际上这是正确的。

The compiler is complaining about assignment inside the constructor:

编译器抱怨构造函数内的赋值:

data = x;

You cannot assign arrays as such, not like objects, you will have to copy each element from the source array to the target array.
Either using a looping construct or using std::copy.

您不能像这样分配数组,而不是像对象一样,您必须将每个元素从源数组复制到目标数组。使用循环结构或使用std :: copy。

A trivial way(efficient would be to use std::copy) of doing so:

这样做的一个简单的方法(有效的方法是使用std :: copy):

Node(char x[], int y)
{
     for(int i = 0;i<y; ++i)
     { 
         data[i] = x[i];
     }
     length = y;
     next = 0;
}

Or Simply

std::copy(x,x+y,data);

And it will compile cleanly.

它会干净地编译。

On a side note, you are much better off using std::string rather than using char arrays.

另外,使用std :: string而不是使用char数组会更好。

#3


0  

First of all, in C++, you can't have an array of unspecified size. Also, you can use a pointer instead of an array or std:string.

首先,在C ++中,您不能拥有未指定大小的数组。此外,您可以使用指针而不是数组或std:string。

#4


0  

class Node{
public:
    int length;
    char *ptr_data;  //pointer 
    Node * next;
    Node(char *x, int y){
        ptr_data = x;  //pointer assignment
        length = y;
        next = NULL;
    }
    Node(char *x, int y, Node * z){
        ptr_data = x;    //pointer assignment
        length = y;
        next = z;
    }
};