LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2005, 09:09 AM   #1
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Rep: Reputation: 15
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;
}
 
Old 12-12-2005, 09:38 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Why is it not working? I see nothing wrong with the code. Are you compiling it right? How are you trying to compile?
 
Old 12-12-2005, 09:54 AM   #3
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
You should allocate memory for str before use
 
Old 12-12-2005, 12:49 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 12-12-2005, 12:57 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
im not a c++ person, i know java.. but do you not need to define a constructor for the check1 class? what is called?
 
Old 12-12-2005, 01:23 PM   #6
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
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.
 
Old 12-12-2005, 01:41 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
ok.. so youd only need one if u were to initialize variables or do some startup stuff right?
thanks
 
Old 12-12-2005, 01:44 PM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 12-12-2005, 06:03 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-12-2005, 06:24 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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...
 
Old 12-12-2005, 06:36 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I wasn't thinking of Java, I was thinking of the STL (viz #include<string>) in the original code.
 
Old 12-12-2005, 07:07 PM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
then say string, not String. std::strings have nice features, but char * are simpler, so there is a advantage to using those instead.
 
Old 12-12-2005, 07:45 PM   #13
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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.
 
Old 12-12-2005, 08:05 PM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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);
 
Old 12-12-2005, 09:08 PM   #15
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
Cool

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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration