我已经为此工作了一段时间,目前正处于困境。程序的起始要求是接受一个字符串,为类创建一个对象,并将其转换为一个链接的字符列表。我在处理整个类时遇到了问题,因为当我试图在while循环之前在Main函数中预定义一个新对象时,我得到了错误charchain.cpp:(.text+0x20(:对linkedChar::linkedChar((的未定义引用我已经测试了该类,并成功地转换为字符的链表。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct node {
char data;
node* next;
};
class linkedChar
{
private:
node* head;
public:
string str;
linkedChar();
linkedChar(const std::string s){
head = NULL;
strToLinkedChar(s);
}
node* add(char data)
{
node* newnode = new node;
newnode->data = data;
newnode->next = NULL;
return newnode;
}
node* strToLinkedChar(string str){
head = add(str[0]);
node* curr = head;
for (int i = 1; i < str.size(); i++) {
curr->next = add(str[i]);
curr = curr->next;
}
}
void print()
{
node* curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
}
void listLen(){
int counter = 0;
node* curr = head;
while (curr != NULL) {
curr = curr->next;
counter++;
}
std::cout << "There are " << counter << " characters in the current linked list of characters." << std::endl;
}
};
int main()
{
int userInput;
linkedChar linkedObject;
while (userInput != 6){
userInput = -1;
std::cout << "Option Menu: n1. Enter new string and store as linked list of characters in an ADT LinkedChar classn2. Get current length (number of characters stored) from the LinkedChar n3. Find index of character in this LinkedChar n4. Append another LinkedChar to this LinkedChar (no shallow copy)n5. Test if another LinkedChar is submatch of this LinkedCharn6. Quitn" << std::endl << "Input:";
std::cin >> userInput;
std::cout << std::endl;
if(userInput == 1){
string str;
std::cout << "Enter the string you want to turn into a linked list of characters: ";
std::cin >> str;
std::cout << std::endl;
linkedObject = linkedChar(str);
} else if(userInput == 2){
linkedObject.listLen();
}
}
}
我感谢您的意见!
您缺少linkedChar
无参数ctor(已声明但未定义(,并且在strToLinkedChar
中缺少return语句。