C++ help
hai sir
i have a doubt about accessing the class members using the obects i declare a char * str; inside a class (public section) then in main() function i create a object and access the variable using dot operator but it will not the code of this is #include<iostream> #include<string> using namespace std; class check1 { public: char *str; }; int main() { check1 c; cout<<"enter the string\n"; cin>>c.str; cout<<c.str; return 0; } |
Why is it not working? I see nothing wrong with the code. Are you compiling it right? How are you trying to compile?
|
You should allocate memory for str before use
|
That isn't the problem. The memory has already been allocated, it is just setting that pointer. As X said, it might be a compile problem. It should compile by doing
Code:
g++ -o exp exp.c++ |
im not a c++ person, i know java.. but do you not need to define a constructor for the check1 class? what is called?
|
Ok, I just did a copy and paste in my Windows machine and it compiled without a problem with borland, so I think it's some error when compiling. Try tuxdev suggestion to compile the program ;)
@ nadroj Quote:
|
ok.. so youd only need one if u were to initialize variables or do some startup stuff right?
thanks |
Pretty much, yeah. The default constructor does nothing, apart from create a new object. If you supply any constructor, the compiler doesn't give you the default no-arg one. If you need that one too, you need to add it yourself.
|
What's the probelm?
I think that it woudl be easier if sajith had said what was wrong. As far as I can see there are no syntax erros and so yes it will compile but as freegianghu said c.str has not been allocated any memory so the code will not run successfully.
All the member variable str is, is a pointer to a character. What is its value (i.e. locatyion in memory)? presumably it is meant to be pointing to an valid area of memory such as a string or character array. But none has been set up. A constructor could be used but I'd recommend that the data type is changed to being an String object. graeme. |
It isn't Java, so there is no String class. The memory has already been allocated by the insertion operator or something underneath that, so it is unnecessary to allocate it yourself. We haven't heard back from sajith yet...
|
I wasn't thinking of Java, I was thinking of the STL (viz #include<string>) in the original code.
|
then say string, not String. std::strings have nice features, but char * are simpler, so there is a advantage to using those instead.
|
yea, you have to allocate memory for the char pointer, otherwise there is no telling what you will get. if you change it to a string, you won't have a problem. their complexity is not too great.
|
The >> operator
The easiest way to think about the >> operator is to consider it as a function which expects two arguments, so
Code:
cin>>c.str; Code:
>>(cin,c.str); In C++ operators are just special functions and the code for the example given above would actually be: Code:
operator>>(cin,c.str); |
i think this works but you have to use a character array and it will only save up to the first blank
Code:
#include<iostream> peace out |
All times are GMT -5. The time now is 03:38 PM. |