Pracowałem na zajęciach i zacząłem pisać wszystko w tym samym pliku .cpp. Jednak po pewnym czasie mogłem zobaczyć, jak klasa się powiększa, więc postanowiłem podzielić ją na plik .h i .cpp.Nieprawidłowe użycie "this" w funkcji nie należącej do członka
plik 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:
#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;
}
Jednak nie mogę tego skompilować. Staram uruchomiony:
g++ -I. -c -w gaussian.cpp
Ale otrzymuję:
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
Dlaczego nie mogę korzystać z tego ?? Używam go w funkcji fromPrecisionMean i kompiluje. Czy to dlatego, że funkcja zwraca Gaussa? Wszelkie dodatkowe wyjaśnienia będą naprawdę mile widziane, staram się uczyć jak najwięcej! Dzięki!
Ach! Nie mogę uwierzyć, że NIE mogłem tego zobaczyć! Przeszedłem to tyle razy ...! Dziękuję Ci!! – coconut
Dzięki za dodatkową poradę! – coconut