LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ help (https://www.linuxquestions.org/questions/programming-9/c-help-391893/)

sajith 12-12-2005 09:09 AM

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

Mega Man X 12-12-2005 09:38 AM

Why is it not working? I see nothing wrong with the code. Are you compiling it right? How are you trying to compile?

freegianghu 12-12-2005 09:54 AM

You should allocate memory for str before use

tuxdev 12-12-2005 12:49 PM

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++
Oh, and use [code] in the future to make your code more readable.

nadroj 12-12-2005 12:57 PM

im not a c++ person, i know java.. but do you not need to define a constructor for the check1 class? what is called?

Mega Man X 12-12-2005 01:23 PM

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:

im not a c++ person, i know java.. but do you not need to define a constructor for the check1 class? what is called?
I don't think you need a constructor here. Just like in Java, if you don't explicitly create a constructor, a default one(no-arg) is provided. Since the class variables are also public, you can access them directly without any problems, from any class, I guess (I'm also a Java person, ghehe).

nadroj 12-12-2005 01:41 PM

ok.. so youd only need one if u were to initialize variables or do some startup stuff right?
thanks

Nylex 12-12-2005 01:44 PM

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.

graemef 12-12-2005 06:03 PM

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.

tuxdev 12-12-2005 06:24 PM

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...

graemef 12-12-2005 06:36 PM

I wasn't thinking of Java, I was thinking of the STL (viz #include<string>) in the original code.

tuxdev 12-12-2005 07:07 PM

then say string, not String. std::strings have nice features, but char * are simpler, so there is a advantage to using those instead.

sirclif 12-12-2005 07:45 PM

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.

graemef 12-12-2005 08:05 PM

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;
can be rewritten as
Code:

>>(cin,c.str);
c.str is an output parameter and so it must have the memory allocated before it is called. As would be the case for any C/C++ function.



In C++ operators are just special functions and the code for the example given above would actually be:
Code:

operator>>(cin,c.str);

kike_coello 12-12-2005 09:08 PM

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>
#include<string>
using namespace std;

class check1
{
public:
        char str[10];
};

int main()
{
        check1 c;

        cout<<"enter the string\n";
        cin>>c.str;

        cout<<c.str;

        return 0;
}

i don't know how this code thing works so sorry if it comes out all *ucked up

peace out


All times are GMT -5. The time now is 03:38 PM.