i have Point3D class in my project.
to create an object of Point3D i have added a cpp file & a header file as follows:
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
& the cpp file is:
#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;
}
i am getting some linking errors:
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
can anybody please help me!!!
also, any suggestions on the code will be appreciated 🙂
THANKS A LOT FOR READING MY POST
In header file use
externwhen declaring the variables, as:And in the source file, define and initialize them as:
You get multiple definitions error, because you include
CreatePoint.hmore than one.cppfile which defines the same variables more than once. Usingexternin header file, avoids this, because it doesn’t define them, it simply declares them, while the actual definition goes in the.cppfile.The keyword
externtells the compiler to look for the definition elsewhere. Theexternstatements in the header are only declaration of the variables.