string object initialization

C

Christian Meier

Baloff said:
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);

These statements do not initialize, I think. As your error messages say,
operators/functions of string/int are called with these three statements.
cout << "employee e1\n\t" << e1.firstname << "\n\t"
<< e1.lastname << "\n\t" << e1.age << endl;


cout << "employee e2\n\t" << e2.firstname << "\n\t"
<< e2.lastname << "\n\t" << 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
 
B

Baloff

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 e1\n\t" << e1.firstname << "\n\t"
<< e1.lastname << "\n\t" << e1.age << endl;


cout << "employee e2\n\t" << e2.firstname << "\n\t"
<< e2.lastname << "\n\t" << 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
 
U

upashu2

e2.firstname ("sam2");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.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.
};
 
U

upashu2

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);
 
B

Baloff

upashu2 said:
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
 
U

upashu2

to have a constructorYou cann't do e1.firstname("......"); read my prevoius post.post #3
What u can do is employees e1("......"); see my post #4.Using iniatialization list in constructor call the copy constructor of
object (here for strings) .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.
 
J

Jakob Bieling

int main(){
employees e1, e2;

At this point, all members of e1 and e2 have been initialized.
e1.firstname = "sam";
e1.lastname = "Jesse";
e1.age=11;

All of the above are assignments.
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
 
K

Karl Heinz Buchegger

Baloff said:
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?
 
P

Peter Julian

Baloff said:
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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top