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 9299389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:32:57+00:00 2026-06-18T22:32:57+00:00

It is very important that my function is static, I need to access and

  • 0

It is very important that my function is static, I need to access and modify another static/non-static class member in order to print it out later. How can I do that?

Flow

  • Class is initiated
  • Constructor sets variable to something using internal function that must be static
  • Some time later I print that variable

Example code

#include <iostream>

class MyClass
{
public:
    static int s;
    static void set()
    {
        MyClass::s = 5;
    }

    int get()
    {
        return MyClass::s;
    }

    MyClass()
    {
        this->set();
    }
};

void main()
{
    auto a = new MyClass();

    a->set(); // Error

    std::cout << a->get() << std::endl; // Error

    system("pause");
}

Error

LNK2001: unresolved external symbol "public: static int MyClass::s" (?s@MyClass@@2HA)
LNK1120: 1 unresolved externals
  • 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-18T22:32:58+00:00Added an answer on June 18, 2026 at 10:32 pm

    You have declared your static variable, but you have not defined it.

    Non-static member variables are created and destroyed as the containing object is created and destroyed.

    Static members, however, need to be created independently of object creation.

    Add this code to create the int MyClass::s:

    int MyClass::s;
    

    Addendum:

    C++17 adds inline variables, allowing you code to work with a smaller change:

    static inline int s;  // You can also assign it an initial value here
           ^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need GUI control for audio file presentation. The language is not very important
I use one library which is very important and untouchable. The problem is that
It's important to note that I'm not looking for a rounding function. I am
In My project, I want to implement that when someone access ,need to popup
I need to make a function powers that takes a number n and returns
I know now, that if I need to get a recource in some static
There is very important shortcut for "Basic Code Completion" in IntelliJ IDE which assumed
It's not a very important question, but it's important for me. I'm using the
Memory management is a very important issue in iPhone. So I am asking a
I know Exception Handling is a very important thing and we are doing it

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.