链接器错误: /* 项实例 */ 已在项.obj 中定义

Linker error: /* item instance */ already defined in item.obj

本文关键字:obj 定义 实例 错误 链接      更新时间:2023-10-16

我正在用 c++ 编写一个程序,试图实现链表,但在引用我的一个构造函数和析构函数(两个属于同一个类)时收到链接器错误。该类称为项。这是 CS260 的任务,我不允许在 main 中编辑任何内容.cpp .

这些是我尝试构建时的错误消息:

1>item.obj : 错误 LNK2005: "公共: __thiscall 项目::项目(字符 *,双精度)" (??0item@@QAE@PADN@Z) 已在 main.obj 中定义

1>item.obj : 错误 LNK2005: "公共: 虚拟__thiscall 项目::~项目(无效)" (??1item@@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)
};

这是我的项目.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;
}

最后,这是我的教师提供的无法编辑的主.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;
}

任何建议将不胜感激。我对构造函数和析构函数没有信心,因此我不知道我哪里出错了,或者为什么我会收到这些链接器错误。谢谢!

这个:

#include "item.h"
#include "item.cpp" // <--

是错误的。您只包含文件。获取定义的方式是编译item.cpp然后编译main.cpp然后将它们链接在一起。