| |

VerySource

 Forgot password?
 Register
Search
View: 642|Reply: 2

static problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-7 23:00:01
| Show all posts |Read mode
class point3d {
public:
      float x () {return _x;
private:
    static float _x;
};

main ()
{
    point3d rout;
    point3d * pt =&rout;
    cout << rout.x () << endl << pt-> x () << endl;
}

In the above program, an error LNK2001: unresolved external symbol "private: static float Point3d :: _ x" (? _X @ Point3d @@ 0MA) is always reported as an error. I can remove the static and it can run, please prawn instructions!
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-3-30 02:15:01
| Show all posts
STATIC member variables are placed outside the object and need to be defined
class point3d {
public:
      float x () {return _x;
private:
    static float _x;
};

float point3d :: _ x;
main ()
{
    point3d rout;
    point3d * pt =&rout;
    cout << rout.x () << endl << pt-> x () << endl;
}
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-30 10:00:01
| Show all posts
All static data is placed in the static area, it does not belong to any one object alone

But only the class in which it is declared can access it (in the private case), and it can only be defined once
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