错误:初始化'SetIterator <t> :: SetIterator(T *)的参数1 [使用T = std :: basic_string <char>]'

时间:2017-10-16 20:21:53

标签: c++ c++11

我是c +的新手,我正在做这个GoFish项目,但我收到以下错误消息,我无法修复它。这是错误消息:

week05.cpp: In function ‘void testFind()’: week05.cpp:241:35: error: cannot convert ‘SetIterator<std::basic_string<char> >’ to ‘int’ in initialization int itFind = s1.find(text); ^ week05.cpp:245:28: error: invalid conversion from ‘int’ to ‘std::basic_string<char>*’ [-fpermissive] s1.erase(itFind); ^ In file included from week05.cpp:17:0: set.h:93:3: error: initializing argument 1 of ‘SetIterator<T>::SetIterator(T*) [with T = std::basic_string<char>]’ [-fpermissive] SetIterator(T* p) : p(p) {} ^ make: *** [week05.o] Error 1

这是testFind()函数:

void testFind()
{
    try
    {
        Set <string> s1;

        // fill the Set with text
        cout << "Enter text, type \"quit\" when done\n";
        string text;
    do
    {
        cout << "\t" << s1 << " > ";
        cin  >> text;
        if (text != "quit")
            s1.insert(text);
    }
    while (text != "quit");

    // make a copy of the set using the copy constructor
    Set <string> s2(s1);

    // look for an item in the set
    cout << "Find items in the set and delete.\n";
    cout << "Enter words to search for, type \"quit\" when done\n";

    cout << "\t" << s1 << " > ";
    cin  >> text;
    do
    {
        int itEmpty = -1;
        int itFind = s1.find(text);
        if (itFind != itEmpty)
        {
            cout << "\tFound and removed!\n";
            s1.erase(itFind);
        }
        else
            cout << "\tNot found\n";
        cout << "\t" << s1 << " > ";
        cin  >> text;
    }
    while (text != "quit");

    // show the list again
    cout << "The remaining set after the items were removed\n";
    cout << "\t" << s1 << endl;

    // show the list before the items were removed
    cout << "The items in the set before the items were removed\n";
    cout << "\t" << s2 << endl;
    }
    catch (const char * sError)
    {
       cout << sError << endl;
    }
  #endif // TEST3
 }

这是在set.h文件中,函数find()和erase():

template <class T>
SetIterator<T> Set <T> :: find(const T & t) const throw (const char *)
{
    SetIterator<T> loc;
    //Linear Search
    for(loc=begin(); loc!=end(); loc++)
    {
        if(*loc==t)
            return loc;
    }
    return loc;
}


template<class T>
void Set<T> :: erase(SetIterator<T> item)
{
    for (int i = 0; i < numItems; i++)
    {
        if (data[i] == *item)
        {
            data[i] = data[--numItems];
        }
    }
    sort();
}

我尝试将模板用于testFind()并将itFind声明为T而不是int,但它会说未定义的引用。

有没有人知道可能出现什么问题?

1 个答案:

答案 0 :(得分:0)

您的Set::find函数返回SetIterator<T>,其中T是您的集合中的类型。您尝试将SetIterator<T>分配给int,这是不可能的。您可以取消引用迭代器以获取它指向的值(*it),但是当您有一组string时,它会给您一个string }不是int

您没有为任何人提供足够的Set实施提供字符串建议,但您不应该将find的结果分配给int - 它应该被分配给SetIterator<string>并与相同类型的东西进行比较。