c++有关string的报错 “”声明与所属文件不兼容 急切求助!

zzcc209 2012-09-10 10:34:58
环境vs2010
目的:想要实现string类型的参数传递,在头文件中声明,在源文件中定义
报错真心搞不明白。。。求助啊,各位!


//在头文件 a.h中代码如下:
#ifndef _STRING
#define _STRING
#endif
namespace a
{
class b
{
static __declspec(dllexport) string c(string str);
};
}
//在源文件helloworld.cpp中代码如下:
#include<iostream>
#include"a.h"
#include<string>
using namespace std;
using namespace a;
string b::c(string str) /*就在此处报错:IntelliSense: 声明与 "<error-type> a::b::c(<error-type> str)" (已声明 所在行数: 9,所属文件: "c:\documents and settings\administrator\my documents\visual studio 2010\projects\test\a.h") 不兼容 c:\documents and settings\administrator\my documents\visual studio 2010\projects\test\helloworld.cpp 66 17 test
*/

{
string s;
int len=str.length();
for(int i=0;i<len;i++)
{
s[i]=str[i];
}
return s;
}

//如果您看不明白的话可以加我qq:914351863
...全文
9524 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhouYates 2012-10-16
  • 打赏
  • 举报
回复 1
在CPP里写类成员函数时,要把类名加上,如果要调用成员变量也是,必须加上类名::
图灵狗 2012-09-10
  • 打赏
  • 举报
回复
1、a.h改为如下:

#ifndef _STRING
#define _STRING

#include <string>
using namespace std;

namespace a
{
class b
{
static string c(string str);
};
}

#endif

2、a.cpp改为如下:

#include <iostream>
#include <string>
#include "a.h"

using namespace std;
using namespace a;

string b::c(string str)
{
string s;
int len = str.length();
for(int i = 0; i < len; i++)
{
s[i] = str[i];
}

return s;
}

int main(int argc, char* argv[])
{
return 0;
}
  • 打赏
  • 举报
回复
类成员的话,你得声明整个类才行。
zzcc209 2012-09-10
  • 打赏
  • 举报
回复
刚才输入失误了,在helloworld中包含
#include"a.h"的
继续求助。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