-
February 27th, 2014, 12:31 AM
#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?
-
February 27th, 2014, 01:13 AM
#2
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
-
February 27th, 2014, 02:20 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|