无法从' *'转换参数1 to" std :: string'

时间:2014-12-14 20:54:57

标签: c++ linked-list

我有一个错误,就是用8个错误填满我的日志。错误读取错误1错误C2664:' Node :: Node(const Node&)' :无法转换来自' CarPart *'的参数1 to' std :: string'我不确定如何诊断或解决这个问题。谢谢你的帮助。这个项目有很多代码,所以我只是发布我的内容并让大家通读它,让我知道我是否可以更容易地总结一下。调用新CarPart时,驱动程序中会出现错误。另外我知道printList函数在驱动程序中是不完整的,这是我的下一个问题,我不知道如何使用我为标题和实现填写的信息打印列表。驱动程序由研究文本提供,我只需要它来编译和执行。

标题

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
#include <array>

class CarPart
{

private:
    // Variable Name: partNumber
    // Purpose: Variable that contains a unique identifier
    // Parameters: None
    // Returns: None
    // Pre-conditions: Must be of type string
    // Post-conditions: None
    std::string partNumber;

    // Variable Name: partDescription
    // Purpose: Variable that contains the parts description
    // Parameters: None
    // Returns: None
    // Pre-conditions: Must be of type string
    // Post-conditions: None
    std::string partDescription;

    // Variable Name: price
    // Purpose: Variable that contains price
    // Parameters: None
    // Returns: None
    // Pre-conditions: Must be of type double
    // Post-conditions: None
    double price;

public:
    // Default Constructor Name: CarPart
    // Purpose: Default constructor to initialize
    // Parameters: None
    // Returns: None
     CarPart();

     // Parameterized Constructor Name: CarPart
     // Purpose: Parameterized constructor to initialize data types
     // Parameters: A string value, a string value, and a double value
     // Returns: None
     CarPart(std::string partNum, std::string partDesc, double partPrice);

     // Function Name: getPartNumber
     // Purpose: To get the number for the part
     // Parameters: None
     // Returns: None
     // Pre-conditions: Part number cannot be NULL
     // Post-conditions: None
     std::string getPartNumber() const;

     // Setter Name: setPartNumber
     // Purpose: Initialize data
     // Parameters: A string object
     // Returns: None
     void setPartNumber(std::string);

     // Function Name: getDescription
     // Purpose: To get the description for the part
     // Parameters: None
     // Returns: None
     // Pre-conditions: Description cannot be empty
     // Post-conditions: None
     std::string getDescription() const;

     // Setter Name: setDescription
     // Purpose: Initialize data
     // Parameters: A string object
     // Returns: None
     void setDescription(std::string);

     // Function Name: getPrice
     // Purpose: To get the price for the part
     // Parameters: None
     // Returns: None
     // Pre-conditions: Node cannot be NULL
     // Post-conditions: None
     double getPrice() const;

     // Setter Name: setPrice
     // Purpose: Initialize data
     // Parameters: A double object
     // Returns: None
     void setPrice(double);
};

 class Node
 {
 private:
     // Variable Name: next
     // Purpose: Points to the next node
     // Parameters: None
     // Returns: None
     Node* next;

     // Varible Name: CarPart
     CarPart* carPart;

 public:
     // Default Constructor Name: Node
     // Purpose: Default constructor to initialize
     // Parameters: None
     // Returns: None
     Node();

     // Parameterized Constructor Name: Node
     // Purpose: Parameterized constructor to initialize
     // Parameters: A string value
     // Returns: None
     Node(std::string);

     //Destructor
     //Purpose:Deletes any dynamically allocated storage
     //Parameters: None
     //Returns: None
     ~Node();

     // Getter Name: getNext
     // Purpose: Initialize data
     // Parameters: None
     // Returns: None
     Node* getNext();

     // Setter Name: setNext
     // Purpose: Initialize data
     // Parameters: A pointer to next node
     // Returns: None
     void setNext(Node*);
 };

 class List
 {
 private:
     // Variable Name: numNodes
     // Purpose: Variable that contains the number of nodes
     // Parameters: None
     // Returns: None
     // Pre-conditions: Must be of type int
     // Post-conditions: None
     int numNodes;

     // Variable Name: next
     // Purpose: Points to the first node
     // Parameters: None
     // Returns: None
     Node* firstNode;

     // Variable Name: next
     // Purpose: Points to the last node
     // Parameters: None
     // Returns: None
     Node* lastNode;

 public:
     // Default Constructor Name: List
     // Purpose: Default constructor to initialize
     // Parameters: None
     // Returns: None
     List();

     //Destructor
     //Purpose:Deletes any dynamically allocated storage
     //Parameters: None
     //Returns: None
     ~List();

     // Function Name: push_back
     // Purpose: Take the node and place it at the end of the list
     // Parameters: Node pointer
     // Returns: None
     // Pre-conditions: Node cannot be NULL
     // Post-conditions: None
     void push_back(Node*);

     // Function Name: push_front
     // Purpose: Take the node and place it at the front of the list
     // Parameters: Node pointer
     // Returns: None
     // Pre-conditions: Node cannot be NULL
     // Post-conditions: None
     void push_front(Node*);

     // Function Name: pop_back
     // Purpose: Removes last node from list
     // Parameters: None
     // Returns: A pointer to this node
     // Pre-conditions: None
     // Post-conditions: None
     Node* pop_back();

     // Function Name: pop_front
     // Purpose: Removes first node from list
     // Parameters: None
     // Returns: A pointer to this node
     // Pre-conditions: None
     // Post-conditions: None
     Node* pop_front();

     // Function Name: getFirstNode
     // Purpose: To get the first node
     // Parameters: None
     // Returns: A pointer to the first node
     // Pre-conditions: None
     // Post-conditions: None
     Node* getFirstNode();

     // Function Name: getLastNode
     // Purpose: To get the last node
     // Parameters: None
     // Returns: A pointer to the last node
     // Pre-conditions: None
     // Post-conditions: None
     Node* getLastNode();
 };

