already defined error


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: already defined error

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    already defined error

    header file
    Code:
    #ifndef CONVEXHULL_GRAHAM_SCAN_H
    #define CONVEXHULL_GRAHAM_SCAN_H
    using namespace std;
    struct point
    {
       long double x, y;
    };
    point stack[100]; // the actual point stack array
    point Pivot, S[100];
    int TOS_ptr;// = 0;           // Top Of Stack pointer
    int Push(point p);
    int Pop(point *p);
    long double CrossProduct(point p0, point p1, point p2);
    int compare_points(const void *a, const void *b);
    int LocatePivot(point S[], int N);
    void Graham_Scan(point S[], int N);
    #endif
    This problem is driving me crazy....
    the header file as shown when I include it in my project, it compiles fine. As soon as I include the cpp in to my project (even if the cpp was empty and had just the line #include "ConvexHull_Graham_Scan.h"), it than complains:

    error C2871: 'std' : a namespace with this name does not exist


    cpp file
    Code:
    #include "ConvexHull_Graham_Scan.h"
    int Push(point p)
    {
    	//*********************************************************
    	// pushes point p onto the stack
    	// if successful Push() returns 1
    	// on stack overflow Push() returns 0
    	if (TOS_ptr == 100) return(0);
    	stack[TOS_ptr++] = p;
    	return(1);
    }
    int Pop(point *p)
    {
    	//*********************************************************
    	// pops a point off of the stack
    	// if successful Pop() returns 1
    	// on stack underflow Pop() returns 0
    	if (TOS_ptr == 0) return(0);
    	*p = stack[--TOS_ptr];
    	return(1);
    }
    long double CrossProduct(point p0, point p1, point p2)
    {
    	//*************************************************************************
    	// Computes the cross product (p1-p0)x(p2-p0). If p2 is to the left of the 
    	// line from p0 to p1 the result will be positive.
    	long double xprod;
    	xprod = (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    	return(xprod);
    }
    int compare_points(const void *a, const void *b)
    {
    	//*********************************************************
    	// Support function for qsort()
    	// a < b if a makes a smaller angle with Pivot than does b
    	long double xprod;
    	point *point_a, *point_b;
    	point_a = (point *)a;
    	point_b = (point *)b;
    	xprod = CrossProduct(Pivot, *point_a, *point_b);
    	if (xprod < 0)
    	{
    		return(+1);    // a > b
    	}
    	else if (xprod > 0)
    	{
    		return(-1);    // a < b
    	}
    	return(0);         // a = b
    }
    int LocatePivot(point S[], int N)
    {
    	// Find point with smallest y coordinate; if there is more
    	// than one select the one with the smallest x coordinate.
    	int i, ndx;
    	long double minx, miny;
    	// initialize minimum coordinates found so far
    	ndx  = 0;
    	minx = S[ndx].x;
    	miny = S[ndx].y;
    	// locate lowest point in set
    	for (i = 1; i < N; i++)
    	{
    	   if (S[i].y > miny) continue;
    	   //    found a candidate for a new pivot
    	   if (S[i].y < miny)
    	   {
    		  ndx = i;
    		  miny = S[i].y;
    	   }
    	   else if (S[i].x < minx)
    	   {
    		  ndx = i;
    		  minx = S[i].x;
    	   }
    	}
    	// all done - return index of pivot point
    	return(ndx);
    }
    void Graham_Scan(point S[], int N)
    {
    	//***********************************************************************
    	// This is the actual Graham scan algorithm. It is called after the pivot 
    	// point has been located and the rest of the points have been sorted by 
    	// their polar angle.
    	
    	int i;
    	point p;
    	Push(S[0]);
    	Push(S[1]);
    	Push(S[2]);
    	for (i = 3; i < N; i++)
    	{
    	   while (CrossProduct(stack[TOS_ptr-2], stack[TOS_ptr-1], S[i]) < 0)
    	   {
    		  Pop(&p);
    	   };
    	   Push(S[i]);
    	}
    }

    now if I remove using name space, it compiles as long as I don't have #include "ConvexHull_Graham_Scan.h" anywhere else.

    when I include this line in my main file, it now complains that:

    ConvexHull_Graham_Scan.obj : error LNK2005: "struct point * S" (?S@@3PAUpoint@@A) already defined in stressfilereader.obj
    ConvexHull_Graham_Scan.obj : error LNK2005: "int TOS_ptr" (?TOS_ptr@@3HA) already defined in stressfilereader.obj
    ConvexHull_Graham_Scan.obj : error LNK2005: "struct point * stack" (?stack@@3PAUpoint@@A) already defined in stressfilereader.obj
    ConvexHull_Graham_Scan.obj : error LNK2005: "struct point Pivot" (?Pivot@@3Upoint@@A) already defined in stressfilereader.obj

    is there something obvious that I'm not seeing? :confused:
    Last edited by rssmps; 05-31-2006 at 05:27 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    Yep. Rather obvious :)

    You declare actual data storage in the header file. That means everywhere the file is included your compiler will try to allocate data with the same name. When the linker sees this it will complain.
    Just move the data storage declarations to the relevant CPP file. With encapsulation it should not be neccessary to access those variables from more than one file.

    Rune
    If you hit a brick wall, you didn't jump high enough!

  3. #3
    Join Date
    Oct 2005
    Posts
    173
    I'm confused, isn't the #ifndef supposed to guard against multiple definitions?

    Ok, so moving the those definitions into the cpp file allowed it to compile as long as I don't use those variables.

    in my main program when I need to access the variables, it says undefined.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <cstdlib>
    #include <ctime>
    #include "ConvexHull_Graham_Scan.h"
    
    void main(void)
    {
    	int i, N;
    	point p;
    	// make up some points
    	N = 12;
    	srand(time(0));
    	for (i = 0; i < N; i++)
    	{
    		S[i].x = (rand() % 10)*100;        <-----
    		S[i].y = (rand() % 10)*100;        <-----
    		printf("(%3f, %3f)\n", S[i].x, S[i].y);
    	}
    	// find the convex hull
    	i = LocatePivot(S, N);                             <-----
    	Pivot = S[i];                                          <-----
    	S[i]  = S[0];                                          <-----
    	S[0]  = Pivot;                                        <-----  
    	qsort((void *)&S[1], N-1, sizeof(S[0]), compare_points);  <-----
    	Graham_Scan(S, N);                               <-----
    	printf("points in convex hull\n");
    	// output points of convex hull in clockwise order
    	for (i = 0; Pop(&p) != 0; i++)
    	{
    		printf("(%3f, %3f)\n", p.x, p.y);
    	}
    	printf("%d points in convex hull\n", i);
    }

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    no, #include guards are suppsoed to protect against multiple *inclusions* of the same file during the same compilation session; they don't protect against multuple definitions because the file may be #included twice in two separate compilation sessions, which would of course lead to multiple definitions. As a rule, place declarations of functions, types and constants in a header file. Definitions -- only in a .cpp file that is compiled exactly once.
    Danny Kalev

  5. #5
    Join Date
    Oct 2005
    Posts
    173
    Code:
    #ifndef CONVEXHULL_GRAHAM_SCAN_H_
    #define CONVEXHULL_GRAHAM_SCAN_H_
    #define sizeof_Stack 100
    #define StackTop     stack[TOS_ptr-1]
    #define Stack2nd     stack[TOS_ptr-2]
    struct point
    {
       long double x, y;
    };
    point Pivot;
    point S[];
    point stack[]; 
    int Push(point p);
    int Pop(point *p);
    long double CrossProduct(point p0, point p1, point p2);
    int compare_points(const void *a, const void *b);
    int LocatePivot(point S[], int N);
    void Graham_Scan(point S[], int N);
    #endif

    Code:
    #include "ConvexHull_Graham_Scan.h"
    point stack[sizeof_Stack]; // the actual point stack array
    point S[sizeof_Stack];
    int TOS_ptr = 0;           // Top Of Stack pointer
    
    int Push(point p)
    {
       if (TOS_ptr == sizeof_Stack) return(0);
       stack[TOS_ptr++] = p;
       return(1);
    }
    
    int Pop(point *p)
    {
       if (TOS_ptr == 0) return(0);
       *p = stack[--TOS_ptr];
       return(1);
    }
    
    long double CrossProduct(point p0, point p1, point p2)
    {
       long double xprod;
       xprod = (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
       return(xprod);
    }
    
    int compare_points(const void *a, const void *b)
    {
       long double xprod;
       point *point_a, *point_b;
       point_a = (point *)a;
       point_b = (point *)b;
       xprod = CrossProduct(Pivot, *point_a, *point_b);
       if (xprod < 0)
       {
    	   return(+1);    // a > b
       }
       else if (xprod > 0)
       {
    	   return(-1);    // a < b
       }
       return(0);         // a = b
    }
    
    int LocatePivot(point S[], int N)
    {
       int i, ndx;
       long double minx, miny;
       // initialize minimum coordinates found so far
       ndx  = 0;
       minx = S[ndx].x;
       miny = S[ndx].y;
       // locate lowest point in set
       for (i = 1; i < N; i++)
       {
          if (S[i].y > miny) continue;
    	  //    found a candidate for a new pivot
          if (S[i].y < miny)
          {
             ndx = i;
             miny = S[i].y;
          }
          else if (S[i].x < minx)
          {
             ndx = i;
             minx = S[i].x;
          }
       }
       // all done - return index of pivot point
       return(ndx);
    }
    
    void Graham_Scan(point S[], int N)
    {
       int i;
       point p;
    
       Push(S[0]);
       Push(S[1]);
       Push(S[2]);
    
       for (i = 3; i < N; i++)
       {
          while (CrossProduct(Stack2nd, StackTop, S[i]) < 0)
          {
             Pop(&p);
          };
          Push(S[i]);
       }
    }
    ok, I added to the header file:
    point S[];
    point stack[];

    and placed these in the cpp.
    point stack[sizeof_Stack];
    point S[sizeof_Stack];

    Now S is being recongnize.....however, Pivot is still not being recongized.
    if I put point Pivot in the header, it says redefined.
    if I put it in the cpp file, it says unrecognized.

    where does point Pivot belong?

    main function is same as above.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Pivot is a variable, right? So why do you treat it as a type (i.e., a function parameter)? If it's an object it shouldn't be defined in a header file. If it's a type, it should.
    Danny Kalev

  7. #7
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    What Danny means is that every CPP file is a separate compilation unit, and the #ifdef construct prevents a header file from being included several times in one CPP file. The compiler creates one OBJ file per CPP file, and each of these will contain definitions of identical variables.
    The linker then assembles all of these OBJ files, and will complain about the duplicate names (as you have noticed...)

    If you absolutely must access the variables from other parts of the application you have two alternatives:
    1. (the best) Create functions that return the values contained in the variables. Declared in the header file, defined in the CPP.
    2. Put extern declarations in the header file, and actual definitions in the CPP.

    Rune
    If you hit a brick wall, you didn't jump high enough!

Similar Threads

  1. I can't link FreeImage to C#
    By mesh2005 in forum .NET
    Replies: 1
    Last Post: 03-06-2006, 09:16 AM
  2. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 04:53 AM
  3. Returning errors from SQL Server Stored Procs
    By Khalizan in forum VB Classic
    Replies: 1
    Last Post: 11-28-2001, 02:32 AM
  4. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 11:50 PM
  5. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center