c++ - 变量具有不完整的类型'void'c ++
问题描述
我想出了一个学校作业的解决方案,但是我无法编译它。我有五个错误,我相信它们都源于我没有正确编写我的 void 函数。我对编码仍然很陌生,我无法弄清楚这一点。帮助将不胜感激,谢谢!
当前代码:
#include <iostream>
#include <string>
#include <algorithm>
void AddList(LinkedList<char>& List, std::string input)
{
for (int i = input.size()-1; i >=0; i--)
{
List.add(input[i]);
} // end for loop
} // end ListTester
bool ifSubString(LinkedList<char>& List, std::string input)
{
std::string fromTheList = List.toString();
return (fromTheList.find(subString) != std::string::npos);
}
void AppendList(LinkedList<char>& List, std::string input)
{
List.clear();
for (int i = input.size()-1; o >=0; i--)
{
List.add(input);
}
}
int main()
{
std::string userString, newstring, substring;
int choice;
char charInput;
LinkedList<char> listik;
std::cout << "Welcome to the linkedlist program\n" <<std::endl;
std::cout << "Please Enter the string before we start: ";
getline(std::cin, userString);
AddList(listik, userString);
std::cout << "The string \"" << userString << "\" has been added to the linked list.\n" << std::endl;
std::cout << "Menu: " << std::endl;
std::cout << "1. Find the length of your input." << std::endl;
std::cout << "2. Add a new string to the current string." << std::endl;
std::cout << "3. Find the index of the character." << std::endl;
std::cout << "4. Find matches in the string." << std::endl;
std::cout << "5. Quit and sleep" << std::endl;
std::cin >> choice;
std::cin.ignore();
while(choice!=5)
{
switch(choice)
{
case 1:
std::cout << "The List " << listik.toString() << " contains " << listik.getCurrentSize() << " elements." << std::endl;
std::cout << std::endl;
break;
case 2:
std::cout << "Please type the string you'd like to append to the current list: ";
getline(std::cin, newstring);
Appendlist(listik, (userString+newstring));
std::cout << "The list is \"" << listik.toString() << "\"." << std::endl; //AppendList function contains add method
std::cout << std::endl;
break;
case 3:
std::cout << "Please type the character you'd like to have the index of: ";
std::cin >> charInput;
std::cout << "The index of \"" << charInput << "\" is " << listik.getFrequencyOf(charInput) << std::endl;
std::cout << std::endl;
break;
case 4:
std::cout << "Please type the string you'd like to find in the current list: ";
getline(std::cin, subString);
std::cout << "The string \"" << subString << "\" is";
if (ifSubString(listik, subString)==false) std::cout << " not";
std::cout << "found in \"" << listik.toString() << "\"." << std::endl;
std::cout << std::endl;
break;
case 5:
listik.clear();
exit(0);
break;
}
}
}
解决方案
我认为你抄袭了一个作业,并在途中引入了一堆错别字。您需要填写作业中的空白并实施课程模板LinkedList
。
这是您删除错别字的任务,您需要实现以下成员函数LinkedList
:
#include <iostream>
#include <string>
#include <algorithm>
template<class T>
struct LinkedList {
void add(T element);
std::string toString() const;
void clear();
int getCurrentSize() const;
int getFrequencyOf(T element) const;
};
void AddList(LinkedList<char>& List, std::string input)
{
for (int i = input.size()-1; i >=0; i--)
{
List.add(input[i]);
} // end for loop
} // end ListTester
bool ifSubString(LinkedList<char>& List, std::string input)
{
std::string fromTheList = List.toString();
return (fromTheList.find(input) != std::string::npos);
}
void AppendList(LinkedList<char>& List, std::string input)
{
List.clear();
for (int i = input.size()-1; i >=0; i--)
{
List.add(input[i]);
}
}
int main()
{
std::string userString, newstring, subString;
int choice;
char charInput;
LinkedList<char> listik;
std::cout << "Welcome to the linkedlist program\n" <<std::endl;
std::cout << "Please Enter the string before we start: ";
getline(std::cin, userString);
AddList(listik, userString);
std::cout << "The string \"" << userString << "\" has been added to the linked list.\n" << std::endl;
std::cout << "Menu: " << std::endl;
std::cout << "1. Find the length of your input." << std::endl;
std::cout << "2. Add a new string to the current string." << std::endl;
std::cout << "3. Find the index of the character." << std::endl;
std::cout << "4. Find matches in the string." << std::endl;
std::cout << "5. Quit and sleep" << std::endl;
std::cin >> choice;
std::cin.ignore();
while(choice!=5)
{
switch(choice)
{
case 1:
std::cout << "The List " << listik.toString() << " contains " << listik.getCurrentSize() << " elements." << std::endl;
std::cout << std::endl;
break;
case 2:
std::cout << "Please type the string you'd like to append to the current list: ";
getline(std::cin, newstring);
AppendList(listik, (userString+newstring));
std::cout << "The list is \"" << listik.toString() << "\"." << std::endl; //AppendList function contains add method
std::cout << std::endl;
break;
case 3:
std::cout << "Please type the character you'd like to have the index of: ";
std::cin >> charInput;
std::cout << "The index of \"" << charInput << "\" is " << listik.getFrequencyOf(charInput) << std::endl;
std::cout << std::endl;
break;
case 4:
std::cout << "Please type the string you'd like to find in the current list: ";
getline(std::cin, subString);
std::cout << "The string \"" << subString << "\" is";
if (ifSubString(listik, subString)==false) std::cout << " not";
std::cout << "found in \"" << listik.toString() << "\"." << std::endl;
std::cout << std::endl;
break;
case 5:
listik.clear();
exit(0);
break;
}
}
}
阅读编译器错误消息,它们会告诉您出了什么问题。
推荐阅读
- r - 根据另一列的值强制不同的值相等
- firebase - 在 Firestore 中是否有重复的 FieldValue.serverTimestamp()?
- python - 如何使 plt.scatter() 中的点一次显示一个?
- python - Python 速成班 8-14
- android - database.sqlite.SQLiteException:靠近“=”:语法错误(代码1 SQLITE_ERROR):,编译时:DELETE FROM users = UserModel@dff4685;
- netlogo - 如何选择特定链接并使其死亡
- mysql - 如何通过查询从sql组中获取某些值,其中数据为空
- sql - 复杂的嵌套聚合以获取订单总计
- android - 日历不会返回带有 AM/PM 的日期
- azure - 什么是 Azure 资源锁?