tuan nguyen
tuan nguyen

Reputation: 33

Error when declare array struct in header file c++

Header file:

class SourceManager{

    typedef   struct  {
        const char  *name;
        int size ;
        const char  *src;
    }imgSources;

public:
    imgSources *   img;

    SourceManager();

};

In cpp file:

SourceManager::SourceManager(){

    img ={
       {  "cc",
            4,
            "aa"
        }
    };   
}

It shows the error: - Cannot initialize a value of type 'imgSources *' with an lvalue of type 'const char [3]' -Too many braces around scalar initializer

How to fix it?

Upvotes: 2

Views: 96

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

Data member img is declared as having type of a pointer

imgSources *   img;

So in the constructor you need something like

SourceManager::SourceManager()
{

    img = new imgSources { "cc", 4, "aa" };
    //...
}

If you need to allocate an array of the structure then the constructor can look like

SourceManager::SourceManager()
{

    img = new imgSources[5] { { "cc", 4, "aa" }, /* other initializers */ };
    //...
}

Upvotes: 0

Related Questions