473,226 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

string class question..

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
11 2199
"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
"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

"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
"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
"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

"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
"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
"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
[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
> 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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag,...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: vbdotnetmania | last post by:
Hi, I'm trying to port java code to c#, I am not too familiar with either so forgive me for what might be a stupid question, I wish to use the java string type rather than the c# one, when I try to...
7
by: Ioannis Vranos | last post by:
I had reported this as a bug: Description: Default indexed property of System::String crashes for object with stack semantics. int main()
2
by: memosa | last post by:
Hi, friends: This is my programme (work on Microsoft visual c++) and I have two question . First, I want to say that there is no error but I need a little help. This is a small programme...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
5
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.