Cool_Coder
Cool_Coder

Reputation: 5083

linking error in vc ++

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

Upvotes: 0

Views: 136

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361702

In header file use extern when declaring the variables, as:

//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

And in the source file, define and initialize them as:

//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

You get multiple definitions error, because you include CreatePoint.h more than one .cpp file which defines the same variables more than once. Using extern in header file, avoids this, because it doesn't define them, it simply declares them, while the actual definition goes in the .cpp file.

The keyword extern tells the compiler to look for the definition elsewhere. The extern statements in the header are only declaration of the variables.

Upvotes: 2

Elpezmuerto
Elpezmuerto

Reputation: 5581

You need to declare variables as extern in the header because they are outside a function block. If they were declared inside a function, they would be considered automatic

Upvotes: 0

John Dibling
John Dibling

Reputation: 101494

You are declaring & defining these variables directly in a header file. Every CPP file that includes this H file will get it's own definition of it, which is in violation of the One Definition Rule.

You need to change your design so that there is only one definition. You could make them extern in the H file:

CreatePoint.H

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

...and then define them in the CPP file:

CreatePoint.CPP

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

Global variables are generally bad, however. You should redesign your code so as to not use them.

Upvotes: 0

Related Questions