![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|