C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string object initialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Christian Meier
Guest





PostPosted: Thu Jul 28, 2005 6:40 am    Post subject: Re: string object initialization Reply with quote




"Baloff" <washdc (AT) wash (DOT) edu> schrieb im Newsbeitrag
news:874qaf2zit.fsf (AT) wash (DOT) edu...
Quote:

Hello
I am not sure why my compiler will not initialize
string e1("sam");
and will initialize
string e1 = "sam";

here is my code and the error.

thanks alot

********************code********************
#include #include
using namespace std;

typedef struct
{
string firstname;
string lastname;
int age;
}
employees;

int main(){
employees e1, e2;
e1.firstname = "sam";
e1.lastname = "Jesse";
e1.age=11;

e2.firstname ("sam2");
e2.lastname("Jesse2");
e2.age(22);

These statements do not initialize, I think. As your error messages say,
operators/functions of string/int are called with these three statements.

Quote:

cout << "employee e1nt" << e1.firstname << "nt"
e1.lastname << "nt" << e1.age << endl;


cout << "employee e2nt" << e2.firstname << "nt"
e2.lastname << "nt" << e2.age << endl;
}


********************Error********************
cd /home/sam/Exercies/ThinkingInC++/Vol1/C03/15/
make -k
g++ -g -c -o main.o main.cpp
main.cpp: In function `int main()':
main.cpp:20: error: no match for call to `(std::string) (const char[5])'
main.cpp:21: error: no match for call to `(std::string) (const char[7])'
main.cpp:22: error: call to non-function `employees::age'
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.

Compilation exited abnormally with code 2 at Thu Jul 28 16:46:34



Back to top
Baloff
Guest





PostPosted: Thu Jul 28, 2005 6:52 am    Post subject: string object initialization Reply with quote




Hello
I am not sure why my compiler will not initialize
string e1("sam");
and will initialize
string e1 = "sam";

here is my code and the error.

thanks alot

********************code********************
#include <iostream>
#include <string>

using namespace std;

typedef struct
{
string firstname;
string lastname;
int age;
}
employees;

int main(){
employees e1, e2;
e1.firstname = "sam";
e1.lastname = "Jesse";
e1.age=11;

e2.firstname ("sam2");
e2.lastname("Jesse2");
e2.age(22);

cout << "employee e1nt" << e1.firstname << "nt"
<< e1.lastname << "nt" << e1.age << endl;


cout << "employee e2nt" << e2.firstname << "nt"
<< e2.lastname << "nt" << e2.age << endl;
}


********************Error********************
cd /home/sam/Exercies/ThinkingInC++/Vol1/C03/15/
make -k
g++ -g -c -o main.o main.cpp
main.cpp: In function `int main()':
main.cpp:20: error: no match for call to `(std::string) (const char[5])'
main.cpp:21: error: no match for call to `(std::string) (const char[7])'
main.cpp:22: error: call to non-function `employees::age'
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.

Compilation exited abnormally with code 2 at Thu Jul 28 16:46:34
Back to top
upashu2
Guest





PostPosted: Thu Jul 28, 2005 7:18 am    Post subject: Re: string object initialization Reply with quote



Quote:
e2.firstname ("sam2");
e2.lastname("Jesse2");
It is not a string initialization, it is calling a function firstname()

with parameter "sam2" and no such function exists in struct employees.
In C++, You can't call consturctor in this way.
Quote:
string e1("sam");
It is perfectly fine. It is constructing the object by calling

constructor. But in object e2 , string is already constructed when u
write employees e2; with default constructor of string. and you are
thinking again calling a different constructor at same object.

In this case How will compiler differentiate whether u r calling a
function or constructor?So simply, It is not allowed in C++.

to call constructor of memeber objects, use initialization list of
your container. For example:
struct employees
{
string firstname;
string lastname;
int age;
employess() : firstname("Upashu2"),lastname("Balooff"),age(22) {
//////
} ////defining default constructor of
employees which call constructors
///////////////////////////////////////of member objects to initialize
them.
};


Back to top
upashu2
Guest





PostPosted: Thu Jul 28, 2005 7:27 am    Post subject: Re: string object initialization Reply with quote

Also,
struct employees
{
string firstname;
string lastname;
int age;
employess() : firstname("Upashu2"),lastname("Balooff"),age(22) { }
///default construcot
employess(char* f,char* l, int a) : firstname(f),lastname(l),age(a)
{
//////
} //defining overloaded consturcotr
};

employess e2("jammes2", "jasse2",22);

Back to top
Baloff
Guest





PostPosted: Thu Jul 28, 2005 9:47 am    Post subject: Re: string object initialization Reply with quote

"upashu2" <upashu1 (AT) rediffmail (DOT) com> writes:

Quote:
Also,
struct employees
{
string firstname;
string lastname;
int age;
employess() : firstname("Upashu2"),lastname("Balooff"),age(22) { }
///default construcot
employess(char* f,char* l, int a) : firstname(f),lastname(l),age(a)
{
//////
} //defining overloaded consturcotr
};

employess e2("jammes2", "jasse2",22);

