链接器错误:/ * item instance * /已在item.obj中定义

时间:2015-01-25 04:46:05

标签: c++ visual-studio-2013 constructor linker-errors destructor

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

当我尝试构建它时,这些是错误消息:

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

1> item.obj:错误LNK2005:已在main.obj中定义的“public:virtual __thiscall item ::〜item(void)”(?? 1item @@ UAE @ XZ)

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)

};

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

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

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

1 个答案:

答案 0 :(得分:2)

此:

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

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