Solved

C++ Second Defintion Ignored Warnings... struct objects header & CPP

Posted on 2011-03-21
3
811 Views
Last Modified: 2012-08-13
I am unable to link my project without "second definition ignored' warnings.
1>__NDatabase.obj : warning LNK4006: "struct _DataTypeMap * DataTypeMap" (?DataTypeMap@@3PAU_DataTypeMap@@A) already defined in __NVTransfers.obj; second definition ignored

Are my header and CPP files correctly configure for a object type of struct ?

Header file
#pragma once
#include "StaticData.h"

#ifndef _DataTypeMap_IMPLEMENT_
#define _DataTypeMap_IMPLEMENT_

struct _DataTypeMap
{
	LPCTSTR Name;
	UINT    OrdinalType;
	LPCTSTR SQLInteger;           // SQL_INTEGER  = 4
	LPCTSTR SQLSmallInt;          // SQL_SMALLINT = 5
	LPCTSTR SQLTinyInt;           // SQL_TINYINT  = -6
	LPCTSTR SQLBigInt;            // SQL_BIGINT   = -5
	LPCTSTR SQLReal;              // SQL_REAL     = 7
	LPCTSTR SQLNumeric;           // SQL_NUMERIC  = 2
	LPCTSTR SQLDecimal;           // SQL_DECIMAL  = 3
	LPCTSTR SQLFloat;             // SQL_FLOAT    = 6
	LPCTSTR SQLDouble;            // SQL_DOUBLE   = 8
	LPCTSTR SQLLongVarBinary;     // SQL_LONGVARBINARY = -4
	LPCTSTR SQLChar;              // SQL_CHAR     = 1
	LPCTSTR SQLVarChar;           // SQL_VARCHAR  = 12
	LPCTSTR SQLLongVarChar;       // SQL_LONGVARCHAR = -1
	LPCTSTR SQLTimeStamp;         // SQL_TIMESTAMP = 11, SQL_TYPE_TIMESTAMP = 93
	LPCTSTR SQLDateTime;          // SQL_DATETIME = 9
	LPCTSTR SQLDate;              // SQL_DATE     = 9, SQL_TYPE_DATE = 91
	LPCTSTR SQLTime;              // SQL_TIME     = 10, SQL_TYPE_TIME = 92
	LPCTSTR SQLBit;               // SQL_BIT      = -7
	LPCTSTR SQLVarBinary;         // SQL_VARBINARY = -3
	LPCTSTR SQLNull;              // 
} DataTypeMap[] = {
	_T("Default"),                 SQLSRV, _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Oracle"),                  ORACLE,     _T("integer"), _T("integer"),  _T("integer"), _T("number"), _T("float"), _T("numeric"), _T("number"), _T("float"), _T("number"), _T("longvarbinary"), _T("char"), _T("varchar2"), _T("longvarchar"), _T("timestamp"), _T("date"), _T("date"),     _T("time"), _T("bit"), _T("raw"),        _T("null"),
	_T("Ingres"),                  OPEN_INGRES,     _T("integer"), _T("integer"),  _T("integer"), _T("number"), _T("float"), _T("numeric"), _T("number"), _T("float"), _T("number"), _T("longvarbinary"), _T("char"), _T("varchar"), _T("longvarchar"), _T("timestamp"), _T("date"), _T("date"),     _T("time"), _T("bit"), _T("raw"),        _T("null"),
	_T("Oracle8"),                 ORACLE,     _T("integer"), _T("integer"),  _T("integer"), _T("number"), _T("float"), _T("numeric"), _T("number"), _T("float"), _T("number"), _T("longvarbinary"), _T("char"), _T("varchar2"), _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("raw"),        _T("null"),
	_T("Microsoft SQL Server"),    SQLSRV, _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("image"),         _T("char"), _T("varchar"),  _T("longvarchar"), _T("datetime"),  _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Rdb"),                     RDB,        _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Access"),                  ACCESS,     _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Informix"),                INFORMIX,   _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Sybase"),                  SYBASE,     _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("DB2"),                     DB2,        _T("integer"), _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("MySQL"),                   MYSQL,      _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double"),           _T("longblob"),      _T("char"), _T("varchar"),  _T("longtext"),    _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("tinyint(1)"), _T("varbinary"), _T("null"),
	_T("TurboImage"),              TURBOIMAGE, _T("integer"), _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("numeric"), _T("float"), _T("double"),           _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Attunity Connect Driver"), ATTUNITY,   _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Rename Me"),               CONNX,      _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
};
#endif

Open in new window


CPP  File
#include "stdafx.h"
#include "_DataTypeMap.h"

Open in new window

0
Comment
Question by:TimPeer
3 Comments
 
LVL 15

Accepted Solution

by:
DonConsolio earned 250 total points
ID: 35186522
"second definition ignored' warning means: "struct _DataTypeMap * DataTypeMap" is
compiled into  __NDatabase.obj and into __NVTransfers.obj, but only one object with
the same (mangled) name  (i.e. same name and same prototype) can be linked into
your executable.

Header files should be used for definitions, but not for actual variables, as they tend to be
included multiple times.

To get rid of your problem
 
// in header file: the prototype
// can be included multiple times
-----------------------------------
struct _t_DataTypeMap
{
.....
};
typedef struct _t_DataTypeMap _DataTypeMap;

extern _DataTypeMap DataTypeMap[];
-----------------------------------

in ONE c++ file (e.g. main.cpp)
-----------------------------------
_DataTypeMap DataTypeMap[] = {
	_T("Default"),                 SQLSRV, _T("int"),     
....
};
-----------------------------------

Open in new window

0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 35187187
if you have a manager class (singleton class) which handles the DataTypeMap i would suggest to make the definition of the array a member of this class. this class also could provide functions to access the array from other classes if needed.

generally, you can't put a definition into a header file cause each cpp which includes the header would create the same symbol (a named global variable pointing to some part of memory) again what gives linker error. header files only may contain declarations, for example of struct/class or function prototypes, extern declarations as told by DonConsolio, preprocessor macros enumerations, and inline code.

Sara
0
 

Author Closing Comment

by:TimPeer
ID: 35196371
I abandoned the struct type and embedded in a class with the CMapStringToOb so will split the points since I didn't select either solution.

Thanks!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 102
Need some help with Microsoft Visual Studio C++ 2003 5 60
c++ syntax question 9 48
Beginner to Unreal Engine 4 5 79
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question