首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

#include <string> using namespace std;报错解决办法

2012-05-03 
#include string using namespace std报错新建一个application,空白的unti1,再添加一个unit2unit 2.hC/

#include <string> using namespace std;报错
新建一个application,空白的unti1,再添加一个unit2
unit 2.h

C/C++ code
//---------------------------------------#ifndef Unit2H#define Unit2H//---------------------------------------string &replace_all_distinct(string::size_type s_pos,string &str, const string &old_value, const string &new_value);string &replace_all(string &str, const string &old_value, const string &new_value);#endif


unit2.cpp
C/C++ code
//---------------------------------------#pragma hdrstop#include "Unit2.h"#include <string>using namespace std;//---------------------------------------string &replace_all(string &str, const string &old_value, const string &new_value){    while(true)    {        string::size_type pos(0);        if(   (pos=str.find(old_value)) != string::npos   )            str.replace(pos,old_value.length(),new_value);        else   break;    }    return str;}string replace_all_distinct(string::size_type s_pos, string &str, const string &old_value, const string &new_value){    for(string::size_type pos = s_pos; pos!=string::npos; pos+=new_value.length())    {        if(   (pos=str.find(old_value,pos)) != string::npos   )            str.replace(pos,old_value.length(),new_value);        else   break;    }    return str;}#pragma package(smart_init)

编译报错:
[C++ Error] Unit2.h(7): E2141 Declaration syntax error
[C++ Error] Unit2.h(9): E2238 Multiple declaration for 'string'
[C++ Error] Unit2.h(7): E2344 Earlier declaration of 'string'
[C++ Error] Unit2.h(9): E2141 Declaration syntax error
[C++ Error] Unit2.cpp(12): E2015 Ambiguity between 'string' and '_STL::string'
[C++ Error] Unit2.cpp(12): E2238 Multiple declaration for 'string'
[C++ Error] Unit2.h(9): E2344 Earlier declaration of 'string'
[C++ Error] Unit2.cpp(12): E2141 Declaration syntax error
折腾了一下午,没搞出个名堂来,求指教!

[解决办法]
#include <string>
using namespace std;
这两行代码放在unit2.h文件中,如:
C/C++ code
#ifndef Unit2H#define Unit2H#include <string>using namespace std;//---------------------------------------string &replace_all_distinct(string::size_type s_pos,string &str, const string &old_value, const string &new_value);string &replace_all(string &str, const string &old_value, const string &new_value);#endif 

热点排行