无法添加另一个对象类的新节点c ++错误未定义引用

时间:2016-12-23 22:11:04

标签: c++ templates linked-list

的main.cpp

int main()
{
    cout << "Hello world!" << endl;
    List<Item> test;
Item x("coca","1",3.5,"Beverage",1);
test.AddNode(x);
return 0;
}

Item.h 项目标题

#ifndef ITEM_H
#define ITEM_H
#include<string>
using namespace std;

class Item
{
    public:

        Item();
        Item(string Name,string ID,double Price ,string Type,bool Edible);

        string GetName() { return Name; }
        void SetName(string val) { Name = val; }
        string GetID() { return ID; }
        void SetID(string val) { ID = val; }

        string GetType() { return Type; }
        void SetType(string val) { Type = val; }
        double GetPrice() { return Price; }
        void SetPrice(double val) { Price = val; }

    protected:

    private:
        string Name;
        string ID;
        string Type;
        double Price;
        bool Edible;
};

Item.cpp

#include "Item.h"
#include <iostream>
#include<string>
using namespace std;
Item::Item(string Name,string ID,double Price,string Type,bool Edible)
{
this->Name=Name;
this->ID=ID;
this->Price=Price;
this->Type=Type;
this->Edible=Edible;
}

list.h 这是我的链接列表,其中编译器作为错误指示进入struct node并将这些错误提供给Item :: Item()错误未定义引用:id返回1存在状态

   #ifndef LIST_H
        #define LIST_H
        #include<cstdlib>
        #include<string>
        #include<iostream>
        #include "Item.h"
        using namespace std;
         template <typename TYPE>
            struct node{

> The compiler goes to this part after i run 

                    node<TYPE> *next;
                    TYPE data;
                };
        template <typename TYPE>
        class List
        {

            public:

                List();


        void AddNode(TYPE addData);
        void DeleteNode(TYPE delData);
        void PrintList();
        int CountItem();
            private:
                node<TYPE>* head;
                node<TYPE>* curr;
                node<TYPE>* temp;


        };
        #include<cstdlib>
        #include<string>
        #include "List.h"
        #include<iostream>
        using namespace std;
        template<typename TYPE>
        List<TYPE>::List()
        {
           head = NULL;
           curr = NULL;
           temp = NULL;
        }
        template<typename TYPE>
        void List<TYPE>::AddNode(TYPE addData){
            node<TYPE>* n=new node<TYPE>;
            n->next=NULL;
            n->data=addData;
            if(head!=NULL){
                curr=head;
            while(curr->next!=NULL){
                curr=curr->next;
                                   }
            curr->next=n;
                         }
            else{
                head=n;
            }

    }

#endif // LIST_H

0 个答案:

没有答案