各位帮忙看看 sting前向声明的问题

本文探讨了C++编程中遇到的一个常见问题:当使用标准库中的string类型时出现的引用歧义错误。文中通过具体的代码示例,详细解释了如何正确地包含必要的头文件并使用命名空间来避免此类编译错误。

/*****************************文件tt.h***********************************/

  1 class string;
  2 string *tt();


/***************************文件tt.cpp***************************/
  1 #include <iostream>
  2 #include <string>
  3 #include "tt.h"
  4 
  5 using namespace std;
  6 string *tt(){
  7     return new string("dddddd");
  8 }
  9 int main(int argc, const char *argv[])

 10 {

 11

 12     return 0;

 13 }


g++版本:

g++ (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)

g++ tt.cpp tt.h 出现以下错误:

tt.cpp:6: 错误:对‘string’的引用有歧义
tt.h:1: 错误:candidates are: struct string
/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/stringfwd.h:56: 错误:                typedef struct std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::string
tt.cpp:6: 错误:expected constructor, destructor, or type conversion before ‘*’ token

不在头文件中加入#include <string>和using std::string;请大家帮忙看看什么问题?

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值