Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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
C++ LNK2005 X类已在Y.obj中定义_C++_Linker_Allocator - Fatal编程技术网

C++ LNK2005 X类已在Y.obj中定义

C++ LNK2005 X类已在Y.obj中定义,c++,linker,allocator,C++,Linker,Allocator,我有一个初学者项目,我需要为它编写一个自定义分配器和诊断工具。我制作了一个类class,其中我有两个方法用于自定义分配器void alloc()void dealloc()和诊断工具void evaluate()现在,我在CustomAllocator.h中声明了一个类型为Class的对象test,并使用这两种方法分配和释放内存,没有问题。但是,当我尝试调用CustomAllocatorTest.cpp中的evaluate()方法时,我得到了链接器错误类测试(?test@@3VClass@@A)

我有一个初学者项目,我需要为它编写一个自定义分配器和诊断工具。我制作了一个类
class
,其中我有两个方法用于自定义分配器
void alloc()
void dealloc()
和诊断工具
void evaluate()

现在,我在
CustomAllocator.h
中声明了一个类型为
Class
的对象
test
,并使用这两种方法分配和释放内存,没有问题。但是,当我尝试调用
CustomAllocatorTest.cpp中的
evaluate()
方法时,我得到了链接器错误
类测试(?test@@3VClass@@A),该错误已在CustomAllocatorTest.obj
LNK1169中定义,找到了一个或多个多重定义的符号

h班

#pragma once
class Class
{
public:
    void alloc() { std::cout << "alloc"; }
    void dealloc() { std::cout << "dealloc"; }
    void evaluate() { std::cout << "evaluate"; }
};

在您的文件
CustomAllocator.h
中,您在全局范围内声明
test

#ifndef _CUSTOM_ALLOCATOR_H_
#define _CUSTOM_ALLOCATOR_H_
#include "Class.h"

Class test; // <-- Declaration of test 

#endif 
\ifndef\u自定义分配器\u H_
#定义自定义分配器_
#包括“h类”

类别测试;//在StackOverflow,代码和错误必须是问题的一部分,并且必须是文本。一个问题的主要目的是帮助未来的读者从现在起几年内遇到同样的问题。我知道我会尝试编辑它并添加所有的类,但不知道它是否会帮助未来的读者,因为我在问题中有相当具体的东西。啊,我理解这样
类测试将在许多文件中多次声明。我试图按照链接中的建议,声明
类测试CustomAllocator.cpp
外部类测试中的code>
CustomAllocator.h
中,现在我得到了以下错误
未解析的外部符号“class-class-test”(?test@@3VClass@@A)
。由于包含了
CustomAllocator.h
,因此该对象只应包含一次并在其他文件中显示在标题中,您需要
类测试在某些翻译单元(源文件)中。@OvidiuFirescu正如@drescherjm所解释的,您必须编写
类测试(不带关键字
extern
)在
CPP
文件中。例如,您可以在
CustomAllocatorTest.cpp
中执行此操作。在
CustomAllocator.h
中编写
extern类测试希望这能帮助您了解我可能做错了什么?不,它应该有用。除非您正在创建dll。如果库是dll,则根本不处理导出/导入。
#include "stdafx.h"

using namespace std;

int main()
{
  test.evaluate();
  return 0;
}
#ifndef _CUSTOM_ALLOCATOR_H_
#define _CUSTOM_ALLOCATOR_H_
#include "Class.h"

Class test; // <-- Declaration of test 

#endif