在vc ++中链接错误

时间:2011-10-07 17:10:32

标签: c++ visual-c++

我的项目中有Point3D类。 创建一个Point3D的对象我添加了一个cpp文件&头文件如下: CreatePoint.h

#include "stdafx.h"
#pragma once
#include "Point3D.h"

//*******************************************************************
int counter = 0;
int size = 50;
Point3D **point;

//*******************************************************************
void create_array(int);//this will be called in main & pass 50//this is the method to             create an array of pointers to Point3D

//*******************************************************************
void resize();//this increases the size of array if size - 5 elements are filled &     increases size by 25

//*******************************************************************
Point3D *get_point(int);//this returns the pointer according to the index

//*******************************************************************
int get_index(Point3D *);//this returns the index of a point

//*******************************************************************
void move_point(int, int);//this interchanges the memory locations of 2 points

//*******************************************************************
void del_point(Point3D *);//this makes NULL value to the passed point

//*******************************************************************
void destruct_point();//this is called when the program ends by me

&安培; cpp文件是:

#include "stdafx.h"
#include "CreatePoint.h"

//*******************************************************************
void create_array(int s)
{
point = new Point3D *[s];
for (int i = 0; i<s; i++)
{
    point[i] = NULL;
}
}

//*******************************************************************
void resize()
{
Point3D **copy = new Point3D *[size];
for(int i = 0; i<size; i++)
{
    copy[i] = point[i];
}

delete [] point;

size += 25;
create_array(size);

for(int i = 0; i<(size - 25); i++)
{
    point[i] = copy[i];
}

delete [] copy;
}

//*******************************************************************
Point3D *get_point(int i)
{
if((size - counter) == 5)
    resize();
return point[i];
}

//*******************************************************************
int get_index(Point3D *p)
{
for(int i = 0; i<size; i++)
{
    if(point[i] == p)
        return i;
}
return -1;
}

//*******************************************************************
void move_point(int a, int b)
{
Point3D *apt = get_point(a);
Point3D *bpt = get_point(b);

Point3D *t = new Point3D;
t = apt;
apt = bpt;
bpt = t;

delete t;
}

//*******************************************************************
void del_point(Point3D *p)
{
int d = get_index(p);
move_point(d, counter - 1);
point[counter - 1] = NULL;
}

//*******************************************************************
void destruct_point()
{
delete [] point;
point = NULL;
}

我收到一些链接错误:

stdafx.obj : error LNK2005: "class Point3D * * point" (?point@@3PAPAVPoint3D@@A)     already defined in CreatePoint.obj
1>stdafx.obj : error LNK2005: "int counter" (?counter@@3HA) already defined in     CreatePoint.obj
1>stdafx.obj : error LNK2005: "int size" (?size@@3HA) already defined in CreatePoint.obj
1>C:\Documents and Settings\SUMIT & AMIT\my documents\visual studio     2010\Projects\Maths\Debug\Maths.exe : fatal error LNK1169: one or more multiply defined     symbols found

任何人都可以帮帮我!!! 此外,任何有关代码的建议将不胜感激:) 感谢很多阅读我的帖子

3 个答案:

答案 0 :(得分:2)

声明变量时,在头文件中使用extern,如下:

//CreatePoint.h
extern int counter;       //it is only a declaration
extern int size;          //it is only a declaration
extern Point3D **point;   //it is only a declaration

在源文件中, define 并将它们初始化为:

//CreatePoint.cpp

#include "CreatePoint.h"

int counter = 0;   //it is the definition
int size = 50;     //it is the definition
Point3D **point;   //it is the definition

您会收到多个定义错误,因为您包含CreatePoint.h个多个.cpp文件,该文件多次定义相同的变量。在头文件中使用extern可以避免这种情况,因为它不会定义它们,它只是声明它们,而实际的定义进入.cpp文件。

关键字extern告诉编译器在其他地方查找定义。标头中的extern语句只是变量的声明

答案 1 :(得分:0)

你宣布&amp;直接在头文件中定义这些变量。包含此H文件的每个CPP文件都将获得它自己的定义,这违反了One Definition Rule

您需要更改设计,以便只有一个定义。您可以在H文件中创建extern

CreatePoint.H

extern int counter;
extern int size;
extern Point3D **point;

...然后在CPP文件中定义它们:

CreatePoint.CPP

int counter = 0;
int size = 50;
Point3D** point = 0;

然而,全局变量通常很糟糕。您应该重新设计代码,以免使用它们。

答案 2 :(得分:0)

您需要在标头中将变量声明为extern,因为它们位于功能块之外。如果它们在函数内声明,则它们将被视为automatic