문제

I am writing a small program in Visual C++ 2010. I'm having some trouble implementing pure virtual functions inherited from some abstract class, when the classes in question are divided into *.h and *.cpp files.

This is the Sir.h file where I declare my classes and methods, Sir is a base class:

class Sir{
protected : Uint _count; 
            TVint _elemente; 

public : Sir();
         virtual ~Sir(){}
         Uint operator[] (int index);
         Sir& operator() (int index);
         friend ostream& operator << (ostream &out, const Sir &sir);
        
protected : virtual void CalculateValues(int index)=0;
}
class Fibonacci:public Sir{
enum { MAX_FIB = 47 };

using Sir::CalculateValues;
void CalculateValues(int index);
}

This is Sir.cpp :

 #include "Sir.h"
        
    Sir::Sir(){_count=0;}
        
    Uint Sir::operator[](int index)
                 {
                        if(index<0)
                            ParameterOutOfBounds();
                        else{_count=index+1;}
                        return _elemente[_count];
                 }
    Sir& Sir::operator()(int index){operator[] (index);}
        
    void Fibonacci::CalculateValues(int index){
            
            if(index<Fibonacci::MAX_FIB)
            {Fibonacci::_elemente.push_back(1);
             Fibonacci::_elemente.push_back(1);
             while(_elemente.size()<_count)
            Fibonacci::_elemente.push_back(_elemente[_elemente.size()-1]+_elemente[_elemente.size()-2]);
                }
            else ParameterOutOfBounds();
        
      }
ostream& operator << (ostream &out, const Sir &sir)
       {
          cout<<"Index  Value"<<endl;
          cout<<"--------------------"<<endl;
          for(int i=0;i<_count;i++)
          cout<<i<<"    "<<_elemente[_count]<<endl;
        }

This is the testSir.cpp file :

#include "Sir.h";
#include<iostream>;
#include"Calc\Exceptions.h";
using namespace std;

int main(){

     Sir * fib=new Fibonacci;
     fib->CalculateValues(1); //Error : function is inaccessible
     system("PAUSE");
     return 0;
}

I get the error : Error:function "Sir::CalculateValues" is inaccessible. How can i fix this ?

EDIT :

The protected was my bad . Now i have another problem at Sir * fib=new Fibonacci; I'm getting "Error: object of abstract class type "Fibonacci" is not allowed".

도움이 되었습니까?

해결책

The method is inaccessible since its declared as protected.

protected : virtual void CalculateValues(int index)=0;

Add it under public : :

public : Sir();
         virtual ~Sir(){}
         Uint operator[] (int index);
         Sir& operator() (int index);
         friend ostream& operator << (ostream &out, const Sir &sir);
         virtual void CalculateValues(int index)=0;

And the same for the Fibonacci class (only the relevant method)

a protected method is accessible only to the class declaring it or any of it derivatives

다른 팁

CalculateValues is declared as protected, so it can be accessed only from subclasses.of Sir class.

Declare it public if you want it to be callable from any other class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top