Ich bin neu in C++ und ich mache dieses GoFish-Projekt, aber ich bekomme die folgende Fehlermeldung und ich kann es einfach nicht beheben. Dies ist die Fehlermeldung:Fehler: Initialisierung Argument 1 von 'SetIterator <T> :: SetIterator (T *) [mit T = Std :: Basic_string <char>]'
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
Und das ist die testFind() Funktion:
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
}
und dies ist in der set.h Datei, Funktionen finden() und löschen():
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();
}
ich versuchte Vorlage für testFind() und erklärt ITErfahren als T statt int setzen, aber dann wird es nicht definierten Referenz sagen.
Hat jemand eine Idee von was könnte falsch sein?
Bitte lesen Sie [fragen] und geben Sie ein [mcve]. – rustyx