string class question..

J

JustSomeGuy

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'
 
C

Cy Edmunds

JustSomeGuy said:
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"));
 
J

Justin Dubs

Cy Edmunds said:
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
 
R

Ron Natalie

JustSomeGuy said:
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
 
C

Cy Edmunds

Justin Dubs said:
"Cy Edmunds" <[email protected]> wrote in message
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?


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.
 
C

Cy Edmunds

Ron Natalie said:
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
 
R

Ron Natalie

Cy Edmunds said:
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)
 
C

Cy Edmunds

Ron Natalie said:
\]
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
 
J

Justin Dubs

Cy Edmunds said:
Justin Dubs said:
"Cy Edmunds" <[email protected]> wrote in message
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.
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.
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.
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.
They are always issues.

God, I wish you were joking. Again I will bring out the hammer/nail
analogy.
LOL


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.
Probably.

That's a shame.
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
 
C

Cy Edmunds

[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.
 
B

Big Brian

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.
 
O

Old Wolf

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 }
};
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top