now, what is more effecent, to have a constructor
inside the struct and do e1.firstname("some-name") or use the assigning
operator (=) with no constructor in the struct?

thanks

Back to top
upashu2
Guest





PostPosted: Thu Jul 28, 2005 10:25 am    Post subject: Re: string object initialization Reply with quote

Quote:
to have a constructor
inside the struct and do e1.firstname("some-name")
You cann't do e1.firstname("......"); read my prevoius post.post #3

What u can do is employees e1("......"); see my post #4.
Quote:
employess(char* f,char* l, int a) : firstname(f),lastname(l),age(a) {}
Using iniatialization list in constructor call the copy constructor of

object (here for strings) .
Quote:
employess(char* f,char* l, int a) { firstname = f; lastname =l; age =a;}
OR
employees e1; e1.firstname = "....."
This results first constructing the string object (firstname, lastname)

with their default constructor. after that when u assign the value
using "=" , it calls overladed assignment operator to copy the value
from rightside object to leftside object. it will take Two steps to
initialize the object.

In your case , if u don't want to define the constructor, simply use
employees e1; e1.firstname ="...."; Don't think to write
e1.firstname(....) even by mistake , It is syntax of function call,
not initalization, you will never found such statements in C++ anywhere.


Back to top
Jakob Bieling
Guest





PostPosted: Thu Jul 28, 2005 10:32 am    Post subject: Re: string object initialization Reply with quote

"Baloff" <washdc (AT) wash (DOT) edu> wrote


Quote:
int main(){
employees e1, e2;

At this point, all members of e1 and e2 have been initialized.

Quote:
e1.firstname = "sam";
e1.lastname = "Jesse";
e1.age=11;

All of the above are assignments.

Quote:
e2.firstname ("sam2");
e2.lastname("Jesse2");
e2.age(22);

Tho you think you are initializing, the objects are already
initialized above. You cannot initialize twice. That is why this syntax
is interpreted as a function call.

To answer your other question in a follow-up post: It is better to
have a constructor inside the struct, which initializes the strings
using the initializer list. The compiler might optimize your code above,
so it is just as fast, so the constructor with initializer-list might
not be faster with all compilers, but it will always be at least as fast
as the method above.

hth
--
jb

(reply address in rot13, unscramble first)



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Thu Jul 28, 2005 11:56 am    Post subject: Re: string object initialization Reply with quote

Baloff wrote:
Quote:

"upashu2" <upashu1 (AT) rediffmail (DOT) com> writes:

Also,
struct employees
{
string firstname;
string lastname;
int age;
employess() : firstname("Upashu2"),lastname("Balooff"),age(22) { }
///default construcot
employess(char* f,char* l, int a) : firstname(f),lastname(l),age(a)
{
//////
} //defining overloaded consturcotr
};

employess e2("jammes2", "jasse2",22);

now, what is more effecent, to have a constructor
inside the struct and do e1.firstname("some-name") or use the assigning
operator (=) with no constructor in the struct?


In general questions about efficiency can aonly be answered with:
you have to try it on your specific platform.

But in this case, think about the following.

If you do (in a version with no constructor in the struct)

employess e1;
e1.firstname = "jammes2";
e1.lastname = "jasse2";
e1.age = 22;

What is going on?
Well. First of all the e1 object comes into existence. For this the members
get initialized. In this specific case this means:
* firstname initializes to an empty string
* lastname initializes to an empty string
* age does nothing, since it is a builtin type
it contains a random value

Then the assignments are performed, which do
* change firstname from an empty string to "jammes2"
* change lastname from an empty string to "jasse2"
* age changes from some unspeficied random value to 22

Now contrast this with the version using a constructor in the struct.
When you do:
empoyess e2( "jammes2", "jasse2",22);
what is going on.

Well. First of all the e2 object comes into existence. For this the members
get intialized. In this specfic case this means:
* firstname instializes to "jammes2"
* lastname initializes to "jasse2"
* age initializes to 22

and that's it. Same end result.

Now compare both variants and answer the question:
Which of those 2 variants is likely to be more efficient
then the other?

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Peter Julian
Guest





PostPosted: Thu Jul 28, 2005 2:50 pm    Post subject: Re: string object initialization Reply with quote


"Baloff" <washdc (AT) wash (DOT) edu> wrote

Quote:

Hello
I am not sure why my compiler will not initialize
string e1("sam");
and will initialize
string e1 = "sam";

You are not requesting an initialization with e1.firstname("sam2") above.
That is, until you provide the ctor that will carry out such initialization.
Since you haven't provided a ctor with parameters, the compiler is looking
for a function that matches that signature.

However, you can create a class that initializes its members through
parameters in the ctor. Note the initialization lists.

<snip>

#include <string>

class Employees
{
std::string firstname;
std::string lastname;
int age;
public:
Employees()
: firstname("unknown"), lastname("unknown"), age(0) { }
Employees(std::string fn, std::string ln, int n)
: firstname(fn), lastname(ln), age(n) { }
~Employees() { }

};

int main()
{
Employees e1("George", "Smith", 30);
}



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.