473,385 Members | 1,453 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,385 software developers and data experts.

But I have to use a struct!

Pep
I have this massive legacy application to support and although it is
C++, it has been mostly written like a C application, making extensive
uses of structs instead of classes, which are recorded for persistence
in a database as a straight binary write.

So in order to supply a better way of handling a complex data field in
one of the structs, I have created another struct which is included
inside the original struct and things are working fine.

However my problem is that the new struct needs to have a member that
takes a parameter of the same struct and at that point C++ is
complaining.

The struct looks like this

typedef struct
{
private:
int field1;
int field2;
public:
setField1(int foo)
{
field1 = foo;
}

setField2(int foo)
{
field1 = foo;
}

setFields(myStruct& foo)
{
setField1(foo.field1);
setField2(foo.field2);
}
} myStruct;

However g++ complains that I am using a undeclared type in the
setFields method.

I have tried declaring it as setFields(struct myStruct& foo) and have
tried a forward declaration of struct myStruct before the struct
definition but none of these approaches work.

I do not want to use a void* ptr in the setFields method, is there any
way to do what I want to do?

TIA,
Pep.

Jul 31 '06 #1
9 1169

Pep wrote:
<...>
I do not want to use a void* ptr in the setFields method, is there any
way to do what I want to do?
Try changing to use the form:

struct myStruct
{
private:
int field1;
int field2;
public:
//(Also C++ needs return types for functions)
void setField1(int foo)
/////////////////
{
field1 = foo;
}
void setField2(int foo)
{
field1 = foo;
}
void setFields(myStruct& foo)
{
setField1(foo.field1);
setField2(foo.field2);
}
} ;

regards
Andy Little

