-1

I want to set the an element of a dynamic array to nullptr ,but i get an error.



#include <iostream>
#include <vector>
using namespace std;

class Site {
//a class called site
public:
    int i{};
};

int main() {
    Site* p = new Site[10];
p[0].i = 5;
delete [0] p;

p[0]=nullptr;



}

I keep on getting the error,"no operator "=" matches these operands"

I tried searching on google but i couldn't find what i was looking for.

  • 2
    stop using raw pointers and new just use `std::vector sites(10);`. Time for you to study this : [learn.cpp introduction to std::vector](https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/) and cppreference [std::vector](https://en.cppreference.com/w/cpp/container/vector) – Pepijn Kramer Aug 16 '23 at 17:17
  • 1
    `p[0]` is NOT a pointer it is a `Site` so that's why you cannot assign `nullptr` to it. Also have a look at [C++ core guidelines R11 Avoid calling new/delete explicitly](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly). Current C++ has tools in the standard library to help you achieve this e.g. std::vector, std::make_unique etc. – Pepijn Kramer Aug 16 '23 at 17:19
  • Google, or any other web search engine, can't help you much until you know the right terminology. If you don't already have one, [get a good book](https://stackoverflow.com/q/388242/4581301), read it and work through the examples. When you're done you'll have a much better foundation and a much easier time with searches. – user4581301 Aug 16 '23 at 17:24
  • `delete [0] p;` does not destroy the first element of the array, and `p[0]` is not a pointer. You need to get a good book and stop guessing. – molbdnilo Aug 17 '23 at 09:02

3 Answers3

3

Your array doesn't contain pointers. Your array contains objects of type Site. And an object cannot, in general be assigned a value of type nullptr_t.

You could make the array contain pointers, by adding an extra layer of indirection.

Site** p = new Site*[10];

But if your goal is to make a value optional, you should use std::optional rather than the blunt instrument of raw pointers.

std::optional<Site>* p = new std::optional<Site>[10];

Now you can assign std::nullopt as a value to any of these slots.

Even better, you should avoid new-style allocation altogether and just use a modern std::vector.

std::vector<std::optional<Site>> p(10);

No new or delete required. Make the language work for you, instead of the other way round.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
0

In code : (And watch stop using using namespace)

#include <iostream>
#include <vector>

//using namespace std; <== Never use this     
class Site 
{
public:
    int i{};
};

int main() 
{
    // do not give variables meaningless names like 'p'
    std::vector<Site> sites(10);
    sites[0].i = 42;
    
    // range based for loop
    // const Site& is a constant reference to each element
    // in the vector. Meaning you have read, but not write
    // access 
    for (const Site& site : sites)
    {
        std::cout << site.i << " ";
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
0

C++ created a pointer to an array not an array of pointers, it is an equivalent to:

Site arr[10];

If you want:

Site* p[10];
p[0] = new Site();

and now p[0] == nullptr should not give an error

And to access a member of Site just type

p[0]->i = 7;

make sure to check if it is a nullpttr to avoid segmentation faults

if(p[0] != nullptr)
{
    p[0]->i = 7;
    std::cout << "p[0]->i was set to 7" << std::endl;
}