'.' 之前缺少模板参数代币

发布于 2024-12-13 14:51:36 字数 3235 浏览 2 评论 0原文

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

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

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

请注意,注释掉的代码消除了错误,但会扰乱有序列表 class 并重置其长度或其他内容,导致 orderedlist.getlength() 函数返回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);
    }

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

I am trying to organize my program into functions and have ran into this,

error: "missing template arguments before '.' token"

once I try to run the code in the function, it works fine if its just in main(). Anyone familiar with this error know what the issue may be?

Note, the commented out code removes the error but messes with the ordered list class and resets its length or something, causing the orderedlist.getlength() function to return 0, which makes none of the code in the while() loop execute.

function:

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");
                    }
            }
        }
}

Insert in orderedList class (where length is determined):

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;
}

code in main where list is filled:

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

Please let me know if code from anywhere else in the program would be helpful in assessing this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江城子 2024-12-20 14:51:36

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

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

将声明更改为:

orderedList<filmType> filmList(numFilm);

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

It looks like the line you commented out declares a variable with the same name as a class.

So when you comment it out, static functions of that class are getting invoked.

Change the declaration to something like:

orderedList<filmType> filmList(numFilm);

and then change all the references of orderedList in the function to filmList.

╭ゆ眷念 2024-12-20 14:51:36

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

orderedList<filmType> orderedList(numFilm);

这(有点)就像说,

int int=42;

然后期望 int+1 返回 43

尝试类似的操作,

orderedList<filmType> ol(numFilm);

并更改所有其他对 orderedList 的引用,到ol

Is the problem that you are creating a variable with the same name as the template? When you say,

orderedList<filmType> orderedList(numFilm);

that's (sort of) like saying,

int int=42;

and then expecting int+1 to return 43

Try something like,

orderedList<filmType> ol(numFilm);

And changin all the other references to orderedList, to ol.

酒与心事 2024-12-20 14:51:36

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

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

其中,rentFilem()< /code> 现在是 class 的一部分

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

现在在函数内部,您不必为 orderedList 声明变量;只需使用this-><方法/变量>。它应该有效。

It seems that you populate a variable orderedList in main() and then expect it to be automatically available in rentFilm(...) when you declare with the same name; that is not possible. You have to pass the object to the function from main() or better to make that function as member method of the class orderedList:

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

where, rentFilem() is now part of the class

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

Now inside the function, you don't have to declare variable for orderedList; just use this-><method/variable>. It should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文