this is my Node Structure;
class Node
{
public:
string data;
Node *next;
Node *child;
};
Node *
createList(string *arr, int n)
{
Node *head = NULL;
Node *tmp;
for (int i = 0; i < n; i )
{
if (head == NULL)
{
tmp = head = new Node();
}
else
{
tmp->next = new Node();
tmp = tmp->next;
}
tmp->data = arr[i];
tmp->next = tmp->child = NULL;
}
return head;
}
This is the problem where I get.
filesystem::path cwd = filesystem::current_path();
int file_count;
vector files = filesInDir(cwd, file_count);
string file_names[file_count];
string file_extensions[file_count];
string file_sizes[file_count];
for (int i = 0; i < file_count; i)
{
for (int i = 0; i < file_count; i)
{
file_names.push_back(files[i 0 * file_count]);
file_extensions.push_back(files[i 1 * file_count]);
file_sizes.push_back(files[i 2 * file_count]);
}
}
Node *head1 = createList(file_names, file_count);
Node *head2 = createList(file_extensions, file_count);
Node *head3 = createList(file_sizes, file_count);
main.cpp:54:24: error: request for member 'push_back' in 'file_names', which is of non-class type 'std::string [file_count]' {aka 'std::__cxx11::basic_string<char> [file_count]'}
54 | file_names.push_back(files[i 0 * file_count]);
| ^~~~~~~~~
I'm trying to read all files in the current directory, and save them into like vector((file_name_1, file_extension_1, file_size_1), ......)
(All of them in the string format). I need to use multi linked list to store them, how can I solve the problem?
CodePudding user response:
push_back()
is a method of standard C containers, like std::vector
. But you are trying to call it on C-style variable-length arrays instead, which do not have methods (and BTW, they are NOT part of standard C ).
Since you are already using std::vector
anyway for the return value of filesInDir()
, simply use std::vector<std::string>
instead of std::string[]
for your variable-length arrays, eg:
vector<string> file_names;
file_names.reserve(file_count);
// populate file_names as needed ...
Node *head1 = createList(file_names.data(), file_names.size());
...
Do the same with the file_extensions
and file_sizes
arrays, too.
Also, since you are using the standard library anyway (for std::string
, std::vector
, and std::filesystem
), consider using std::list
(double-linked) or std::forward_list
(single-linked) for the linked lists, instead of using a manual implementation.
CodePudding user response:
I solved it with;
for (int i = 0; i < file_count; i)
{
for (int i = 0; i < file_count; i)
{
file_names[i] = (files[i 0 * file_count]);
file_extensions[i] = (files[i 1 * file_count]);
file_sizes[i] = (files[i 2 * file_count]);
}
}
but this time I needed to add tail pointer to node structure, I'm trying to solve that
class Node
{
public:
string data;
Node *next;
Node *child;
};
Node *
createList(string *arr, int n)
{
Node *head = NULL;
Node *tmp;
for (int i = 0; i < n; i )
{
if (head == NULL)
{
tmp = head = new Node();
}
else
{
tmp->next = new Node();
tmp = tmp->next;
}
tmp->data = arr[i];
tmp->next = tmp->child = NULL;
}
return head;
}