By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

string class question..

P: n/a
I have a structure

typedef struct
{
string a;
string b;
} atype;

string ABC = ("123");
string DEF = ("456");
atype mylist[] = {
"ABC", ABC
"DEF", DEF
};

Works well in gcc but mylist fails to compile in vc++.
Error message is something like....
structs.h(171) : error C2440: 'initializing' : cannot convert from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' to 'atype'
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
I have a structure

typedef struct
{
string a;
string b;
} atype;
Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to
use a class in C++.

string ABC = ("123");
Why the parentheses? ITYM

string ABC = "123";
string DEF = ("456");
atype mylist[] = {
"ABC", ABC
Is there really a comma after this line?
"DEF", DEF
};

Works well in gcc
It does?
but mylist fails to compile in vc++.
Those miscreants from Redmond at it again!
Error message is something like....
structs.h(171) : error C2440: 'initializing' : cannot convert from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' to 'atype'


I don't know why this code works with any compiler. A C++ solution would
look like this:

#include <string>

class atype
{
private:
std::string m_a;
std::string m_b;
public:
atype() {}
atype(const std::string &i_a, const std::string &i_b) :
m_a(i_a), m_b(i_b) {}
const std::string &a() const {return m_a;}
const std::string &b() const {return m_b;}
};

atype mylist[] = {atype("ABC", "123"), atype("DEF", "456")};

or better yet

#include <vector>

std::vector<atype> avec;
avec.push_back(atype("ABC", "123"));
avec.push_back(atype("DEF", "456"));

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

P: n/a
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<op********************@twister.nyroc.rr.com> ...
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
I have a structure

typedef struct
{
string a;
string b;
} atype;
Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to
use a class in C++.


Why is a class better? There is no difference between a struct and a
class in C++ aside from the default access specifier being private for
classes and public for structs.
I don't know why this code works with any compiler. A C++ solution would
look like this:

[ snip the monstrosity ]
Why the heck do you think that's a better way to put to strings
together? All that overhead with private data members and two
constructors and getters when a plain struct would work just fine.
Not to mention that you didn't provide setters and your only getter
uses const references so your class doesn't even provide the same
functionality as his struct because you can't modify the a and b in
your version.

struct atype {
std::string a;
std::string b;

atype(const std::string &a, const std::string &b) : a(a), b(b) { }
};

Depending on the usage, this little struct may be just perfect.
There's no reason to make it a class with private data members unless
encapsulation or data hiding is an issue. And god help you if you
make them private AND then include getters and setters for them.
or better yet

#include <vector>

std::vector<atype> avec;
avec.push_back(atype("ABC", "123"));
avec.push_back(atype("DEF", "456"));


Why is this "better yet?" Yeah, the vector is resizeable. It also
has overhead both in memory and speed. What if he didn't need to
resize it. Is your way still better then?

There's a time and a place for super-dooper OO patterns, but this may
not necessarily be it.

Justin Dubs
Jul 22 '05 #3

P: n/a

"JustSomeGuy" <no**@nottelling.com> wrote in message news:br***********@nserve1.acs.ucalgary.ca...
Works well in gcc but mylist fails to compile in vc++.
Error message is something like....
structs.h(171) : error C2440: 'initializing' : cannot convert from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' to 'atype'


Works fine for me provided you define string and you insert hte comma missing
at the end of the line
"ABC", ABC

Jul 22 '05 #4

P: n/a
"Justin Dubs" <jt****@eos.ncsu.edu> wrote in message
news:2e**************************@posting.google.c om...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<op********************@twister.nyroc.rr.com> ...
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
I have a structure

typedef struct
{
string a;
string b;
} atype;


Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to use a class in C++.


Why is a class better? There is no difference between a struct and a
class in C++ aside from the default access specifier being private for
classes and public for structs.
I don't know why this code works with any compiler. A C++ solution would
look like this:

[ snip the monstrosity ]


Why the heck do you think that's a better way to put to strings
together? All that overhead


"Overhead" is a term I hear a lot. Show me your timing or memory usage
studies and I will take due note. However, maintainability is often more
important than raw performance, so faster and/or smaller isn't necessarily
better anyway.
with private data members and two
constructors and getters when a plain struct would work just fine.
I suspect you do not understand the basics of object oriented programming if
you think public data is OK.
Not to mention that you didn't provide setters and your only getter
uses const references so your class doesn't even provide the same
functionality as his struct because you can't modify the a and b in
your version.
Yes, the structure has more "functionality" in that anybody is free to
modify the contents directly. This is of course a disadvantage.

