C++ 在非成员函数中无效使用“this”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9047671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:13:55  来源:igfitidea点击:

Invalid use of 'this' in non-member function

c++compiler-errorsthisnon-member-functions

提问by coconut

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.

我在一个班上工作,并开始在同一个 .cpp 文件中编写所有内容。然而,过了一会儿,我看到这个类越来越大,所以我决定把它分成一个 .h 和一个 .cpp 文件。

gaussian.h file:

gaussian.h 文件:

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 file:

gaussian.cpp 文件:

#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

But I get:

但我得到:

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

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!

为什么我不能用这个??我在 fromPrecisionMean 函数中使用它并编译。是因为那个函数返回一个高斯函数吗?任何额外的解释将不胜感激,我正在努力学习尽可能多的知识!谢谢!

回答by Mysticial

You forgot to declare absoluteDifferenceas part of the Gaussianclass.

您忘记声明absoluteDifferenceGaussian课程的一部分。

Change:

改变:

double absoluteDifference (Gaussian aux){

to this:

对此:

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){