实施

#include "CarPart Header.h"

//Constructor
//Purpose: Initialize data
//Parameters: None
//Returns: None
CarPart::CarPart()
{
    price = 0.00;
    partNumber = "";
    partDescription = "";
}

//Parameterized Constructor
//Purpose: Initialize data
//Parameters: None
//Returns: None
CarPart::CarPart(std::string partNum, std::string partDesc, double partPrice)
{
    price = partPrice;
    partNumber = partNum;
    partDescription = partDesc;
}

// Setter Name: setDescription
// Purpose: Initialize data
// Parameters: A string object
// Returns: None
void CarPart::setDescription(std::string)
{
    partDescription;
}

// Setter Name: setPartNumber
// Purpose: Initialize data
// Parameters: A string object
// Returns: None
void CarPart::setPartNumber(std::string)
{
    partNumber;
}

// Setter Name: setPrice
// Purpose: Initialize data
// Parameters: A double object
// Returns: None
void CarPart::setPrice(double)
{
    price;
}

// Getter Name: getPartDescription
// Purpose: Return Data
// Parameters: None
// Returns: partDescription
std::string CarPart::getDescription() const
{
    return partDescription;
}

// Getter Name: getPartNumber
// Purpose: Return data
// Parameters: None
// Returns: partNumber
std::string CarPart::getPartNumber() const
{
    return partNumber;
}

// Getter Name: getPriceNumber
// Purpose: Return data
// Parameters: None 
// Returns: price
double CarPart::getPrice() const
{
    return price;
}

//Node Constructor
//Purpose: Initialize data
//Parameters: None
//Returns: None
Node::Node()
{
    next = NULL;
}

//Parameterized Constructor
Node::Node(std::string)
{

}

//Node Destructor
//Purpose:
//Parameters:
//Returns:
Node::~Node()
{
    if (carPart != NULL)
    {
        delete carPart;
    }
}

//List Constructor
//Purpose: Initialize data
//Parameters: None
//Returns: None
List::List()
{
    numNodes = NULL;
    firstNode = NULL;
    lastNode = NULL;
}

//Destructor
//Purpose:Deletes any dynamically allocated storage
//Parameters: None
//Returns: None
List::~List()
{
    while (firstNode != nullptr&&lastNode != nullptr)
    {
        //1. Create a temporal pointer pointing to your firstnode.  
        Node* tempPtr = firstNode;
        //2. Alter your firstnode to point to the next node;    
        firstNode = firstNode-> getNext();
        //3. Delete the temporal pointer ans set it to nullptr.     
        delete tempPtr;
        tempPtr = nullptr;
        //4. Repeat the process while the list is not empty.    
    }
    firstNode = nullptr;
}

//push_back
//Purpose: Adds the node pointed to by the pointer to the end of the list
//Parameters: None
//Returns: None
void List::push_back(Node* newNode)
{
    if (lastNode != NULL)
    {
        newNode->setNext(lastNode);
        lastNode = newNode;
        numNodes++;
    }
}

//push_front 
//Purpose: Adds the node pointed to by the pointer to the front of the list
//Parameters: None
//Returns: None
void List::push_front(Node* newNode)
{
    if (firstNode != NULL)
    {
        newNode->setNext(firstNode);
        firstNode = newNode;
        numNodes++;
    }
}

//pop_back
//Purpose: Removes last node from list
//Parameters: None
//Returns: A pointer to this node
Node* List::pop_back()
{
    if (lastNode = NULL)
    {
        return NULL;
    }
    if (lastNode != NULL)
    {
        Node* tempPtr = lastNode;
        lastNode = lastNode->getNext();
        numNodes--;
        return tempPtr;
    }
}

