string undeclared identifier in class












0















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.



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


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;
}


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;
}


and the errors that I get:




Severity Code Description Project File Line Suppression State



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



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



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



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



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



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



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



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

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



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



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




and other errors...










share|improve this question





























    0















    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.



    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


    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;
    }


    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;
    }


    and the errors that I get:




    Severity Code Description Project File Line Suppression State



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



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



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



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



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



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



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



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

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



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



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




    and other errors...










    share|improve this question



























      0












      0








      0








      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.



      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


      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;
      }


      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;
      }


      and the errors that I get:




      Severity Code Description Project File Line Suppression State



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



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



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



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



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



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



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



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

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



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



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




      and other errors...










      share|improve this question
















      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.



      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


      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;
      }


      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;
      }


      and the errors that I get:




      Severity Code Description Project File Line Suppression State



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



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



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



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



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



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



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



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

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



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



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




      and other errors...







      c++ class






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:09









      Werner Henze

      10.7k72854




      10.7k72854










      asked Jan 2 at 6:33









      meysamimanimeysamimani

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          5














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




          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





          share|improve this answer































            0














            This answer appears to address your question.
            As a side note, since C++11, you can also specify default values for class members. So you could just do this instead:



            #ifndef BOOK_H
            #define BOOK_H

            #include <string>

            class book
            {
            private:
            std::string name = "default";

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

            #endif





            share|improve this answer


























            • In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

              – UnholySheep
              Jan 2 at 6:57











            • I like the addition, however, you didn't explain where the actual issue was

              – JVApen
              Jan 2 at 6:58











            • You need to add a default constructor: book() = default;

              – Remy Lebeau
              Jan 2 at 8:31













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002183%2fstring-undeclared-identifier-in-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














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




            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





            share|improve this answer




























              5














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




              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





              share|improve this answer


























                5












                5








                5







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




                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





                share|improve this answer













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




                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 6:37









                R SahuR Sahu

                168k1292191




                168k1292191

























                    0














                    This answer appears to address your question.
                    As a side note, since C++11, you can also specify default values for class members. So you could just do this instead:



                    #ifndef BOOK_H
                    #define BOOK_H

                    #include <string>

                    class book
                    {
                    private:
                    std::string name = "default";

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

                    #endif





                    share|improve this answer


























                    • In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

                      – UnholySheep
                      Jan 2 at 6:57











                    • I like the addition, however, you didn't explain where the actual issue was

                      – JVApen
                      Jan 2 at 6:58











                    • You need to add a default constructor: book() = default;

                      – Remy Lebeau
                      Jan 2 at 8:31


















                    0














                    This answer appears to address your question.
                    As a side note, since C++11, you can also specify default values for class members. So you could just do this instead:



                    #ifndef BOOK_H
                    #define BOOK_H

                    #include <string>

                    class book
                    {
                    private:
                    std::string name = "default";

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

                    #endif





                    share|improve this answer


























                    • In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

                      – UnholySheep
                      Jan 2 at 6:57











                    • I like the addition, however, you didn't explain where the actual issue was

                      – JVApen
                      Jan 2 at 6:58











                    • You need to add a default constructor: book() = default;

                      – Remy Lebeau
                      Jan 2 at 8:31
















                    0












                    0








                    0







                    This answer appears to address your question.
                    As a side note, since C++11, you can also specify default values for class members. So you could just do this instead:



                    #ifndef BOOK_H
                    #define BOOK_H

                    #include <string>

                    class book
                    {
                    private:
                    std::string name = "default";

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

                    #endif





                    share|improve this answer















                    This answer appears to address your question.
                    As a side note, since C++11, you can also specify default values for class members. So you could just do this instead:



                    #ifndef BOOK_H
                    #define BOOK_H

                    #include <string>

                    class book
                    {
                    private:
                    std::string name = "default";

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

                    #endif






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 3 at 0:54

























                    answered Jan 2 at 6:54









                    betelgeusebetelgeuse

                    194




                    194













                    • In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

                      – UnholySheep
                      Jan 2 at 6:57











                    • I like the addition, however, you didn't explain where the actual issue was

                      – JVApen
                      Jan 2 at 6:58











                    • You need to add a default constructor: book() = default;

                      – Remy Lebeau
                      Jan 2 at 8:31





















                    • In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

                      – UnholySheep
                      Jan 2 at 6:57











                    • I like the addition, however, you didn't explain where the actual issue was

                      – JVApen
                      Jan 2 at 6:58











                    • You need to add a default constructor: book() = default;

                      – Remy Lebeau
                      Jan 2 at 8:31



















                    In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

                    – UnholySheep
                    Jan 2 at 6:57





                    In this code the default value will never be used, as you don't have a default constructor (and the provided one replaces it)

                    – UnholySheep
                    Jan 2 at 6:57













                    I like the addition, however, you didn't explain where the actual issue was

                    – JVApen
                    Jan 2 at 6:58





                    I like the addition, however, you didn't explain where the actual issue was

                    – JVApen
                    Jan 2 at 6:58













                    You need to add a default constructor: book() = default;

                    – Remy Lebeau
                    Jan 2 at 8:31







                    You need to add a default constructor: book() = default;

                    – Remy Lebeau
                    Jan 2 at 8:31




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002183%2fstring-undeclared-identifier-in-class%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas