Solved

definition error in visual studio C++

Posted on 2014-01-18
8
699 Views
Last Modified: 2014-01-21
hi


I seem to be getting the following error all of a sudden when trying to compile my code. I dont fully understand what it means. The part that is really confusing is that I have not made any changes to the A.h file or included it specifically anywhere between the last compiling stage and the current non-compilable stage. Any ideas how this might have come  up andhow i can  fix this?

c:\A.h(107) : error C2766: explicit specialization; '`anonymous-namespace'::ctti<int>' has already been defined
3>        c:\A.h(95) : see previous definition of 'ctti<int>'

Find Results 1, "Entire Solution"
  C:\A.h(95):template<> struct ctti<int> : ctti_base<int> {


line 95 is
#ifdef DETECTED_OS_WINDOWS
template<> struct ctti<int> : ctti_base<int> {
  typedef unsigned int size_type;
  typedef int  signed_type;
};

line 107 is
template<> struct ctti<boost::int32_t> : ctti_base<boost::int32_t> {
  typedef boost::uint32_t size_type;
  typedef boost::int32_t signed_type;
};
0
Comment
Question by:LuckyLucks
  • 4
  • 3
8 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 39791769
I never heard of this error before so I looked it up.
http://msdn.microsoft.com/en-us/library/2x4hcz93.aspx
// C2766.cpp
// compile with: /c
template<class T> 
struct A {};

template<> 
struct A<int> {};                   // specialization #1a

template<> 
struct A<int> {};   // C2766     // specialization #1b
// try the following line instead
// struct A<char> {};     // specialization #2

Open in new window

Specializations 1a and 1b are obviously duplicates, so #2 could be used instead.

Now back to your case. My guess is that struct ctti<int>  and struct ctti<boost::int32_t> are seen by the compiler on your system as the same in that if your system has int as a 32 bit word, then that is the same as boost::int32_t.
Just a guess.

Other wild guess is to make sure that you have guards around all your header files.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39793997
Other wild guess is to make sure that you have guards around all your header files.
the guess is good and by my experience the more reasonable issue.

if the a.h was not "protected" by a

#pragma once 

Open in new window

statement or by macros like

// a.h
#ifndef A_H
#define A_H
// put here the current contents of the header 
...
#endif // A_H

Open in new window

the template definition would be defined twice when the a.h was included in two source files.

// b.h
....
#include "a.h"
...

// b.cpp
...
#include "b.h"
#include "a.h"   
...

Open in new window

when including a.h the second time the error could occur when a.h is not protected.

Sara
0
 

Author Comment

by:LuckyLucks
ID: 39795952
i put in #pragma once in A.h - did not change the error.

I notcied something interesting though.
I tried to reproduce the errror by slowly adding code that i thought might introduce the error in the first place.


Seems when I add the below line of code to one of my other header files C.h (not related or refering to A.h in any way) ,
std::tr1::unordered_map<size_t,std::vector<UINT64>> myhash;
I get this error.

What's strange is if i comment out the line, the error does not go away.

What causes the compiler to not let go of this error?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39796127
try changing line 107 to use <boost::uint32_t> instead of <boost::int32_t>.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:LuckyLucks
ID: 39797686
is there an easier way to find out which header file it's definition is being picked up first? Going through all the headers in the headers  seem a really painful way to go in a big project.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 39798654
Not sure if this will help you. In Visual Studio, there are two toggles with regards to headers. In project properties, under Configuration Properties -> C/C++:
Preprocessor -> Preprocess to a File -> Yes (/P)
The (/P) provides a text .i file which shows what the compiler processes.

Advanced -> Show Includes -> Yes (/showIncludes)
As you compile, the headers that are included are shown as they are included by the preprocessor.

I would work with them one at a time, and compare the results to make sure that you understand the outputs.
0
 

Author Comment

by:LuckyLucks
ID: 39799003
@phoffric : thanks! there is a certain third party header file that is being included at numerous places. I am surprised that the compiler has not complained at the 2nd instance it was included but instead much later.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39799011
Glad you were able to figure it out. Lord knows, we tried, but without a little more source code, all we could do is WAG. You asked the right question to narrow down the problem. Nice going.

Paul
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

920 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

17 Experts available now in Live!

Get 1:1 Help Now