-2

Hello, everyone! I'm trying to dynamically allocate space for a string pointer in C++, but I'm having much trouble.

The code I've written is, partially, this (it's about RadixSort-MSD):

class Radix
{
    private:
        int R = 256;
        static const int M = 15;
        std::string aux[];
        int charAt(std::string s, int d);
        void sortR(std::string a[]);
    public:
        void sortR(std::string a[], int left, int right, int d);
};

And here is the problematic part:

void Radix::sortR(std::string a[])
{
    int N = sizeof(a)/sizeof(std::string*);
    aux = new std::string[N];  //Here is the problem!
    sortR(a, 0, N-1, 0);
}

The error that apears when I try to compile my project is below, and it's about the variable "aux", which is a string pointer.

|15|error: incompatible types in assignment of 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'std::__cxx11::string [0] {aka std::__cxx11::basic_string<char> [0]}'|

I'm a completely noob brazilian C++ student. So I can't understand what the error message is saying.

Could you help me?

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 1
    Why use `new` at all?? – Jesper Juhl Sep 19 '20 at 16:22
  • 1
    What does `std::string aux[];` mean, as a declaration? This is not valid C++. Your description is somewhat unclear as to what this is supposed to accomplish. If this is meant to be a pointer, it should be `std::string *`. Even if so, `int N = sizeof(a)/sizeof(std::string*);` will not do what you think it does. You think it will give you the size of the array that gets passed as this parameter. It does not, because you'll be surprised to learn that the parameter to this function is not an array. See your C++ textbook for more information about array decaying. – Sam Varshavchik Sep 19 '20 at 16:24
  • please use Vector Reference --- https://stackoverflow.com/questions/17938166/displaying-a-vector-of-strings-in-c – Dickens A S Sep 19 '20 at 16:24
  • Jesper, I used "new" because the main string "a" will deal with a hole gigantic text. Sam, I thought std::string aux[] would be the same as std::string* aux. I will try to explain better my problem. Thank you all guys, but the problem keeps alive. – Enzo Bonacina Sep 19 '20 at 16:29
  • 1
    Please stop changing the code in the question. SO is not a forum, the point of it is to create a database of questions & answers to help future visitors. If you change the question after getting an answer, the thread won't make any sense to future readers. – HolyBlackCat Sep 19 '20 at 16:55
  • @HolyBlackCat You are right. I was editing because this is how we do in my university. – Enzo Bonacina Sep 19 '20 at 17:29
  • New problems came to light inside my insertion_sort and sortR functions. I've updated my question. Any help and suggestions are welcome. By the way, I'm sorry for the mess I did in the main question. Living and learning :) – Enzo Bonacina Sep 19 '20 at 18:08
  • @EnzoBonacina why do you continue to use SO like a forum. You have been already told that this is a Q&A platform, and has to be used accordingly. – t.niese Sep 19 '20 at 18:27
  • @t.niese You are absolutely right! This is my first experience in this type of platform and, of course, I'm making many mistakes. This is how I will learn how to properly use it. I just need time and people who want to help, and I have both right now. – Enzo Bonacina Sep 19 '20 at 18:38
  • @EnzoBonacina You were already told what you should not do: changing the question in a way that the original problem isn’t present anymore. But you still continued to do that. Further more one a q&a platform a question must only target one problem a time. SO has a tour, help and how to ask section you have to read. – t.niese Sep 19 '20 at 21:11
  • @t.niese I will read that section right now, thank you. By the way, could you teach me how to use a std::vector? I have the parameter `const std::vector& C` in a function and I would like to pass it's value to a separeted string variable. – Enzo Bonacina Sep 19 '20 at 21:32
  • @EnzoBonacina once again, read how this platform works. The comments section is not there to ask new questions. It is only there to clarify ambiguities regarding the question/answer. – t.niese Sep 20 '20 at 07:37

1 Answers1

2

Use a std::vector. Change this

std::string aux[];

to this

std::vector<std::string> aux;

and this

void Radix::sortR(std::string a[])
{
    int N = sizeof(a)/sizeof(std::string*);
    aux = new std::string[N];  //Here is the problem!
    sortR(a, 0, N-1, 0);
}

to this

void Radix::sortR(const std::vector<std::string>& a)
{
    aux.resize(a.size());  //No problem!
    sortR(a, 0, a.size()-1, 0);
}

You'll also have to change the other version of sortR to use vectors instead of pointers.

Your code could not work because you cannot pass an array to a function in C++, so this code sizeof(a)/sizeof(std::string*) does not work because inside your sortR function a is a pointer.

As general rule you should not use arrays, pointers or new in C++ programs. Of course there are lots of exceptions, but your first choice should be to use std::vector instead.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you, John! I'll test your suggestion. – Enzo Bonacina Sep 19 '20 at 16:39
  • Well, now it apears `error: 'vector' in namespace 'std' does not name a template type|` for the declaration `std::vector aux;`, and it says "a" was not declared in the scope of `void Radix::sortR(const std::vector& a)`. Should I add a library or something like that? – Enzo Bonacina Sep 19 '20 at 16:55
  • @EnzoBonacina *"[Defined in header ](https://en.cppreference.com/w/cpp/container/vector)"*. – HolyBlackCat Sep 19 '20 at 16:57
  • Things are getting better! I will read again my hole code and adapt things. – Enzo Bonacina Sep 19 '20 at 17:01