C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数

C++ LinkedList problems. Conflict between datatypes? No matching Constructor?

本文关键字:构造函数 数据类型 问题 之间 存在 冲突 C++LinkedList      更新时间:2023-10-16

所以我正在编写一个程序,该程序接收用户的字符串输入,并对其执行一些基本操作,如getlength、getindex、追加到另一个字符串并测试它是否匹配。我的大部分代码都是基于教授的示例代码,我只是编写额外的方法来执行前面列出的操作。然而,我收到了来自LinkedChar.h和Node.h的错误,我反复犯了这个错误,但找不到解决方案。我会注意到大错误!我代码中注释部分的单词。

#include <iomanip>
#include "LinkedChar.h"

// Function prototype
bool isCreated(const LinkedChar<char>* charPtr);
LinkedChar<char> toLinkedChar(const string &items);
/************************************************************************
*                                 main                                  *
************************************************************************/
int main()
{
bool done = false; // A finish flag
int choice; // Menu choice
cout << " This program solves an ADT character string.  n";
LinkedChar<char>* charPtr = nullptr;
string firstString;
char aChar;
while (!done)
{
// Display the menu and get the user's choice
cout << "=========================================================n"
<< "Operationsnn";
cout << setw(5) << "1. New the first stringn";
cout << setw(5) << "2. Get the length from the first stringn";
cout << setw(5) << "3. Find index of character in the first stringn";
cout << setw(5) << "4. Append another string to the first stringn";
cout << setw(5) << "5. Test if another string is submatch of the first stringn";
cout << setw(5) << "6. Quitn"
<<"=========================================================nn";
cout << "Enter your Choice (1-6): ";
cin >> choice;
cin.ignore();
// Validate and process the menu choice
if (choice == 1)
{
cout << "Please enter a string: ";
getline(cin, firstString);
charPtr = new LinkedChar<char>(firstString);
cout << "The first string has been created. nn";
}
else if (choice == 6)
{
// Set the program is finished
done = true;
}
else if (choice > 1 && choice < 6)
{
if (isCreated(charPtr))
// Execute the correct set of actions
switch (choice)
{
case 2:
cout << "The length of the first string "" <<  charPtr->toString() << "" is " << charPtr->length() << ".nn";
break;
case 3:
cout << "Please enter a character to find: ";
cin >> aChar;
cout << "The index of "" << aChar << "" is " << charPtr->index(aChar) << "nn";
break;
case 4:
cout << "Please enter another string to append: ";
getline(cin, firstString);
charPtr->append(toLinkedChar(firstString));
cout << "The string changes to "" << charPtr->toString() << "".nn";
break;
case 5:
cout << "Please enter another string to test: ";
getline(cin, firstString);
cout << "The string "" << firstString << "" is ";
if (!charPtr->submatch(toLinkedChar(firstString)))
cout << "not ";
cout << "submatch of the first string "" << charPtr->toString() << "".nn";
break;
}
}
else
{
cout << "The valid choices are 1 through 6.nn";
}
}
return 0;
}
bool isCreated(const LinkedChar<char>* charPtr)
{
if (charPtr == nullptr)
{
std::cout << "Please choose 1 to create the first string before start!nn";
return false;
}
else
{
return true;
}
}
LinkedChar<char> toLinkedChar(const string &items)
{
return LinkedChar<char>(items);
}

节点.h

#ifndef NODE_H
#define NODE_H
template<class ItemType>
class Node
{
private:
ItemType item;
Node<ItemType>* next; // Pointer to next node
public:
Node() {
next = nullptr;
}
Node(const ItemType& anItem)
{
//  ----------- ERRORS ------------// 
item(anItem);    //Called object type 'char' is not a function or function 
//pointer
next(nullptr);   //Called object type 'Node<char> *' is not a function or 
//function pointer
/*This copy constructor is also exposed an error that I 
could not understand. I think that I declare the item as a 
template so later I could determine that datatype for item is 
either string or char*/  
// --------------------------------// 
}
Node(const ItemType& anItem, Node<ItemType>* nextNodePtr)
{
item(anItem);
next(nextNodePtr);
}
void setItem(const ItemType& anItem)
{
item = anItem;
}
void setNext(Node<ItemType>* nextNodePtr)
{
next = nextNodePtr;
}
ItemType getItem()  const {
return item;
}
Node<ItemType>* getNext() const {
return next;
} 
}; // end Node
#endif

