开发者社区> 问答> 正文

在头文件里定义一个string name; 变量,编译报错 ?报错

//Parent.h
class Parent
{
public:
Parent(void);
~Parent(void);
private:
string name;
};

//main.cpp

#include
#include
#include "Parent.h"
using namespace std;

void main()
{
system("pause");
}

//Parent.cpp
#include "Parent.h"

Parent::Parent(void)
{
}

Parent::~Parent(void)
{
}

//编译器报错
错误 1 error C2146: 语法错误: 缺少“;”(在标识符“name”的前面) d:\用户目录\我的文档\visual studio 2012\projects\string\string\parent.h 8
错误 2 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int d:\用户目录\我的文档\visual studio 2012\projects\string\string\parent.h 8
错误 3 error C2146: 语法错误: 缺少“;”(在标识符“name”的前面) d:\用户目录\我的文档\visual studio 2012\projects\string\string\parent.h 8
错误 4 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int d:\用户目录\我的文档\visual studio 2012\projects\string\string\parent.h 8
5 IntelliSense: 未定义标识符 "string" d:\用户目录\我的文档\Visual Studio 2012\Projects\string\string\Parent.h 8

展开
收起
爱吃鱼的程序员 2020-06-23 14:38:39 578 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    析构函数不传参数吧,传参数也不是这样传吧

    //Parent.h#pragmaonce#include<iostream>usingnamespacestd;classParent{public:Parent(void);~Parent(void);private:stringname;};//main.cpp#include"Parent.h"voidmain(){system("pause");}//Parent.cpp#include"Parent.h"Parent::Parent(void){}Parent::~Parent(void){}

    不知道不知道不知道不知道不知道不知道

    //Parent.cpp
    #include"Parent.h"
    这个源代码文件使用string之前要
    #include
    usingnamespacestd;

    把下面放到Parent.h文件开头

    #include<iostream>usingnamespacestd;

    包含头文件
    #include

    //Parent.h
    #include
    #include
    usingnamespacestd;

    classParent
    {
    public:
    Parent(void);
    ~Parent(void);
    private:
    stringname;
    };

    必须包含usingnamespacestd;这条语句,因为string类是包含在std这个命名空间里面的。

    必须包含usingnamespacestd;这条语句,因为string类是包含在std这个命名空间里面的。

    2020-06-23 14:38:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载