Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 3rd, 2005, 7:54 PM   #1
omdown
Newbie
 
Join Date: Oct 2005
Posts: 3
Rep Power: 0 omdown is on a distinguished road
Problem assigning a string to a class object.

So I'm trying to teach myself C++, but I've stumbled across a programming exercise that I can't figure out. The general idea is this: create a class called Dog which has a string field for the dog's name. In main(), create a dog and assign a name to it. The problem is, it doesn't seem to work the same way as assigning a string in a normal fashion. The way I've been going about it is:

       // this is the class
       class Dog
             {
             public:
	          char dogName[5];
              };

      // then skipping down to the main () section where I assign a name

	Dog oneDog;
	oneDog.dogName = {"Spot"};

Which produces errors indicating that the { } should not be there. When I remove the braces:

	Dog oneDog;
	oneDog.dogName = "Spot";

It gives me the error that "left operand must be l-value." How can I assign a string value to this? I'm also a little confused as to how you would pass an object's value to a function as well. The book I bought has a bad habit of going over how you would do something in an ideal situation but not if it's anything more complicated. Would it be the same as a regular variable like this:

function_name(oneDog.dogName)

Thanks in advance.
omdown is offline   Reply With Quote
Old Oct 3rd, 2005, 8:25 PM   #2
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
If you want the dog just to have a reference to the name and not take ownership of it ( not be able to change it ) do this:

class Dog
{
public:
	const char * Name;
};

Dog oneDog;
oneDog.Name = "Spot";
function( oneDog.Name );

void function( const char * name )
{
}

If you want the dog to own the name, so it can change it's name later do this:

#include <cstring>

class Dog
{
public:
	char Name[5];
};

Dog oneDog;
std::strncpy( oneDog.Name, "Dog", 5 );
function( oneDog.Name );

void function( char * name )		//if you want to change it
{
}
void function( const char * name )	//if you dont want to change it
{
}

There is an easier route however, as you are using C++ you can take advantage of the string class defined in the standard library. This is, by far, the prefered option:

#include <string>

class Dog
{
public:
	std::string Name;
};

Dog oneDog;
oneDog.Name = "Spot";
function( oneDog.Name );

void function( string & name )		//take a reference you can change
{
}
void function( const string & name )	//take a reference you can't change
{
}
void function( string name )			//create a copy of the name (slowest method)
{
}

The string class makes string operations much easier e.g. defining the == opertor to test if two strings are equal. See here for more examples.

edit: added in functions.

Last edited by Animatronic; Oct 3rd, 2005 at 8:36 PM.
Animatronic is offline   Reply With Quote
Old Oct 3rd, 2005, 9:10 PM   #3
omdown
Newbie
 
Join Date: Oct 2005
Posts: 3
Rep Power: 0 omdown is on a distinguished road
Wow, thanks man, it would be nice if this book listed that you had to strcpy to move the info in. Sometimes I think I should write to the authors and bring up such things.
omdown is offline   Reply With Quote
Old Oct 4th, 2005, 12:48 AM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
minimal working code:

#include <string>
    class Dog
    {
	  public:
		  std::string dogName;
    };

	int main()
	{
	    Dog oneDog;
	    oneDog.dogName = "pascal";
	    return 0;
	}
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC