“redefinition of 'string' as different kind of symbol”
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: “redefinition of 'string' as different kind of symbol”

  1. #1
    Join Date
    Feb 2014
    Posts
    1

    “redefinition of 'string' as different kind of symbol”

    I declared a function which returns std::string in read_qacct.h

    Code:
    #include <string>
    std::string read_qacct(std::string login, int days);
    Then I include read_qacct.h in another cpp file called db2class.cpp

    Code:
    #include "read_qacct.h"
    When I compile db2class though, the first error is

    Code:
    In file included from db2class.cpp:8:
    ./read_qacct.h:2:6: error: redefinition of 'string' as different kind of symbol
    std::string read_qacct(std::string login, int days);
         ^
    /usr/include/c++/4.2.1/bits/stringfwd.h:59:33: note: previous definition is here
      typedef basic_string<char>    string;
    I had read_qacct.h included the same way in read_qacct.cpp where the function read_qacct was defined. I successfully compiled read_qacct.cpp. How come I got this weird error for db2class.cpp?

  2. #2
    Join Date
    Jul 2013
    Posts
    225

    Re: “redefinition of 'string' as different kind of symbol”

    Use a unique include guard in every .h file like say this in read_qacct.h,

    Code:
    #ifndef READ_QACCT_ONCE 
    #define READ_QACCT_ONCE
    
       #include <string>
       std::string read_qacct(std::string login, int days);
    
    #endif

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: “redefinition of 'string' as different kind of symbol”

    razzle's suggestion is a general good practice recommendation, but I don't think that it will solve your problem here since an inclusion guard ensures that multiple inclusions of a header in a translation unit (e.g., your source file includes the same header twice, or includes two different headers that themselves include a particular header) will boil down to just one inclusion of that header, thus avoiding say, the error of a redefinition of a class. In this case it looks like you have two translation units (corresponding to the source files db2class.cpp and read_qacct.cpp) in which read_qacct.h is included once each, so I reason that the introduction of inclusion guards will make no difference, though you should put them in anyway.

    That said, I cannot think of what else might cause this problem given what you've shown here. I suggest that you post the code for the smallest and simplest program that you expect should compile but which demonstrates this error.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center