错误 C2355:'this':只能在非静态成员函数或非静态数据成员初始值设定项中引用
error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers
我在编译代码时遇到了一些问题。上面写着,
错误C2355:"this":只能在非静态成员内部引用函数或非静态数据成员初始化程序
错误显示在上的代码部分
double getR() {
return this->r;
}
double getG() {
return this->g;
}
double getB2() {
return this->b2;
}
也在这里
rez.r = this->r / 2 + a.getR() / 2;
rez.g = this->g / 2 + a.getG() / 2;
rez.b2 = this->b2 / 2 + a.getB2() / 2;
有什么想法吗?
这个问题已经解决了。
现在这部分代码出现相同错误。。。
rez.r = this->r / 2 + a.getR() / 2;
rez.g = this->g / 2 + a.getG() / 2;
rez.b2 = this->b2 / 2 + a.getB2() / 2;
它还说
错误C2227:"->r"的左侧必须指向类/结构/联合/泛型类型
您需要将类作用域添加到方法中,例如,如果您的类名为YourClass
,那么您的函数将是
double YourClass::getR() {
return this->r;
}
否则,getR
是一个自由函数,因此没有this
可操作。其他方法也是如此。
相关文章:
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- "Class1"类"Class2"对象作为私有数据成员。如何通过"Class 2"函数引用"Class1"对象?
- 引用类型的数据成员提供有关恒常正确性"loophole"
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- 具有引用数据成员的结构不是文本类型吗?
- 引用静态数据成员
- 为什么当类的数据成员更改为引用时输出不同
- 为什么INVOKE总是取消引用数据成员,而不是在可能的情况下调用
- 了解常量引用对非成本数据成员的分配
- C++对类数据成员的引用
- 移动具有常量数据成员或引用成员的类的ctor
- 在C++中,"static initialization fiasco"是否仅影响对另一个模块中定义的对象的数据成员的引用?
- 将基元类型的数据成员作为常量引用返回
- 如果类具有引用数据成员,为什么编译器不合成默认赋值运算符
- 组合常量和非常量引用数据成员的单个类
- 从成员函数引用数据成员时出错
- 基类静态引用数据成员上的C++对象切片行为的奇怪案例
- 具有引用数据成员的类的默认构造函数
- 引用数据成员和移动构造函数
- 如何处理赋值操作符和复制构造函数中的引用数据成员