2011-10-06 78 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 再次感謝.. –