Invalid use of #39;this#39; in non-member function(在非成员函数中无效使用“this)
问题描述
我曾在一个班级工作,并开始在同一个 .cpp 文件中编写所有内容.然而,过了一会儿,我看到这个类越来越大,所以我决定把它分成一个 .h 和一个 .cpp 文件.
I had working on a class and started writing everything in the same .cpp file. However, after a while I could see the class getting bigger and bigger so I decided to split it into a .h and a .cpp file.
gaussian.h 文件:
gaussian.h file:
class Gaussian{
private:
double mean;
double standardDeviation;
double variance;
double precision;
double precisionMean;
public:
Gaussian(double, double);
~Gaussian();
double normalizationConstant(double);
Gaussian fromPrecisionMean(double, double);
Gaussian operator * (Gaussian);
double absoluteDifference (Gaussian);
};
gaussian.cpp 文件:
gaussian.cpp file:
#include "gaussian.h"
#include <math.h>
#include "constants.h"
#include <stdlib.h>
#include <iostream>
Gaussian::Gaussian(double mean, double standardDeviation){
this->mean = mean;
this->standardDeviation = standardDeviation;
this->variance = sqrt(standardDeviation);
this->precision = 1.0/variance;
this->precisionMean = precision*mean;
}
//Code for the rest of the functions...
double absoluteDifference (Gaussian aux){
double absolute = abs(this->precisionMean - aux.precisionMean);
double square = abs(this->precision - aux.precision);
if (absolute > square)
return absolute;
else
return square;
}
但是,我无法编译它.我试着跑步:
However, I can't get this to compile. I try running:
g++ -I. -c -w gaussian.cpp
但我明白了:
gaussian.cpp: In function ‘double absoluteDifference(Gaussian)’:
gaussian.cpp:37:27: error: invalid use of ‘this’ in non-member function
gaussian.h:7:16: error: ‘double Gaussian::precisionMean’ is private
gaussian.cpp:37:53: error: within this context
gaussian.cpp:38:25: error: invalid use of ‘this’ in non-member function
gaussian.h:6:16: error: ‘double Gaussian::precision’ is private
gaussian.cpp:38:47: error: within this context
为什么我不能用这个??我在 fromPrecisionMean 函数中使用它并编译.是因为那个函数返回一个高斯函数吗?任何额外的解释将不胜感激,我正在努力学习尽可能多的知识!谢谢!
Why can't I use this?? I am using it in the fromPrecisionMean function and that compiles. Is it because that function returns a Gaussian? Any extra explanation will be really appreciated, I am trying to learn as much as I can! Thanks!
推荐答案
您忘记将 absoluteDifference
声明为 Gaussian
类的一部分.
You forgot to declare absoluteDifference
as part of the Gaussian
class.
变化:
double absoluteDifference (Gaussian aux){
为此:
double Gaussian::absoluteDifference (Gaussian aux){
<小时>
旁注:通过引用而不是通过值传递可能更好:
Side Note: It might be better to pass by reference rather than by value:
double Gaussian::absoluteDifference (const Gaussian &aux){
这篇关于在非成员函数中无效使用“this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在非成员函数中无效使用“this"
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01