| |

VerySource

 Forgot password?
 Register
Search
View: 755|Reply: 7

Questions about the use of class member static variables

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-9 12:30:01
| Show all posts |Read mode
I want to use a class status, and class members use static variables to record real-time changing status information of the program.

However, in the initialization function of the dialog cmydialog, an error is reported when initializing its static variables:
unresolved external sysmbol: public static status My static variable member ...

If you delete #include "status.h" in cmydialog.cpp, you cannot recognize the status class. If you include an include statement, the above error is reported.

Does that hero give pointers?
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-4-4 17:45:01
| Show all posts
Static variables are initialized outside the class, for example:

//abc.h

class abc
{
public:
    static int i;
}

// abc.cpp

int abc :: i = 0; // to be initialized like this
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-5 16:30:01
| Show all posts
The upstairs makes sense, what I need to remind here is
In C ++, static variables are divided into declarations and definitions

In the case you said, you may only have declared, but you must define it in your cpp file
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-19 19:45:01
| Show all posts
Thank you for your help. I will talk about the situation more specifically. I hope to get a solution to the problem:

Mine is a dialog program, the main dialog file
CmyDialog.h CmyDialog.cpp
There is also a custom class that I generated with the application wizard
Cstatus.h Cstatus.cpp
In this class I defined four static variables
This is one of them: static bool connected;
I have a manually added global function file
common.h and common.cpp

I have the following code in common.cpp:

#include "Cstatus.h"

void Initialize ()
{
   Cstatus :: connected = false;
}

Call this function in the initialization function of the dialog box, the following code in the file CmyDialog.cpp
#include "common.h"
#include "CStatus.h"

CmyDialog :: OnInitDialog ()
{
   Initialize ();
}


The result will report an error
error LNK2001: unresolved external symbol "public: static bool Cstatus :: connected" (? connected @ StaticStatus @@ 2_NA)
Help inform the reason, or are there any good suggestions to implement the initialization of static variables in class Cstatus;
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-20 00:45:01
| Show all posts
Thank you for your help. I will talk about the situation more specifically. I hope to get a solution to the problem:
The above mentioned missed a little, this is corrected!
 
Mine is a dialog program, the main dialog file
CmyDialog.h CmyDialog.cpp
There is also a custom class that I generated with the application wizard
Cstatus.h Cstatus.cpp
In this class I defined four static variables
This is one of them: static bool connected;
I have a manually added global function file
common.h and common.cpp
 
I have the following code in common.cpp:
 
#include "Cstatus.h"
 

Cstatus status;
void Initialize ()
{
     Cstatus :: connected = false;
     //status.connected = false;
 
}
 
Call this function in the initialization function of the dialog box, the following code in the file CmyDialog.cpp
#include "common.h"
#include "CStatus.h"
 
CmyDialog :: OnInitDialog ()
{
     Initialize ();
}
 
 
The result will report an error
error LNK2001: unresolved external symbol "public: static bool Cstatus :: connected" (? connected @ StaticStatus @@ 2_NA)
Help inform the reason, or are there any good suggestions to implement the initialization of static variables in class Cstatus;
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-4-22 17:15:01
| Show all posts
Put bool Cstatus :: connected = false; at the beginning of the cpp file

Then put

void Initialize ()
{
     // Cstatus :: connected = false; Compare it. Just static member functions must be initialized outside the class
     //status.connected = false;
 
}
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-4-22 20:00:01
| Show all posts
E.g

Cstatus status;

bool Cstatus :: connected = false;

void Initialize ()
{
 
}
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-30 12:15:01
| Show all posts
I used a static structure in the class, according to the method you said it still does not work,
Now I maintain the entire global class as a state class, so I do n’t need static,
Thank you anyway
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list