链接器错误: /* 项实例 */ 已在项.obj 中定义
Linker error: /* item instance */ already defined in item.obj
我正在用 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
然后将它们链接在一起。
相关文章:
- float* 已在 Gameobject.obj 中定义
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- C++ LNK 2005 错误"已在 .obj 中定义
- "... already defined in *.obj"但我没有在同一范围内两次定义任何变量
- 已经在 .obj 中定义,即使有 #ifndef #define #endif
- 需要的具体示例.错误:已经定义的.OBJ 一个或多个多个定义的符号
- Main.Obj中已经定义的主要
- 变量已在 .obj 中定义;这是怎么回事?
- 已在 ConsoleApplication1.obj 中定义
- DllMain 已在 dllmain.obj 中定义
- 已在main.obj中定义了双精度
- 错误LNK2005结构已在.obj文件中定义
- 已经在main.obj中定义了
- 变量已在 .obj 中定义,但没有循环包含
- 成员函数已在.obj(visual studio)中定义
- 链接器错误: /* 项实例 */ 已在项.obj 中定义
- 错误 11 错误 LNK2005:已在 MyForm.obj 中定义"class cv::Mat imageOutput" (?imageOutput@@3VMat@cv@@A)
- 错误 LNK2005:"class Player m_player"已在 Game.obj 中定义
- 已经定义了 Obj?链接错误
- LNK2005错误,已在main.obj中定义