-1

I am trying to create a constructor that sets an array field to null. Im getting a "no viable overloaded '=' " error

    Horse::Horse()
    {
        string *ptr;
        ptr = NULL;
        Name[SIZE] = ptr;

    }

My question is what exactly is going on behind the scenes here. I thought I could create a string pointer, set it to null, then set the array to the pointer and it would make the first element of the array equal to null?

Thanks

fferri
  • 18,285
  • 5
  • 46
  • 95
Marty86
  • 3
  • 5
  • The declaration of `Name` might just be relevant. – Alan Stokes Apr 26 '15 at 20:38
  • 1
    An array can't be set to NULL, because an array is not a pointer. But you can set the *elements* of an array to something. – juanchopanza Apr 26 '15 at 20:38
  • Name is declares as a string array with 20 elements string Name[20]; – Marty86 Apr 26 '15 at 20:41
  • That's a different type than what you're trying to assign (`string` != `string*`). Maybe just use a `std::vector` or `std::array` if you want to assign to the array? – Emil Laine Apr 26 '15 at 20:42
  • "then set the array to the pointer" You can't do that. While an array can decay to a pointer, a pointer can't "decay" to an array. – Emil Laine Apr 26 '15 at 20:48
  • What type is `Name`? – phantom Apr 26 '15 at 20:49
  • "it would make the first element of the array equal to null" That doesn't make sense since your array is a `std::string` array, and `std::string` can't be "null". If you mean an empty string, it's already empty. The default `std::string` constructor constructs empty strings. – Emil Laine Apr 26 '15 at 20:50
  • Disclaimer** This is a school project. I can only assume that by "set the Name field to null" with in the constructor, means each time you create an object from the class, it starts the name array at the zero address. @Phantom Name is a string array with 20 elements – Marty86 Apr 26 '15 at 20:57
  • If name is a `char` array, you are trying to do `char = std::string*` when you do `Name[SIZE] = ptr`. – phantom Apr 26 '15 at 21:04
  • Maybe it's a C assignment and the Name field should be `char*` (i.e. C-string), not an array of `std::string`s? You can set the char pointer to null. A char array or a `std::string` can't be set to null, only pointers can. – Emil Laine Apr 26 '15 at 21:04

2 Answers2

0

I'll just go ahead and write the whole thing for you.

  • If you think about what a name is, it's just a string, a string of characters. So Name should be a std::string. Then the constructor would look like this:

    Horse::Horse() {}
    

    Done. Name will be initialized to an empty string, i.e. "".

  • If the assignment requires you to use C-strings (char arrays) instead of std::string, then Name should be of type char*, if it needs to be set to NULL. Now the constructor would look like this:

    Horse::Horse() : Name(NULL) {}
    

    The Name field will be initialized to NULL, i.e. it will be a null pointer, hence it doesn't contain even an empty string.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • Im not sure how the format `Horse::Horse() : Name(NULL) {}` works. Maybe I have not learned that syntax with constructors yet Would it be incorrect to use `Name[Size] = '\0'` When a new instance is created does this set the address of the first element to null? – Marty86 Apr 26 '15 at 23:36
  • @Marty86 That's the [constructor initializer list](http://en.cppreference.com/w/cpp/language/initializer_list) where all member variables are initialized. Doing `membervar = something` in the constructor body isn't initialization, it's assignment, and therefore doesn't work if the member is non-assignable (e.g. const, or a reference). – Emil Laine Apr 27 '15 at 07:36
  • @Marty86 `Name[Size] = '\0'` sets the character at index `Size` to `'\0'` (i.e. the null character, which is used to terminate C-strings). – Emil Laine Apr 27 '15 at 07:38
  • Ahhh, I see. Thanks for explaining the difference. – Marty86 Apr 27 '15 at 15:24
0

Setting Name[SIZE] to point to the address of std::string * is meaningless.
If Name is a std::string array, then the array will be defaulted empty.

If your task was to set the Name field empty in the constructor, I'm going to assume this is a const char* instead of a std::string. This can be done using initializer list:

Horse::Horse()
: Name{0} // Name{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} does the same thing.
{}        // empty body

The above will initialize all the 20 positions to 0, or NULL if you like. Just as if you declared it in the global scope.
If you are not familiar with the initializer list in constructors, you can always do this, but at an extra cost:

Horse::Horse() // Initialize members runs first
{
    for (int i = 0; i != SIZE; ++i) // Assign values to Name array
        Name[i] = 0;
}

Side note: Instead of using macros to define your values,
a better choice is to use const unsigned SIZE = 20; for reasons described here

Community
  • 1
  • 1
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • Okay, that worked for me. (no compiler errors but I haven't tested it yet. I have yet to build my main function.) – Marty86 Apr 27 '15 at 15:26