Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
链接器错误:/*项实例*/已在item.obj中定义 我正在编写一个C++程序,试图实现一个链表,在我的一个构造函数和析构函数(同一个类)中,我得到了一个链接错误。该类称为item。这是CS260的作业,我不允许在main.cpp中编辑任何内容_C++_Visual Studio 2013_Constructor_Linker Errors_Destructor - Fatal编程技术网

链接器错误:/*项实例*/已在item.obj中定义 我正在编写一个C++程序,试图实现一个链表,在我的一个构造函数和析构函数(同一个类)中,我得到了一个链接错误。该类称为item。这是CS260的作业,我不允许在main.cpp中编辑任何内容

链接器错误:/*项实例*/已在item.obj中定义 我正在编写一个C++程序,试图实现一个链表,在我的一个构造函数和析构函数(同一个类)中,我得到了一个链接错误。该类称为item。这是CS260的作业,我不允许在main.cpp中编辑任何内容,c++,visual-studio-2013,constructor,linker-errors,destructor,C++,Visual Studio 2013,Constructor,Linker Errors,Destructor,以下是我尝试构建时的错误消息: 1> item.obj:错误LNK2005:“public:u thiscall item::item(char*,double)”(??0item@@QAE@PADN@Z) 已在main.obj中定义 1> item.obj:错误LNK2005:“public:virtual_uthiscall item::~item(void)”(??1项@@UAE@XZ)已在main.obj中定义 1> C:\Users\Molly\Documents\Visual Stu

以下是我尝试构建时的错误消息:

1> item.obj:错误LNK2005:“public:u thiscall item::item(char*,double)”(??0item@@QAE@PADN@Z) 已在main.obj中定义

1> item.obj:错误LNK2005:“public:virtual_uthiscall item::~item(void)”(??1项@@UAE@XZ)已在main.obj中定义

1> C:\Users\Molly\Documents\Visual Studio 2013\Projects\Lab1\Debug\Lab1.exe:致命错误LNK1169:找到一个或多个多重定义符号

当我编辑掉构造函数和析构函数时,程序生成了。我不确定构造器/析构函数中的信息是在哪里或如何定义的

这是我的item.h文件:

#pragma once

#include <iostream>
#include <ostream>


class item
{
public:
    // Item needs a construtor that takes in a char* name and a double weight   

    item(char * name, double weight);

    virtual ~item(void); // destructor

    char * GetName() const {return name;}
    const double GetWeight() const { return weight; }

private:
    char *name;     // name of the item
    double weight;  // keeps track of weight of that/those item(s)

};
#pragma一次
#包括
#包括
类项目
{
公众:
//项目需要一个接受char*名称和双重权重的构造函数
项目(字符*名称,双倍重量);
virtual~item(void);//析构函数
char*GetName()常量{return name;}
const double GetWeight()const{return weight;}
私人:
char*name;//项的名称
双重重量;//跟踪该/那些物品的重量
};
这是我的item.cpp文件:

#define _CRT_SECURE_NO_WARNINGS

#include "item.h"
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

item::item(char * name, double weight) 
{
    name = NULL;
    weight = 0;

    if (this->name)
        delete[] this->name;
    this->name = new char[strlen(name) + 1];
    strcpy(this->name, name);

}

item::~item()
{
    if (name)
        delete[] name;
}
\define\u CRT\u SECURE\u NO\u警告
#包括“项目h”
#包括
#包括
#包括
使用名称空间std;
项目::项目(字符*名称,双倍重量)
{
name=NULL;
权重=0;
如果(此->名称)
删除[]此->名称;
此->名称=新字符[strlen(名称)+1];
strcpy(此->名称,名称);
}
项目::~item()
{
如果(姓名)
删除[]名称;
}
最后,这是讲师提供的无法编辑的main.cpp文件

#include <iostream>
#include "item.h"
#include "item.cpp"
#include "inventory.h"

#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

using namespace std;

void AddItem(inventory& inv, char* name, double weight)
{
    cout << "Adding " << name << " with a weight of " << weight << "." << endl;
    inv.AddItem(item(name, weight));
}

void RemoveItem(inventory& inv, char* name)
{
    cout << "Removing " << name << "." << endl;
    inv.RemoveItem(name);
}

void doTestBasic()
{
    inventory inv;

    // Make sure printing an empty inventory works
    //inv.PrintInventory();

    // Make sure adding the first one works
    AddItem(inv, "helmet", 5);
    inv.PrintInventory();

    // Note to stackoverflow: there was some node adding/removing stuff
    // in here (and in doTestAdvanced/ doTestBadData that I removed because it's not pertinent 

}

void doTestAdvanced()
{
    inventory inv;

// Add items with different case
AddItem(inv, "helmet", 1.0);
AddItem(inv, "Helmet", 1.0);
AddItem(inv, "HELMET", 1.0);
inv.PrintInventory();

// Remove items with case insensitivity
RemoveItem(inv, "HELMET");
inv.PrintInventory();

}

void doTestBadData()
{
    inventory inv(50);

    // Adding too much
    AddItem(inv, "bag of gold coins", 50);  // should work
    AddItem(inv, "feather", 0.1);               // should fail
    inv.PrintInventory();
    RemoveItem(inv, "bag of gold coins");   // should work
    inv.PrintInventory();

    // Using long strings
    AddItem(inv, "this is such a long item and nothing should have a name             thing long but we have no guarantee that some crazy person wouldn't make such an item and then we need to make sure our program doesn't break.  Hint: don't use char[] to store data.  Only use char*.", 1.0);
    inv.PrintInventory();
}

int main() {

    doTestBasic();
    doTestAdvanced();
    doTestBadData();

#ifdef _WIN32
    if (_CrtDumpMemoryLeaks()) {
        cout << "Memory leaks!" << endl;
    }
#endif

    return 0;
}
#包括
#包括“项目h”
#包括“item.cpp”
#包括“inventory.h”
#ifdef_WIN32
#定义\u CRTDBG\u映射\u ALLOC
#包括
#包括
#恩迪夫
使用名称空间std;
无效附加项(库存和库存,字符*名称,双倍重量)
{
不能这是:

#包括“item.h”

#include“item.cpp”/
#include“item.cpp”
从不包括cpp文件。原因是
#include
将“粘贴”在那个位置的文件内容。哇。非常感谢。那是我的导师提供给我们的文件。我甚至没有想检查它,因为我认为不会有错误。它现在就建立起来了。
#include "item.h"
#include "item.cpp" // <--