c++ - 在main中增加类的静态成员 - C++
问题描述
我收到此错误error: expected unqualified-id before ‘++’ token 29 | std::cout<<x::foo::++z;
基本上我正在尝试z
从 main 中的 foo递增
#include <iostream>
namespace x {
class foo {
public:
void bar1(foo& f) {
++x;
}
friend void bar2(foo& f);
int x;
int y;
static int z;
};
int x::foo::z=15;
void bar2(foo& f) {
f.y++; // Error: y not in scope
}
}
int main() {
x::foo a;
++a.x=5;
std::cout << ++a.x << std::endl;
std::cout << x::foo::z;
std::cout << x::foo::++z; //throws error
return 0;
}
z 是静态成员。
我的问题是如何访问和增加静态成员z
?也可以在 C++ 中的类之外完成声明,不知何故?
解决方案
您必须首先指定操作(++
运算符),然后指定要调用运算符的对象x::foo::z
( ):
// Increment the x::foo::z
++x::foo::z;
// Equivalent to ++(x::foo::z)
推荐阅读
- geometry - 半圆/矩形碰撞检测
- ruby-on-rails - RailsAdmin 枚举显示记录的现有值
- javascript - 如何从 LocalStorage 读回数据
- python-3.x - python3.4.2是否有单独的nltk版本可供下载?
- string - 匹配两个熊猫数据框值
- google-apps-script - 表格插件 Chrome 网上应用店到 G 套件迁移:更改可见性?
- firebase - 如何修复照片上传错误到 Firebase 存储
- java - Unlimited Guessing Game Java 0-100 需要打印出猜对了多少次
- openstack - cinder 将公共端点重定向到 http
- c# - 如何在一个for循环中将矩阵放入数组中?