LinkedChar.h

// Created by 
//
#ifndef LINKEDCHAR_H
#define LINKEDCHAR_H
#include <iostream>
#include <string>
#include "Node.h"
using namespace std;
template<class ItemType>
class LinkedChar
{
private:
Node<ItemType>* headPtr;
int itemCount;
public:
LinkedChar() {
headPtr = nullptr;
itemCount = 0;
}
LinkedChar(string items) {
itemCount = int(items.size());
headPtr = nullptr;
for (int i = itemCount - 1; i >= 0; i--)
{
if (headPtr == nullptr)
{
// Copy first node
headPtr = new Node<ItemType>();
headPtr->setItem(items[i]);
} else {
// Create a new node containing the next item
Node<ItemType>* newNodePtr = new Node<ItemType>(items[i]);
newNodePtr->setNext(headPtr);  // New node points to chain
headPtr = newNodePtr;          // New node is now first node
}
}
}
virtual ~LinkedChar() {
clear();
}
int length() {
return itemCount;
}
int index(const ItemType& anItem) const {
int index = 0;
Node<ItemType>* curPtr = headPtr;
while (curPtr != nullptr)
{
if (anItem == curPtr->getItem())
{
return index;
} // end if
index++;
curPtr = curPtr->getNext();
}
return -1;
}; // -1 if no match
void append(const LinkedChar& lc) {
// -------------- ERRORS ------------------ //
itemCount += lc.length(); // This is when I got the error guys, it says
// " 'this' argument to member function 'length' has
//type 'const LinkedChar<char>', but function is not
marked const "
// ------------------------------------------//
Node<ItemType>* newChainPtr = headPtr;
while (newChainPtr != nullptr && newChainPtr->getNext() != nullptr)
{
newChainPtr = newChainPtr->getNext();
}
Node<ItemType>* origChainPtr = lc.headPtr;
while (origChainPtr != nullptr)
{
// Get next item from original chain
ItemType nextItem = origChainPtr->getItem();
// Create a new node containing the next item
// ---------------- ERRORS -------------------- //
Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);
^^
||
||
// ERROR: The Error of no matching constructor for initialization of Node<Char>
//This happens to me alot previously, my solution is just put new Node(' ') 
//But in this case seem inappropriate
// ---------------------------------------------  //
// Link new node to end of new chain
newChainPtr->setNext(newNodePtr);
// Advance pointer to new last node
newChainPtr = newChainPtr->getNext();
// Advance original-chain pointer
origChainPtr = origChainPtr->getNext();
}
newChainPtr->setNext(nullptr);
}
bool submatch(const LinkedChar& lc) const {
bool found = false;
Node<ItemType>* curPtr = headPtr;
Node<ItemType>* origChainPtr = lc.headPtr;
ItemType anItem;
while (origChainPtr != nullptr && curPtr != nullptr)
{
anItem = origChainPtr->getItem();
while (curPtr != nullptr)
{
if (anItem == curPtr->getItem())
{
found = true;
curPtr = curPtr->getNext();
break;
}
else if (found)
{
found = false;
break;
}
curPtr = curPtr->getNext();
}
if (!found)
origChainPtr = lc.headPtr;
else
origChainPtr = origChainPtr->getNext();
}
return found && (origChainPtr == nullptr);
}
void clear() {
Node<ItemType>* curPtr = headPtr;
while (headPtr != nullptr)
{
headPtr = headPtr->getNext();
// Return node to the system
curPtr->setNext(nullptr);
delete curPtr;
curPtr = headPtr;
}
itemCount = 0;
}
string toString() const {
std::string aString;
Node<ItemType>* curPtr = headPtr;
while ((curPtr != nullptr))
{
aString += (curPtr->getItem());
curPtr = curPtr->getNext();
}
return aString;
}
};
//#include "LinkedChar.cpp"
#endif //LINKEDCHAR_H

您会被错误消息淹没。你应该从第一个开始,依次拿下每一个。明白它在告诉你什么。修复它,重新编译以确保。然后继续下一个。

对于初学者:

  1. 使length()常量

    int length() const {
    
  2. 更改

    next(nullptr);
    

    setNext(nullptr);
    
  3. 更改

    item(anItem);
    

    setItem(anItem);