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

Uri*_*che 0 c++ templates function linked-list

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

14 C:\Users\urielbertoche\Desktop\main.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;
Run Code Online (Sandbox Code Playgroud)

}

所有的包括已经完成,我的标题是:

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);
};
Run Code Online (Sandbox Code Playgroud)

功能代码是:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我真的无法理解为什么会发生这种错误.谁能帮我?谢谢.

Kon*_*hin 5


planilha<double> planilhaTeste();

此行声明一个函数planilhaTeste返回planilha <double>而不是planilha <double>类型的变量.只要你需要一个默认的ctor,只需从声明中删除空的parenthises:


planilha<double> planilhaTeste;