各位大侠好,小弟有一C++程序在调试时总是遇到错误,请各位大侠帮忙,代码及错误如下:

“hello.h”文件:#ifndefHELLO_H#defineHELLO_H#include<string.h>classhello{public:hello(con... “hello.h”文件:
#ifndef HELLO_H
#define HELLO_H
#include<string.h>
class hello
{
public:
hello(const string& m,const string& n)
{
a=m;
b=n;

};
string add()
{
return a+b;
};

private:
string a,b;
};
#endif
main函数如下:

#include"hello.h"
#include<iostream.h>
int main()
{
hello a("agag","ag");
cout<<a.add()<<endl;
return 0;
}
错误如下:
d:\exercise\lll\hello.h(7) : error C2629: unexpected 'class hello ('
d:\exercise\lll\hello.h(7) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
d:\exercise\lll\hello.h(13) : error C2146: syntax error : missing ';' before identifier 'add'
d:\exercise\lll\hello.h(13) : error C2501: 'string' : missing storage-class or type specifiers
d:\exercise\lll\hello.h(16) : warning C4183: 'add': member function definition looks like a ctor, but name does not match enclosing class
d:\exercise\lll\hello.h(19) : error C2146: syntax error : missing ';' before identifier 'a'
d:\exercise\lll\hello.h(19) : error C2501: 'string' : missing storage-class or type specifiers
d:\exercise\lll\hello.h(19) : error C2501: 'a' : missing storage-class or type specifiers
d:\exercise\lll\hello.h(19) : error C2501: 'b' : missing storage-class or type specifiers
d:\exercise\lll\ceshi.cpp(7) : error C2078: too many initializers
d:\exercise\lll\ceshi.cpp(7) : error C2440: 'initializing' : cannot convert from 'char [3]' to 'class hello'
No constructor could take the source type, or constructor overload resolution was ambiguous
还有一点奇怪的地方是:我把类中的private值a和b改为int型时,再编译运行就没有问题了!
展开
 我来答
dongyue2008
2011-03-14 · TA获得超过3606个赞
知道大有可为答主
回答量:1300
采纳率:100%
帮助的人:692万
展开全部
主函数return 0;后面的分号应该是英文分号,而不是中文分号。
另外,在头文件定义类的成员函数时,函数最后的右花括号 } 后面不需要分号。
追问
按照您说的改过后,出现同样的错误提示。
一直令我迷惑的是当把a和b的类型改为int时,就没有问题了。
追答
另外不要#include
这个是C的头文件,不支持 cout 操作的,所以你包含这个头文件试图 cout 一个 string 就错了。
改为:
#include
using namespace std;
浙江启扬智能科技有限公司
2023-06-12 广告
Linux 嵌入式系统中,USB 启动模式能够烧写 ARM 的 uboot 的原因主要有以下几个方面:1. USB 启动模式相对于传统的 BIOS 启动模式来说,具有更高的兼容性和灵活性,可以支持更多的硬件设备和操作系统。2. USB 启动... 点击进入详情页
本回答由浙江启扬智能科技有限公司提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式