Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:15:25+00:00 2026-06-17T17:15:25+00:00

I want to initialize an array of std::string pointers of size that i get

  • 0

I want to initialize an array of std::string pointers of size that i get from a constructor. Also, i want to do the same for two int arrays but the code below doesn’t compile:

class MyQuickInitArray
{
public:
    MyQuickInitArray(int size)
    {
        if(size <= 0)
        {
            throw new std::exception;
        }

        _size = size;
        _counter = 0;
        A = new std::string[size];
        B = new int[size];
        C = new int[size];
    }

    std::string& operator[](int j) {
        if(j > _size)
        {
            throw std::out_of_range("out of range");
        }

        if(((B[j]<0) || (B[j]>=_counter)) || (C[B[j]]!=j))
        {
            // A[j] points to junk
            _counter++;
            A[j] = new std::string;
            B[j] = _counter-1;
            C[_counter-1] = j;
            return A[j];
        }

        // the cell was instantiated before
        return A[j];
    }

    ~MyQuickInitArray(){};


private:
    std::string* A[];
    int B[];
    int C[];
    int _size;
    int _counter;
};

How can i properly declare an array of size that i get from a ctor?

EDIT:

The error that occurs is:

incompatible types in assignment of ‘std::string* {aka std::basic_string<char>*}’ to ‘std::string* [0] 

And for the int arrays:

incompatible types in assignment of ‘int*’ to ‘int [0]’
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:15:26+00:00Added an answer on June 17, 2026 at 5:15 pm

    They are not valid way of declaring static arrays in C++, array size needs to be known at compile time. Below code can’t compile in standard C++ without some special extension.

    std::string* A[];
    int B[];
    int C[];
    

    If you are just playing with pointers that’s fine. However, you’d better think of using std::vector

    #include <vector>
    std::vector<std::string> A;
    std::vector<int> B;
    std::vector<int> C;
    

    I may rewrite your code to below:

    class MyQuickInitArray
    {
    public:
        MyQuickInitArray(int size)
          : A(size),
            B(size),
            C(size),
            size_(size),
            counter_(0)
        {     
        }
    
        std::string operator[](int j) const 
        {
            return A.at(j);
        }
    
    private:
        std::vector<std::string> A;
        std::vector<int> B;
        std::vector<int> C;
        int size_;
        int counter_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to initialize an array of size 5 pointers that holds pointers to
I want to initialize an array that has X two-dimensional elements. For example, if
I want to initialize an array and then initialize a pointer to that array.
I want to initialize an array of int and populate it with a range
I want to initialize array of c-strings with zero pointers in MSVC2010 // Foo.h
I want to initialize an double array with size 200, and its value is
I've a problem with numpy's array constructor. I want to initialize an 2-D array
What I just want is to initialize a string[] array constant by specifying not
Okay, I am a noob and want to get a array from my server
Given this code: void group::build(int size, std::string *ips){ /*Build the LL after receiving the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.