Jul 31 '06 #2
typedef struct
{
If this is C++ code, you can write it like this (same as that of class
in C++)

struct MyStruct
{
public:
void function(MyStruct& xyz)
{

}
};

Jul 31 '06 #3
"Pep" <pe**********@yahoo.co.ukwrote in message
news:11*********************@s13g2000cwa.googlegro ups.com...
: However my problem is that the new struct needs to have a member that
: takes a parameter of the same struct and at that point C++ is
: complaining.
:
: The struct looks like this
:
: typedef struct
: {
: private:
: int field1;
: int field2;
: public:
: setField1(int foo)
: {
: field1 = foo;
: }
:
: setField2(int foo)
: {
: field1 = foo;
: }
:
: setFields(myStruct& foo)
: {
: setField1(foo.field1);
: setField2(foo.field2);
: }
: } myStruct;
:
: However g++ complains that I am using a undeclared type in the
: setFields method.
When using the "typedef struct" trick in C, you often
also need to give a name to the struct itself.
For example:
typedef struct Foo_tag {
//....
struct Foo_tag* next;
} Foo;
Or use a the following correct forward-declaration:
typedef struct Foo_tag Foo;
struct Foo_tag {
//....
Foo* next;
};

But since you are using C++-only constructs anyway,
why don't you just get rid of the "typedef struct"
trick, which is only useful in C ?

Write:
struct myStruct { .... setFields(myStruct& foo) ... };
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Jul 31 '06 #4
Pep wrote:
I have this massive legacy application to support and although it is
C++, it has been mostly written like a C application, making extensive
uses of structs instead of classes, which are recorded for persistence
in a database as a straight binary write.

So in order to supply a better way of handling a complex data field in
one of the structs, I have created another struct which is included
inside the original struct and things are working fine.
Be careful that you do not inadvertently make your struct into a non-POD
type, as that could break the "binary write" to your database. In
particular, avoid adding a copy assignment operator or a destructor.

--
Alan Johnson
Jul 31 '06 #5
Pep

kwikius wrote:
Pep wrote:
<...>
I do not want to use a void* ptr in the setFields method, is there any
way to do what I want to do?

Try changing to use the form:

struct myStruct
{
private:
int field1;
int field2;
public:
//(Also C++ needs return types for functions)
void setField1(int foo)
/////////////////
{
field1 = foo;
}
void setField2(int foo)
{
field1 = foo;
}
void setFields(myStruct& foo)
{
setField1(foo.field1);
setField2(foo.field2);
}
} ;

regards
Andy Little
Yep I missed the function type when I created the test class to show
the problem I am trying to resolve :(

As for the struct definition, of course it works fine when typedef is
removed. It's one of those obvious things that you miss when you have
to deal with such badly written code. Legacy is bad enough but gets
much worse when the 250,000 lines of code are badly written by a non
C++/C developer that used a how to code in 30 days book. Dotcom has a
lot to answer for :(

Jul 31 '06 #6
Pep

am******@yahoo.com wrote:
typedef struct
{

If this is C++ code, you can write it like this (same as that of class
in C++)

struct MyStruct
{
public:
void function(MyStruct& xyz)
{

}
};
Yep, removing the typedef worked fine, something I should have seen
myself but it's a case of "can't see the wood for the trees" ;)

Thanks,
Pep.

Jul 31 '06 #7
Pep

Ivan Vecerina wrote:
"Pep" <pe**********@yahoo.co.ukwrote in message
news:11*********************@s13g2000cwa.googlegro ups.com...
: However my problem is that the new struct needs to have a member that
: takes a parameter of the same struct and at that point C++ is
: complaining.
:
: The struct looks like this
:
: typedef struct
: {
: private:
: int field1;
: int field2;
: public:
: setField1(int foo)
: {
: field1 = foo;
: }
:
: setField2(int foo)
: {
: field1 = foo;
: }
:
: setFields(myStruct& foo)
: {
: setField1(foo.field1);
: setField2(foo.field2);
: }
: } myStruct;
:
: However g++ complains that I am using a undeclared type in the
: setFields method.
When using the "typedef struct" trick in C, you often
also need to give a name to the struct itself.
For example:
typedef struct Foo_tag {
//....
struct Foo_tag* next;
} Foo;
Or use a the following correct forward-declaration:
typedef struct Foo_tag Foo;
struct Foo_tag {
//....
Foo* next;
};

But since you are using C++-only constructs anyway,
why don't you just get rid of the "typedef struct"
trick, which is only useful in C ?

Write:
struct myStruct { .... setFields(myStruct& foo) ... };
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Thanks Ivan, of course removing the typedef works fine :)

Guess I need a holiday form this code and then come back and start all
over again. Would be nice to work on a clean code development just for
once.

Cheers,
Pep.

Jul 31 '06 #8
Pep

Alan Johnson wrote:
Pep wrote:
I have this massive legacy application to support and although it is
C++, it has been mostly written like a C application, making extensive
uses of structs instead of classes, which are recorded for persistence
in a database as a straight binary write.

So in order to supply a better way of handling a complex data field in
one of the structs, I have created another struct which is included
inside the original struct and things are working fine.

Be careful that you do not inadvertently make your struct into a non-POD
type, as that could break the "binary write" to your database. In
particular, avoid adding a copy assignment operator or a destructor.

--
Alan Johnson
Good point. I am not adding any ctors or dtors so that should resolve
the issue. It's basically a struct with fields and setter/getter
methods.

Cheers,
Pep.

Jul 31 '06 #9
In article <11*********************@s13g2000cwa.googlegroups. com>,
pe**********@yahoo.co.uk says...

[ ... ]
typedef struct
{
private:
int field1;
int field2;
public:
setField1(int foo)
{
field1 = foo;
}

setField2(int foo)
{
field1 = foo;
}

setFields(myStruct& foo)
{
setField1(foo.field1);
setField2(foo.field2);
}
} myStruct;
Your original question has been answered, but it leaves a much more
important one: what are you really trying to accomplish with this?
Right now, what you have is still almost equivalent to a struct in
which field1 and field2 are public. The only difference is that you
can't read their values. Right now, since they're private, and you
haven't declared any friends, and nothing in the struct uses their
values -- so you can set the values, but never make any use of them.

This looks a lot like a classic case of adding manipulator functions
that are intended to "help encapsulation" (or something on that
order), but so far, really accomplish nothing positive at all. Rather
the contrary, what you've got right now will make your code uglier
and harder to read, without providing anything useful in return.

Simply changing 'x.field1 = 1' to 'x.setField1(1)' isn't a good
thing. To accomplish something useful, you need to provide some more
intelligence. One of the basic ideas of any function is that code
that uses that function should be able to operate at a higher level
of abstraction than the code internal to the function. In this case,
you're doing more or less the reverse -- internally the function uses
a high-level assignment abstraction, but code that uses it is more
like a warped version of assembly language.

My advice would be to step back and explain to us what this struct is
really supposed to accomplish. If it really makes sense for the
client code to use functions like setField1, setField2 and setFields,
then you should probably just make field1 and field2 public and be
done with it. If there's a good reason for them to be internal and
protected, then you should implement those reasons into the code.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
5
by: anonymous | last post by:
I have a Lex file containing definitions of 2 structures like: %{ struct a {...}; struct b { struct a i; ... }; struct a x;
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
2
by: Maurice | last post by:
Hi, Is it legal to cast from void(*)(A*) to void(*)(B*)? Is it legal to cast from struct Derived{Base b; ...} to struct Base? I'm trying to get some inheritance and polymorphism in C and I...
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
9
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
10
by: Ben | last post by:
Hello, Is it possible to define a struct that has a type of itself in it? So is it possible and if so how to do what I'm trying to do here?: typedef struct mystruct { int a; mystruct *b;...
5
by: karthikbalaguru | last post by:
Hi, struct sky { int stars; struct ocean *oceanptr; }; struct ocean { int waves; struct sky *skyptr;
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.