ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
Last edited by Mega Man X; 12-12-2005 at 03:59 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.
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.