//pop_front
//Purpose: Removes first node from the list
//Parameters: None
//Returns: A pointer to this node
Node* List::pop_front()
{
    if (firstNode = NULL)
    {
        return NULL;
    }
    if (firstNode != NULL)
    {
        Node* tempPtr = firstNode;
        firstNode = firstNode->getNext();
        numNodes--;
        return tempPtr;
    }
}

//getFirstNode
//Purpose: Get the first node
//Parameters: None
//Returns: Returns pointer to the first node in the list
Node* List::getFirstNode()
{
    return firstNode;
}

//getLastNode
//Purpose:Deletes any dynamically allocated storage
//Parameters: None
//Returns: Returns pointer to the las node in the list
Node* List::getLastNode()
{
    return lastNode;
}

驱动程序

// add your file prologue information here
#include <iostream>
#include "CarPart Header.h"
using namespace std;

// the printlist function
// Purpose: Prints out the contents for each Node in the List
// Parameter: A list object, by const reference
// Returns: none
void printList(const List&);

// the printFirstNode function
// Purpose: Prints out the contents for the 1st Node in the List
// Parameter: A list object, passed by value to test the copy constructor
// Returns: none
void printFirstNode(List);

int main()
{

    // set up cout for displaying prices
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    // create a List object
    List partsList;

    cout << "\nPart I: multiple node test: push_front and pop_front\n";
    cout << "\n----------------------------------\n";
    // build a List using push_front
    partsList.push_front(new Node(new CarPart("FL2016", "Oil Filter", 18.95)));
    partsList.push_front(new Node(new CarPart("RS12YC", "Spark Plug", 4.15)));
    partsList.push_front(new Node(new CarPart("D5941", "Digital Tire Guage", 12.15)));
    partsList.push_front(new Node(new CarPart("G19216", "Car Wash Solution", 8.15)));

    cout << "\nThe original nodes in the List:\n";
    printList(partsList);
    cout << "\n----------------------------------\n";

    // test push_front function
    cout << "\nAdding to the front of the List:\n";
    cout << "\n----------------------------------\n";
    partsList.push_front(new Node(new CarPart("X-5077a", "Wiper Blades", 15.45)));
    partsList.push_front(new Node(new CarPart("T-280RA", "Turtle Wax Chrome Polish", 3.15)));

    printList(partsList);
    cout << "\n----------------------------------\n";

    // test pop-front
    cout << "\nRemoving the first node from the list.\n";
    cout << "\n----------------------------------\n";
    Node* item = partsList.pop_front();
    printList(partsList);
    if (item != NULL)
        delete item;

    cout << "\n----------------------------------\n";
    cout << "\nPart Two: Push_back and pop_back";

    // test push_back
    partsList.push_back(new Node(new CarPart("C120-X", "Assorted Fuses", 7.25)));
    partsList.push_back(new Node(new CarPart("CTK-120706", "Mechanic Tool set", 126.00)));

    cout << "\nAdding two nodes at the end\n";
    cout << "\n----------------------------------\n";
    printList(partsList);

    // test pop-back
    cout << "\n----------------------------------\n";
    cout << "\nRemove last node from the list\n";
    cout << "\n----------------------------------\n";
    item = partsList.pop_back();
    printList(partsList);
    if (item != NULL)
        delete item;



    cout << "\n-------------------------------------------\n";
    cout << "\nEnd of Test";
    cout << "\n-------------------------------------------\n";
    system("PAUSE");
    return 0;
}

// you have to fill in the code for this function
void printList(const List& theList)
{
    cout << theList.;
}

// you have to fill in the code for this function
void printFirstNode(List theList)
{
    cout << theList.getFirstNode;
}

1 个答案:

答案 0 :(得分:2)

在这里无处....

 class Node
 {
 private:
     // Variable Name: next
     // Purpose: Points to the next node
     // Parameters: None
     // Returns: None
     Node* next;

     // Varible Name: CarPart
     CarPart* carPart;

 public:
     // Default Constructor Name: Node
     // Purpose: Default constructor to initialize
     // Parameters: None
     // Returns: None
     Node();

     // Parameterized Constructor Name: Node
     // Purpose: Parameterized constructor to initialize
     // Parameters: A string value
     // Returns: None
     Node(std::string);

     //Destructor
     //Purpose:Deletes any dynamically allocated storage
     //Parameters: None
     //Returns: None
     ~Node();

     // Getter Name: getNext
     // Purpose: Initialize data
     // Parameters: None
     // Returns: None
     Node* getNext();

     // Setter Name: setNext
     // Purpose: Initialize data
     // Parameters: A pointer to next node
     // Returns: None
     void setNext(Node*);
 };

你有......所需的构造函数吗?

new Node(new CarPart...

唯一接受参数的构造函数是......

Node(std::string);

因此您会看到错误。