0

I want to add an object to an array. I'm trying to do it in the constructor within this. I'm starting to learn c++ and I'm not sure why it doesn't work in this way. Can someone explain why it won't work and how to do it properly?

#include <iostream>

using namespace std;

const int CAPACITY = 50;

class Photo
{
    private:
        string title, description, size;
        int year, month;
        static Photo collection[CAPACITY];
        static int collection_size;

    public: 
        Photo(string title, string description, string size, int year, int month)
            :title(title), description(description), size(size), year(year), month(month)
        {
            collection[collection_size++] = this;
        }

        Photo()
            :Photo("", "", "1920x1080", 2021, 1)
        {
        }

        static int get_collection_size(){
            return collection_size+1;
        }

        static void print_collection(){
            for(auto photo : collection){
                cout << photo.size;
            }
        }
};

Photo Photo::collection[CAPACITY] = {};
int Photo::collection_size = 0;


int main(){
    Photo p;
    p.print_collection();
    return 0;
}
1
  • Not an answer, but I'd seriously recommend looking into references (&) Commented Mar 28, 2021 at 18:14

2 Answers 2

2

Your constructor is trying to store a Photo* pointer into an array of Photo objects. You would have to either

  • dereference the this pointer to assign the Photo object being pointed at.

collection[collection_size++] = *this;

  • change the array to store Photo* pointers.

static Photo* collection[CAPACITY];

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

Comments

0

You can't define static Photo collection[CAPACITY]; inside the Photo class. You should probably create a class or structure for a single photo (that is, no collection, just one) and then create an array of that somewhere else, or maybe another class to hold and manage the collection. You may want to take a look at standard containers too. https://en.cppreference.com/w/cpp/container

Edit: Code example

class Photo
{
    std::string title, description, size;
    int year, month;
    ...
};

static Photo static_photo_collection[50];

or maybe

class Photo
{
    std::string title, description, size;
    int year, month;
    ...
};

class Photo_Collection
{
    Photo collection[50];
    ...
    // Constructor, destructor if you want
    // functions to add, remove, somehow manage the collection
};

// if you really wanted a static collection...
static Photo_Collection my_photo_collection;

3 Comments

Why can't you define static Photo collection[CAPACITY] inside the class? You absolutely can -- this is not the problem. There's also no need for a separate class/struct ...
@ChrisMM I'm almost sure that the compiler would complain about incomplete class definition or something, because it has not reached the end of the class definition (so it can't know the size of the class to create the array)
That's not true for a self-reference, no. See Remy's answer for what the issue was.

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.