struct atype {
std::string a;
std::string b;

atype(const std::string &a, const std::string &b) : a(a), b(b) { }
};

Depending on the usage, this little struct may be just perfect.
Yes, maybe.
There's no reason to make it a class with private data members unless
encapsulation or data hiding is an issue.
They are always issues.
And god help you if you
make them private AND then include getters and setters for them.
Huh?
or better yet

#include <vector>

std::vector<atype> avec;
avec.push_back(atype("ABC", "123"));
avec.push_back(atype("DEF", "456"));
Why is this "better yet?" Yeah, the vector is resizeable. It also
has overhead both in memory and speed.


Ah yes, once again optimizing for performance without any testing or
consideration of requirements.
What if he didn't need to
resize it. Is your way still better then?
Probably.

There's a time and a place for super-dooper OO patterns, but this may
not necessarily be it.
Keep in mind that this was posted in comp.lang.C++. I have no issue with
people who prefer to program in C. But if you want to program in C++ I think
you should do it right.

Justin Dubs


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #5

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"JustSomeGuy" <no**@nottelling.com> wrote in message news:br***********@nserve1.acs.ucalgary.ca...
Works well in gcc but mylist fails to compile in vc++.
Error message is something like....
structs.h(171) : error C2440: 'initializing' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' to 'atype'
Works fine for me provided you define string and you insert hte comma

missing at the end of the line
"ABC", ABC


Interesting. The following:

#include <string>
using namespace std;

typedef struct
{
string a;
string b;
} atype;

int main()
{
string ABC = ("123");
string DEF = ("456");
atype mylist[] = {
"ABC", ABC, // error refer to this line
"DEF", DEF
};
return 0;
}

did not compile with VC.net. The error messages were:

error C2440: 'initializing' : cannot convert from 'char [4]' to 'atype'

and

error C2689: Initializing 'mylist' : this form of non-aggregate
initialization requires a unary constructor
'atype' has a non-aggregate data member 'atype::a' : Types with a
non-aggregate data member are not aggregate

--
Cy
http://home.rochester.rr.com/cyhome/

Jul 22 '05 #6

P: n/a

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:yt***************@twister.nyroc.rr.com...
\]
error C2689: Initializing 'mylist' : this form of non-aggregate
initialization requires a unary constructor
'atype' has a non-aggregate data member 'atype::a' : Types with a
non-aggregate data member are not aggregate


The compiler is demonstrating it's stupidity.

Types with non-aggregate data members can be aggregates.

