1

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.

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;
    }

};

1
  • "I am working on creating a linked list in c++", purely as an academic exercise, one hopes. The world doesn't need another C++ list class! Commented Feb 7, 2012 at 4:14

4 Answers 4

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.

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's not what the compiler is choking on(I am fairly sure the zero size array is a gcc extension). The compiler is failing on the assignement of the arrays(you can't copy arrays like that in C++).
The error is about assignment inside the constructor not due to passing of pointer to array.
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.

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

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.

1 Comment

memcpy would probably be the most "C-like" way.
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.

Comments

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 Comment

this will be more efficient as it avoids unnecessary copies. only problem arises if char *x goes out of scope. if such situation exists then you should use copy. and this being char array strcpy(dest,source) will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.