1

I have created a new .h file with the following contents:

#include "stdafx.h"
#include <string>
using namespace std;

struct udtCharVec
{
    wstring GraphemeM3;
    wstring GraphemeM2;
};

When I want to compile it, the compiler tells me "error C2011: udtCharVec: struct type redefintion".

I did a text search, and I don't have "struct udtCharVec" defined anywhere else.

Does anybody see where I went wrong?

user2421725
  • 79
  • 2
  • 6
  • you got error in current file where you defined struct? or are you using this code as include file for someother file? – Dineshkumar May 26 '13 at 09:40
  • 1
    Aside from your problem, don't use a "using directive" in a header file: http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c – Michael Burr May 26 '13 at 09:41
  • where is the usage code of your header file? Edit your question and post it... – pinkpanther May 26 '13 at 09:41

3 Answers3

5

You are probably including this header file more than once in a single translation unit. When the file is included for the second time, struct udtCharVec has already been defined, and so you get a "type redefinition" error.

Add an include guard. After the first inclusion, CharVec_H will be defined, and so the rest of the file will be skipped:

#ifndef CharVec_H
#define CharVec_H
#include "stdafx.h"
#include <string>
using namespace std

struct udtCharVec
{
    wstring GraphemeM3;
    wstring GraphemeM2;
};
#endif

Say your project consisted of three files. Two header files and one source file:

CharVec.h

#include "stdafx.h"
#include <string>
using namespace std

struct udtCharVec
{
    wstring GraphemeM3;
    wstring GraphemeM2;
};

CharMatrix.h

#include "CharVec.h"
struct udtCharMatrix
{
    CharVec vec[4];
};

main.cpp

#include "CharVec.h"
#include "CharMatrix.h"

int main() {
    udtCharMatrix matrix = {};
    CharVec vec = matrix.vec[2];
};

After the preprocessor had run, main.cpp would look like this (ignoring standard library includes):

//#include "CharVec.h":
    #include "stdafx.h"
    #include <string>
    using namespace std

    struct udtCharVec //!!First definition!!
    {
        wstring GraphemeM3;
        wstring GraphemeM2;
    };
//#include "CharMatrix.h":
    //#include "CharVec.h":
        #include "stdafx.h"
        #include <string>
        using namespace std

        struct udtCharVec //!!Second definition!!
        {
            wstring GraphemeM3;
            wstring GraphemeM2;
        };
    struct udtCharMatrix
    {
        CharVec vec[4];
    };

int main() {
    udtCharMatrix matrix = {};
    CharVec vec = matrix.vec[2];
};

This expanded file include two definitions of struct udtCharVec. If you add an include guard to CharVec.h, the second definition will be removed by the preprocessor.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
  • Thank you. It worked, but I don't understand why. What do you mean by "You are probally including this header file more than once in a single translation unit"? – user2421725 May 26 '13 at 09:43
  • @user2421725 means you are including #include more than one time in your program which caused CharVec_H trying to be defined twice hence the error – pinkpanther May 26 '13 at 09:45
  • 1
    It would be wise to remove the `using namespace std` from the header. – juanchopanza May 26 '13 at 09:46
2

add this macro at the top of the header file

#pragma once
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ORYRICK
  • 21
  • 1
0

Normally problems like that have additional information in the Output pane (while the Error List only gets the first line), and you can just click to go to the previous definition.

If it goes to the same location, then indeed you have the file included several times -- you can turn on Show includes option under C++/advanced to see all includes listed as they happen.

Majority of the .h files shall have include guards or #pragma once to avoid such errors.

Also you shall not do #include "stdafx.h" in a header file -- that shall be done at start of .cpp file (suboptimal) or specified in the project as forced include.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37