尝试构建基于模板的代码时出错

Error while trying to build template based code

本文关键字:代码 出错 于模板 构建      更新时间:2023-10-16

我试图写一些代码,使用模板,我已经尝试了几个小时了,但仍然无法解决这个错误:

14 C:UsersurielbertocheDesktopmain.cpp request for member 'defineConstante' in 'planilhaTeste', which is of non-class type 'planilha<double> ()()'

我现在的主要内容是:

int main (){
planilha<double> planilhaTeste();
unsigned int contador=0;
double number=0;
for(contador=0; contador<5; contador++)
{
      cout<<"Escreva a constante para a celula "<<contador<<endl;
      cin>>number;
      planilhaTeste.defineConstante(contador, number); // this is line 14 by the way
      planilhaTeste->primeiro=planilhaTeste->primeiro->prox;
      cout<<planilhaTeste.termoConstante;
}
return 0;

}

所有的内容都已经完成了,我的标题是:

template <class Type>
class planilha{
    protected:
        struct celula{
            double termoConstante;
            Type resultadoFinal;
            lista termos;
            int numCelula;
            celula *prox;
            celula():prox(NULL){};
            celula(double novoTermo, int numCel, celula *proxElo=NULL):termoConstante(novoTermo),
                    resultadoFinal(novoTermo), numCelula(numCel), prox(proxElo), termos(){};
        };
        celula *primeiro;
    public:
        planilha();
        planilha(const planilha<Type>& origem);
        ~planilha(void);
        planilha<Type> operator=(const planilha<Type>& origem);
        void defineConstante(int numCel, const Type& valor);
        bool insere_termo(unsigned int numCel, unsigned int refCel, double fator);
        void apagar(unsigned int num_cel);
};

,功能代码为:

template <class Type>
void planilha<Type>::defineConstante(int numCel, const Type& valor){
    celula * finder = primeiro;
    while(finder!=NULL){
        if(this->numCelula==numCel){
            this->termoConstante = valor;
            return;
        }
        finder=finder->prox;
    }
}

我真的不明白为什么会发生这个错误。有人能帮我吗?谢谢。


planilha<double> planilhaTeste();
之前

这行声明了一个函数planilhaeste返回planilha不是planil类型的变量。只要你在这里需要一个默认的函数,只要从声明中删除空括号:

<>之前 planilha<double> planilhaTeste;

main()函数中,将planilha<double> planilhaTeste(); 行更改为planilha<double> planilhaTeste;

还有其他一些错误,如:lista termos; //lista is not even a typedef

planilhaTeste->primeiro=planilhaTeste->primeiro->prox;planilhaTeste不是指针,而且primeiro是受保护的你试图在planilha

范围外访问它