2012-01-28 27 views
8

में 'इस' का अमान्य उपयोग मैंने कक्षा में काम किया था और उसी सीसीपी फ़ाइल में सबकुछ लिखना शुरू कर दिया था। हालांकि, थोड़ी देर के बाद मैं कक्षा को बड़ा और बड़ा हो सकता था इसलिए मैंने इसे एक .h और एक .cpp फ़ाइल में विभाजित करने का निर्णय लिया।गैर-सदस्य फ़ंक्शन

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 फ़ाइल:

#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; 
} 

हालांकि, मैं इस संकलन के लिए नहीं मिल सकता है। मैं चल रहा प्रयास करें:

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 

मैं यह क्यों उपयोग नहीं कर सकते ?? मैं इसे प्रेसिजनमेन फ़ंक्शन से उपयोग कर रहा हूं और यह संकलित करता है। क्या ऐसा इसलिए है क्योंकि वह समारोह गॉसियन लौटाता है? किसी भी अतिरिक्त स्पष्टीकरण की वास्तव में सराहना की जाएगी, मैं जितना कर सकता हूं उतना सीखने की कोशिश कर रहा हूं! धन्यवाद!

उत्तर

23

Gaussian कक्षा के हिस्से के रूप में आप absoluteDifference घोषित करना भूल गए।

बदलें:

double absoluteDifference (Gaussian aux){ 
इस के लिए

:

double Gaussian::absoluteDifference (Gaussian aux){ 

साइड नोट: बेहतर होगा संदर्भ के बजाय मूल्य द्वारा पारित करने के लिए हो सकता है:

double Gaussian::absoluteDifference (const Gaussian &aux){ 
+1

आह! मुझे विश्वास नहीं है कि मैं इसे नहीं देख सका! मैं इसे कई बार चला गया ...! धन्यवाद!! – coconut

+1

इसके अलावा, सलाह के अतिरिक्त टुकड़े के लिए धन्यवाद! – coconut