The rqeuirement is that:
1. It is an array
-or-
2. It is a class with
no user-declared constructor
no private or protected data members
no base classes
and no virtual functions.
atype is an aggregate (rule #2)
mylist is an aggregate(rule #1)
Jul 22 '05 #7

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:yt***************@twister.nyroc.rr.com... \]
error C2689: Initializing 'mylist' : this form of non-aggregate
initialization requires a unary constructor
'atype' has a non-aggregate data member 'atype::a' : Types with a non-aggregate data member are not aggregate
The compiler is demonstrating it's stupidity.


roflmao Think I paid too much? hehe

Types with non-aggregate data members can be aggregates.

The rqeuirement is that:
1. It is an array
-or-
2. It is a class with
no user-declared constructor
no private or protected data members
no base classes
and no virtual functions.
atype is an aggregate (rule #2)
mylist is an aggregate(rule #1)

Jul 22 '05 #8

P: n/a
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<lm***************@twister.nyroc.rr.com>...
"Justin Dubs" <jt****@eos.ncsu.edu> wrote in message
news:2e**************************@posting.google.c om...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message

news:<op********************@twister.nyroc.rr.com> ...
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
> I have a structure
>
> typedef struct
> {
> string a;
> string b;
> } atype;

Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to use a class in C++.


Why is a class better? There is no difference between a struct and a
class in C++ aside from the default access specifier being private for
classes and public for structs.
I don't know why this code works with any compiler. A C++ solution would
look like this:

[ snip the monstrosity ]


Why the heck do you think that's a better way to put to strings
together? All that overhead


"Overhead" is a term I hear a lot. Show me your timing or memory usage
studies and I will take due note. However, maintainability is often more
important than raw performance, so faster and/or smaller isn't necessarily
better anyway.


I didn't necessarily mean timing or memory overhead. I may have meant
person-time overhead. The amount of time I waste typing all that crap
when all I needed was a struct.
with private data members and two
constructors and getters when a plain struct would work just fine.


I suspect you do not understand the basics of object oriented programming if
you think public data is OK.


I recommend you do some more reading on OO theory before you say such
things. The definition of OO itself is hotly debated. Many of the
acceptable definitions to not even include data hiding. Even the ones
that do usually do not tout it as the be-all end-all of programming.
Like all programming techniques, data hiding has it's uses. However,
it is not the solution to all problems. You are a hammer, but not all
problems are nails.
Not to mention that you didn't provide setters and your only getter
uses const references so your class doesn't even provide the same
functionality as his struct because you can't modify the a and b in
your version.


Yes, the structure has more "functionality" that anybody is free to
modify the contents directly. This is of course a disadvantage.


This is trivially false. It CAN be a disadvantage, in some programs,
some of the time. Not in all programs, all of the time. This is
especially true of throw-away programs that are designed to conserve
programmer-time as there are likely no future maintainability issues.

struct atype {
std::string a;
std::string b;

atype(const std::string &a, const std::string &b) : a(a), b(b) { }
};

Depending on the usage, this little struct may be just perfect.


Yes, maybe.


Now you contradict yourself. A few sentences ago you said that public
data members are always bad. In your next sentence you will state
that data hiding is always an issue. Yet here you state that this
struct may be acceptable.
There's no reason to make it a class with private data members unless
encapsulation or data hiding is an issue.


They are always issues.


God, I wish you were joking. Again I will bring out the hammer/nail
analogy.
And god help you if you
make them private AND then include getters and setters for them.


Huh?


LOL
or better yet

#include <vector>

std::vector<atype> avec;
avec.push_back(atype("ABC", "123"));
avec.push_back(atype("DEF", "456"));


Why is this "better yet?" Yeah, the vector is resizeable. It also
has overhead both in memory and speed.


Ah yes, once again optimizing for performance without any testing or
consideration of requirements.


What requirements are those? I don't recall the OP stating that this
code was for a production system that would definitly need future
expantion capabilities. It may just be a throw-away. Either way,
vector's aren't always the answer and it is naive to say otherwise.
What if he didn't need to
resize it. Is your way still better then?


Probably.


That's a shame.
There's a time and a place for super-dooper OO patterns, but this may
not necessarily be it.


Keep in mind that this was posted in comp.lang.C++. I have no issue with
people who prefer to program in C. But if you want to program in C++ I think
you should do it right.


C++. You keep using that word. I do not think it means what you
think it means.

C++ is a multi-paradigm language. Yes, it does support OO, for
various definitions of OO. It also supports procedural programming.
It also supports label/goto-based spaghetti code. None of these are
"right" or "wrong." To say that using data hiding and classes is the
only "right" way to do C++ is rather sad.

Justin Dubs
Jul 22 '05 #9

P: n/a
[snip]

Whatever. But if you want to post on a C++ newsgroup such pearls of wisdom
that public data members provides extra functionality, you are so far from
the position of the C++ community that you should expect some heat.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #10

P: n/a
> vector's aren't always the answer and it is naive to say otherwise.

THANK YOU! It seems that everybody on this newsgroup IMEDIATELY says
to use std::vector.
C++ is a multi-paradigm language. Yes, it does support OO, for
various definitions of OO. It also supports procedural programming.
It also supports label/goto-based spaghetti code. None of these are
"right" or "wrong." To say that using data hiding and classes is the
only "right" way to do C++ is rather sad.


From my personal experience, people that say that there's only one
"right" way to do something usually say it for their own (hidden)
agenda which usually has nothing to do with writting code.
Jul 22 '05 #11

P: n/a
> typedef struct
{
string a;
string b;
} atype;

atype mylist[] = {
"ABC", ABC
"DEF", DEF
};

structs.h(171) : error C2440: 'initializing' : cannot convert from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' to 'atype'


Try:
atype mylist[] = {
{ "ABC", ABC },
{ "DEF", DEF }
};
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.