【问题标题】:Missing template arguments before '.' token'.' 之前缺少模板参数令牌
【发布时间】:2011-11-04 02:34:53
【问题描述】:

我正在尝试将我的程序组织成函数并遇到了这个问题,

错误:“'.' 之前缺少模板参数令牌”

一旦我尝试在函数中运行代码,如果它只是在 main() 中,它就可以正常工作。任何熟悉此错误的人都知道问题可能是什么?

注意,注释掉的代码删除了错误但与有序列表class混淆并重置其长度或其他东西,导致orderedlist.getlength()函数为return 0,这使得while()中没有任何代码循环执行。

功能

void rentFilm(char* filmId, char* custId, char* rentDate, char* dueDate, int numFilm)
{
    //orderedList <filmType> orderedList(numFilm);
    //filmType newItem;
    int index = 0;
    bool found = false;

    while (index < orderedList.getLength() && !found)
        {
            cout << "test" << endl;
        if (strncmp(filmId,orderedList.getAt(index).number,6) == 0 && strncmp("0000",orderedList.getAt(index).rent_id,5) == 0)//If that film is rented by NO customer
            {
                cout << "test" << endl;
                found = true;//customer can rent it
                strcpy(newItem.number,filmId);
                orderedList.retrieve(newItem);
                orderedList.remove(newItem);
                strcpy(newItem.rent_id,custId);
                strcpy(newItem.rent_date,rentDate);
                strcpy(newItem.return_date,dueDate);
                orderedList.insert(newItem);
                cout << "Rent confirmed!" << endl;
            }
        else
            {
                if (strncmp(filmId,orderedList.getAt(index).number,6) > 0 || strncmp("0000",orderedList.getAt(index).rent_id,5) > 0)
                    {
                        ++ index;
                    }
                else
                    {
                     throw string ("Not in list");
                    }
            }
        }
}

插入orderedList类(长度已确定)

template <class elemType>
void orderedList<elemType>::insert(const elemType& newItem)
{
     int index = length - 1;
     bool found = false;

     if (length == MAX_LIST)
         throw string ("List full - no insertion");

         // index of rear is current value of length

     while (! found && index >= 0)
        if (newItem < list[index])
        {
            list[index + 1] = list [index];  // move item down
            --index;
        }
        else
            found = true;

     list [index + 1] = newItem;  // insert new item
     ++length;
}

填充列表的 main 中的代码:

filmFile.open("films.txt", ios::in);
filmFile >> numFilm;
filmFile.get();

orderedList <filmType> orderedList(numFilm);
filmType newItem;

readString(filmFile, newItem.number,5);
    for (int i = 0; i < numFilm; i++)
    {
         newItem.copy = filmFile.get();
     readString(filmFile, newItem.title,30);
         readString(filmFile, newItem.rent_id,4);
         readString(filmFile, newItem.rent_date,8);
         readString(filmFile, newItem.return_date,8);
         filmFile.get();

         orderedList.insert (newItem);//puts filmType struct into the ordered list.

         readString(filmFile, newItem.number,5);
    }

如果程序中其他任何地方的代码有助于评估此错误,请告诉我。

【问题讨论】:

  • orderedlist 是什么?是class 吗?
  • 是的,它是一个类,orderedList(numFilm)是一个构造函数
  • 这段代码似乎没有任何问题。您可能想为class orderlist 提供minimal 代码。
  • 添加了一些相关代码,有帮助吗?
  • 您还没有准备好使用模板。回去研究一下类和类的实例之间的区别。

标签: c++ templates compiler-errors


【解决方案1】:

看起来你注释掉的那行声明了一个与类同名的变量。

因此,当您将其注释掉时,该类的静态函数将被调用。

将声明更改为:

orderedList<filmType> filmList(numFilm);

然后将函数中orderedList的所有引用改为filmList

【讨论】:

  • 这与将我的列表长度重置为 0 的效果相同,就像 orderedList 没有重命名为其他东西一样。
  • 我的猜测是您需要将所有元素实际插入到列表中以使其长度不为零。我对您正在实例化的类一无所知,但我敢打赌,您传递给构造函数的参数只是暗示要在向量中预分配多少元素,因此它不需要增长向量。
  • 对,就是这样,长度是确定的。随着项目被插入到类中,长度增加。
  • 在声明类和使用类之间没有插入项目的代码,所以它的长度最好是零。也许您希望它是静态的或全局的?
  • 我在 main 中插入项目,如果此代码在 main 中,它会正常工作。我现在要做的就是将程序分解为函数,我在这个函数中有这个声明的唯一原因是因为没有它“在'之前缺少模板参数'。令牌”错误被抛出
【解决方案2】:

问题是您正在创建与模板同名的变量吗?当你说,

orderedList<filmType> orderedList(numFilm);

这(有点)就像说,

int int=42;

然后期待int+1 返回43

试试类似的,

orderedList<filmType> ol(numFilm);

并将所有其他对orderedList 的引用更改为ol

【讨论】:

  • 这类似于 moishes 建议,它与我程序中的数据混淆。我在 main 中使用 orderedList &lt;filmType&gt; orderedList(numFilm);,它似乎工作正常
  • 知道orderedList(numFilm); 是我的orderedLists 构造函数在这里有什么意义吗?
  • @mwmnj:不,不是,它是从构造函数创建的变量。
【解决方案3】:

您似乎在main() 中填充了一个变量orderedList,然后当您使用相同的名称声明时,期望它在rentFilm(...) 中自动可用;这是不可能的。您必须将对象从main() 或更好地传递给函数,以使该函数成为class orderedList 的成员方法:

int main ()
{
  orderedList<filmType> ol(numFilm); // variable name different (good practice)
  ... // all the populating
  orderedList.rentFilm(...);  // call the function like this
}

其中,rentFilem() 现在是 class 的一部分

class orderedList {
...
public:
  void rentFilm(char* filmId, char* custId, char* rentDate, char* dueDate, int numFilm);
};

现在在函数内部,您不必为orderedList 声明变量;只需使用this-&gt;&lt;method/variable&gt;。它应该可以工作。

【讨论】:

  • 您需要. 左侧的对象名称,而不是类模板。这就是问题要问的问题。
  • @BenVoigt,如果 OP cmets 代码,就会出现这个问题。在问题的后半部分,OP 在问,“如果代码未注释,为什么它不起作用”? (实际上编译器允许模板类名和对象同名)
  • 是的,但我说的是你的“固定”代码,它有同样的问题。评论“像这样调用函数”真的不是。
猜你喜欢
  • 2018-10-11
  • 2015-09-12
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多