类中未声明的字符串标识符 [英] string undeclared identifier in class

查看:81
本文介绍了类中未声明的字符串标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习c ++继承,这里有一个问题.如果我将这个简单的代码全部放在main.cpp文件中,它将可以正常工作而无需任何操作问题.

I am learning c++ inheritance and here I have a problem. if I make this simple code all in main.cpp file it will works without any problem.

但是当我分离头文件中的文件时,否则它不起作用,并且给了我一些错误.

but when I seperate the files in header file and else it won't work and it gives me some errors.

这是名为book.h的头文件的代码

here is the code of header file named book.h

 #ifndef BOOK_H
 #define BOOK_H

 class book
{
 private:
 string name;

 public:
 book(string n = "default") : name(n) {};
 ~book() {};
 void printname();
};

#endif

这是book.cpp的代码,我定义了此类的功能在此文件中.

and here is the code of book.cpp that I defined the function of this class in this file.

#include <iostream>
#include <Windows.h>
#include <string>
#include "book.h"

using namespace std;

void book::printname()
{
 cout << name << endl;
 return;
}

最后是main.cpp文件:

and finally the main.cpp file:

#include <iostream>
#include <Windows.h> 
#include <string>
#include "book.h"

using namespace std;

 int main()
{
 system("color 0A");
 book programing("c++");
 cout << "the name of the book is ";
 programing.printname();

 system("pause");
 return;
}

和我得到的错误:

严重性代码说明项目文件行抑制状态

Severity Code Description Project File Line Suppression State

错误C2065'名称':未声明的标识符簿d:\ vs程序\ book \ book \ book.cpp 10

Error C2065 'name': undeclared identifier book d:\vs program\book\book\book.cpp 10

错误C3646'名称':未知的替代说明书d:\ vs程序\ book \ book \ book.h 7

Error C3646 'name': unknown override specifier book d:\vs program\book\book\book.h 7

错误C4430缺少类型说明符-假定为int.注意:C ++不会支持default-int book d:\ vs program \ book \ book \ book.h 7

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int book d:\vs program\book\book\book.h 7

错误C2061语法错误:标识符'string'书d:\ vs程序\ book \ book \ book.h 10

Error C2061 syntax error: identifier 'string' book d:\vs program\book\book\book.h 10

错误C2065'n':未声明的标识符簿d:\ vs程序\ book \ book \ book.h 10

Error C2065 'n': undeclared identifier book d:\vs program\book\book\book.h 10

错误C2614'book':成员初始化非法:'name'不是基本书或会员书d:\ vs program \ book \ book \ book.h 10

Error C2614 'book': illegal member initialization: 'name' is not a base or member book d:\vs program\book\book\book.h 10

错误C3646'名称':未知的替代说明书d:\ vs程序\ book \ book \ book.h 7

Error C3646 'name': unknown override specifier book d:\vs program\book\book\book.h 7

错误C4430缺少类型说明符-假定为int.注意:C ++不会支持default-int book d:\ vs program \ book \ book \ book.h 7
错误C2061语法错误:标识符'string'书d:\ vs程序\ book \ book \ book.h 10

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int book d:\vs program\book\book\book.h 7
Error C2061 syntax error: identifier 'string' book d:\vs program\book\book\book.h 10

错误C2065'n':未声明的标识符簿d:\ vs程序\ book \ book \ book.h 10

Error C2065 'n': undeclared identifier book d:\vs program\book\book\book.h 10

错误C2614'book':成员初始化非法:'name'不是基本书或会员书d:\ vs program \ book \ book \ book.h 10

Error C2614 'book': illegal member initialization: 'name' is not a base or member book d:\vs program\book\book\book.h 10

和其他错误...

推荐答案

您需要确保.h文件中的 string 是有效类型.

You need to make sure that string is a valid type in the .h file.

  1. 添加 #include< string> .
  2. 使用 std :: string 而不只是 string .
  1. Add #include <string>.
  2. Use std::string instead of just string.


#ifndef BOOK_H
#define BOOK_H

#include <string>

class book
{
   private:
      std::string name;

   public:
      book(std::string n = "default") : name(n) {};
      ~book() {};
      void printname();
};

#endif

这篇关于类中未声明的字符串标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