2011-10-06 58 views
0

我的项目包含几个类(其中一个是Point3D)& a cpp(CreatePoint.cpp)&一个头文件(CreatePoint.h)。运行时错误在vC++

我的stdafx.h文件

// stdafx.h : include file for standard system include files, 
// or project specific include files that are used frequently, but 
// are changed infrequently 
// 

#pragma once 

#include "targetver.h" 

#include <stdio.h> 
#include <tchar.h> 


// TODO: reference additional headers your program requires here 

#include "CreatePoint.h" 
#include "Point3D.h" 
#include "Vector3D.h" 
#include "Sys.h" 

我CreatePoint.h文件

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


//******************************************************************* 
void initialise(); 

//******************************************************************* 
Point3D *get_point(int); 

//******************************************************************* 
int get_index(Point3D *); 

//******************************************************************* 
Point3D *create_point(); 

//******************************************************************* 
void del_point(Point3D *); 

//******************************************************************* 
void destruct_point(); 

我CreatePoint.cpp文件

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

int counter; 
int size = 50; 
Point3D *point[]; 
//******************************************************************* 
void initialise()//run this func each time point[] is created 
{ 
    counter = 0; 
    for(int i = 0; i<size; i++) 
    { 
    point[i] = '\0'; 
} 
} 

//******************************************************************* 
Point3D *get_point(int index) 
{ 
    return point[index]; 
} 

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

//******************************************************************* 
Point3D *create_point() 
{ 
point[counter] = new Point3D; 
counter++; 
return point[counter]; 
} 

//******************************************************************* 
void del_point(Point3D *p) 
{ 
int d = get_index(p); 
delete point[d]; 
} 

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

我得到一个运行时错误:

CreatePoint.obj : error LNK2001: unresolved external symbol "class Point3D * * point" ([email protected]@[email protected]@A) 
1>C:\Documents and Settings\my documents\visual studio 2010\Projects\Maths\Debug\Maths.exe : fatal error LNK1120: 1 unresolved externals 

我已经搜索了网络&这种失败的原因主要是在每个文件的第一行不包括stdafx.h ......但我已经包含了它。 我也流汗一定的警示最后功能destruct_point() - >

\maths\maths\createpoint.cpp(51): warning C4154: deletion of an array expression; conversion to pointer supplied 
+2

'LNK2001'是一个链接器错误,不是运行时错误。 –

回答

3

LNK2001是链接错误,而不是运行时错误。

Point3D *point[];似乎是一个声明,但不是一个实例。也就是说,这一行告诉编译器这个变量将在稍后存在。因为数组必须具有要实例化的大小。 (我甚至不知道[]没有大小是允许在该范围内)

将其更改为Point3D *point[size];它将实际上创建该数组。此外,size必须是const int

[编辑]
destruct_point()尝试删除整个点数组。由于数组是静态分配的,所以这是不允许的。既然你已经有了一个删除单个点的函数,我无法想象为什么这个函数存在。由于阵列没有用new[]声明,所以不应该使用delete[]

+0

......感谢洛特的工作......但是我最后提到的警告呢? –

+0

你没有为'new []'分配数组,所以你不需要'delete']它。你不需要这个功能。 –

+0

我在msdn上发现它被删除[] &point; http://msdn.microsoft.com/en-us/library/f7h7y2d3.aspx 再次感谢.. –