hi everyone
i've got a question about the variable declaration in the class
why i cannot declare a variable of type string member inside the class definition?
my compiler always show error message stating that there is no such type 'string'
is there any other ways to declare a variable of type string inside the class??
class test
{
string x;
public:
void print();
test();
};
void test::print()
{
std::cout << x << std::endl;
}
test::test()
{
x = "hi";
}
in the .cpp file
1 2 3 4 5 6 7 8
#include<iostream>
#include"test.h"
int main()
{
test t;
t.print();
}
sorry for the late reply, recently busy with my test, now finished already
now back to the problem
the compiler i used is code block
and the problem is that everytime i compile the program, my compiler ends up with giving errors that states no such type as 'string'
is there something wrong with the program??
#include <string>//<-- here's your problem, you forgot to include it
class test
{
string x;
public:
void print();
test();
};
void test::print()
{
std::cout << x << std::endl;
}
test::test()
{
x = "hi";
}
#include<iostream>
//then you can also use using
usingnamespace std;
class test
{
string x;
public:
void print();
test();
};
void test::print()
{
cout << x << endl;
}
test::test()
{
x = "hi";
}
sorry denis
i didn't knew that string need to be writen as std::string
just now i've tried to include usingnamespace std; in the header file
now it can compile correctly
Please learn C++, and it's up to you to include 'using' in header file or not.
But please, trust me it bad idea. If you want to know why please use Google with query
why using namespace std; should not be included in the header file??
actually i am going to solve my assignment using object-oriented programming
so i am trying to write a short header file so that i can understand more about it
then i came to the problem with the string variable
but thanks to denis and olredixsis, finally i know where is the mistake i've made
that is i use string instead of std::string