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

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;


}

所有的包含物都已经制作好了,我的标题是:

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();


该行声明了一个planilhaTeste函数,该函数返回planilha ,而不是planilha 类型的变量。只要您需要此处的默认ctor,只需从声明中删除空括号即可:

planilha<double> planilhaTeste;

关于c++ - 尝试构建基于模板的代码时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7868347/

10-12 14:58