Solved

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

Posted on 2011-03-21
3
806 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 14

Accepted Solution

by:
DonConsolio earned 250 total points
Comment Utility
"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 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now