c++ (非内置/类)静态成员
#include <iostream>
#include <string>
class c1
{
public:
static std::string m1;
static unsigned int m2;
};
//std::string c1::m1 = std::string;
unsigned int c1::m2 = 0;
void main()
{
c1 a;
//std::cout<<a.m1<<std::endl;
std::cout<<a.m2<<std::endl;
}
在此程序中,启用两行注释行会导致第一行出错。
错误 C2275:'std::string':非法使用此类型作为表达式
我做错了什么?
#include <iostream>
#include <string>
class c1
{
public:
static std::string m1;
static unsigned int m2;
};
//std::string c1::m1 = std::string;
unsigned int c1::m2 = 0;
void main()
{
c1 a;
//std::cout<<a.m1<<std::endl;
std::cout<<a.m2<<std::endl;
}
In this program enabling the two remarked lines causes an error on the first.
error C2275: 'std::string' : illegal use of this type as an expression
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为“std::string”是一种类型,而不是一个值。这是一个可能使这一点更加明显的示例:
Because "std::string" is a type, not a value. Here is an example that might make this more obvious:
该错误说明了一切,您正在使用 type
std::string
作为要分配的值。要解决这个问题,你可以这样做:
或者只是
The error says it all, you are using the type
std::string
as the value to be assigned.To fix this you can do:
or just
应该是这样的
should be something like
该错误是由于该行右侧使用了
std::string
- 您试图将 m1 的值初始化为类型< /em>std::string
。您应该会发现像
std::string c1::m1 = "Wee - a string!";
这样的行可以工作。The error is due to the right-hand use of
std::string
on that line - you're trying to initialise the value of m1 to the typestd::string
.You should find that a line like
std::string c1::m1 = "Wee - a string!";
will work.