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

  • SEARCH
  • Home
  • 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 7407173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:40:15+00:00 2026-05-29T05:40:15+00:00

I want to make a static vector in a class and I want to

  • 0

I want to make a static vector in a class and I want to resize the vectors when they are created. I’m trying to do this in the constructor or in the main function. But I can’t get it to work. The problem is that I can’t call a function of the vector class on this way.
This is what I have now:

#include <vector>

using namespace std;

class test
{
public:
    static vector<int> testvec;
    test();
};

test::test() //Not static
{
    test::testvec.resize(0);    //Try 1
}

vector<int> test::testvec.resize(0); //Try 2

int main()
{
    test::testvec.resize(0); //Try 3
    test testclass;


    system("pause");
    return false;

}

I need to handle all the data on the vector in every object, that is why I want to make the vector static.

Can someone help me with this?
Thanks!

Edit: grammer. Every method that I have tried gives a compile error.

  • 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-05-29T05:40:15+00:00Added an answer on May 29, 2026 at 5:40 am

    When you declare a static member attribute you also need to define it:

    class test {
    public:
       static std::vector<int> v; // declaration
    };
    std::vector<int> test::v; // definition, note: no `static` here
    

    You can optionally use a size to the constructor, which would avoid the need to resize:

    std::vector<int> test::v( 10 ); // definition, create it with size==10
    

    But you can still call resize from main if you prefer:

    int main() { 
       test::v.resize( 20 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this
I want to make a global vector of my own object class called Person.
I have style sheet with a class name changebackgroundcolor i want make change in
We are trying to make all our blocks and pages static so that designer
I want to make some static constants globally visible. I'm pretty familiar how to
I want to make an array static and also want to reference it in
I want to achieve something like this: class C { int m_nVal; public: C(int
I'm trying to create a generic function that removes duplicates from an std::vector. Since
I am trying to make a class that staggers the net connections calls by
I've been trying to do this for a while but haven't had much success.

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.