string undeclared identifier in class
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
add a comment |
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
add a comment |
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
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
c++ class
edited Jan 2 at 7:09
Werner Henze
10.7k72854
10.7k72854
asked Jan 2 at 6:33
meysamimanimeysamimani
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You need to make sure that string
is a valid type in the .h file.
- Add
#include <string>
. - Use
std::string
instead of juststring
.
#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
add a comment |
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You need to make sure that string
is a valid type in the .h file.
- Add
#include <string>
. - Use
std::string
instead of juststring
.
#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
add a comment |
You need to make sure that string
is a valid type in the .h file.
- Add
#include <string>
. - Use
std::string
instead of juststring
.
#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
add a comment |
You need to make sure that string
is a valid type in the .h file.
- Add
#include <string>
. - Use
std::string
instead of juststring
.
#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
You need to make sure that string
is a valid type in the .h file.
- Add
#include <string>
. - Use
std::string
instead of juststring
.
#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
answered Jan 2 at 6:37
R SahuR Sahu
168k1292191
168k1292191
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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