faq; struct and class another difference?



 DEVELOP > c-Plus-Plus > faq; struct and class another difference?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "fcvcnet"
Date: 31 Mar 2007 10:57:16 AM
Object: faq; struct and class another difference?
Hi,
I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
Jos¨¦e Lajoie, Barbara E. Moo
"If we define a class using the class keyword, then any members
defined before the first access label are implicitly private; ifwe
usethe struct keyword, then those members are public. Whether we define
a class using the class keyword or the struct keyword affects only the
default initial access level."
Now I defined a struct with overload operators == and != :
....
typedef struct RecordTwoSegmentNo
{
int firstsegmentno;
int secondsegmentno;
bool operator == (const RTSN& rmyrtsn);
bool operator != (const RTSN& rmyrtsn);
}RTSN;
bool RecordTwoSegmentNo::operator ==(const RTSN &rmyrtsn)
{
return (firstsegmentno==rmyrtsn.firstsegmentno &&
secondsegmentno==rmyrtsn.secondsegmentno);
}
bool RecordTwoSegmentNo::operator !=(const RTSN &rmyrtsn)
{
return !(*this==rmyrtsn);
}
....
The compiler (vs2005+winxp) report:
------ Build started: Project: maxborder, Configuration: Debug Win32 ------
Compiling...
maxborder.cpp
c:\maxborder\mypoint.h(16) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
c:\maxborder\mypoint.h(16) : error C2143: syntax error : missing ','
before '&'
c:\maxborder\mypoint.h(17) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
c:\maxborder\mypoint.h(17) : error C2143: syntax error : missing ','
before '&'
c:\maxborder\mypoint.h(21) : error C2511: 'bool
RecordTwoSegmentNo::operator ==(const RTSN &)' : overloaded member
function not found in 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(13) : see declaration of 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(27) : error C2511: 'bool
RecordTwoSegmentNo::operator !=(const RTSN &)' : overloaded member
function not found in 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(13) : see declaration of
'RecordTwoSegmentNo'
....
What is wrong? Is struct and class has another difference?
Thank you.
--
Thank you very much! :)
Thank this newsgroup very much! :)
Visual Studio 2005 Professional Edition
Windows XP Professional
.

User: "fcvcnet"

Title: Re: faq; struct and class another difference? 31 Mar 2007 11:03:25 AM

typedef struct RecordTwoSegmentNo
{
int firstsegmentno;
int secondsegmentno;
bool operator == (const RTSN& rmyrtsn);
bool operator != (const RTSN& rmyrtsn);
}RTSN;

...
What is wrong? Is struct and class has another difference?
Thank you.

Sorry. I change it to
struct RecordTwoSegmentNo
{
int firstsegmentno;
int secondsegmentno;
bool operator == (const RecordTwoSegmentNo& rmyrtsn);
bool operator != (const RecordTwoSegmentNo& rmyrtsn);
};
typedef RecordTwoSegmentNo RTSN;
and it is solved.
--
Thank you very much! :)
Thank this newsgroup very much! :)
Visual Studio 2005 Professional Edition
Windows XP Professional
.

User: "Salt_Peter"

Title: Re: faq; struct and class another difference? 31 Mar 2007 12:01:47 PM
On Mar 31, 11:57 am, fcvcnet <fcvc...@163.com> wrote:

Hi,
I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
Jos=A8=A6e Lajoie, Barbara E. Moo
"If we define a class using the class keyword, then any members
defined before the first access label are implicitly private; ifwe
usethe struct keyword, then those members are public. Whether we define
a class using the class keyword or the struct keyword affects only the
default initial access level."
Now I defined a struct with overload operators =3D=3D and !=3D :
...
typedef struct RecordTwoSegmentNo
{
int firstsegmentno;
int secondsegmentno;
bool operator =3D=3D (const RTSN& rmyrtsn);
bool operator !=3D (const RTSN& rmyrtsn);

the type RTSN is not declared yet.
This is clearly explained in the errors you see below.


}RTSN;

bool RecordTwoSegmentNo::operator =3D=3D(const RTSN &rmyrtsn)
{
return (firstsegmentno=3D=3Drmyrtsn.firstsegmentno &&
secondsegmentno=3D=3Drmyrtsn.secondsegmentno);

}

bool RecordTwoSegmentNo::operator !=3D(const RTSN &rmyrtsn)
{
return !(*this=3D=3Drmyrtsn);}

...

The compiler (vs2005+winxp) report:
------ Build started: Project: maxborder, Configuration: Debug Win32 ----=

--

Compiling...
maxborder.cpp
c:\maxborder\mypoint.h(16) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
c:\maxborder\mypoint.h(16) : error C2143: syntax error : missing ','
before '&'
c:\maxborder\mypoint.h(17) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
c:\maxborder\mypoint.h(17) : error C2143: syntax error : missing ','
before '&'
c:\maxborder\mypoint.h(21) : error C2511: 'bool
RecordTwoSegmentNo::operator =3D=3D(const RTSN &)' : overloaded member
function not found in 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(13) : see declaration of 'RecordTwoSegment=

No'

c:\maxborder\mypoint.h(27) : error C2511: 'bool
RecordTwoSegmentNo::operator !=3D(const RTSN &)' : overloaded member
function not found in 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(13) : see declaration of
'RecordTwoSegmentNo'
...
What is wrong? Is struct and class has another difference?
Thank you.

--

Other than default access, there is no difference whatsoever between
struct and class.
The exception to that rule is a POD.
If you need to declare a class and then typedefine it do it on a
seperate line or don't do it at all. The reason for that is obvious
from your code above.
#include <iostream>
struct Record
{
Record(int f =3D 0, int s =3D 0) : first(f), second(s) { }
bool operator =3D=3D (const Record& record) const;
bool operator !=3D (const Record& record) const;
private:
int first;
int second;
};
// typedef Record RTSN;
bool Record::operator =3D=3D (const Record& record) const
{
return ( first =3D=3D record.first &&
second =3D=3D record.second );
}
bool Record::operator !=3D (const Record& record) const
{
return !(*this =3D=3D record);
}
int main()
{
Record a, b; // default intialized
Record c(1,1);
std::cout << "a =3D=3D b: " << (a =3D=3D b) << std::endl;
std::cout << "a =3D=3D c: " << (a =3D=3D c) << std::endl;
std::cout << "a !=3D b: " << (a !=3D b) << std::endl;
std::cout << "a !=3D c: " << (a !=3D c) << std::endl;
}
/*
a =3D=3D b: 1
a =3D=3D c: 0
a !=3D b: 0
a !=3D c: 1
*/
.
User: "James Kanze"

Title: Re: faq; struct and class another difference? 01 Apr 2007 05:13:46 AM
On Mar 31, 7:01 pm, "Salt_Peter" <pj_h...@yahoo.com> wrote:

On Mar 31, 11:57 am, fcvcnet <fcvc...@163.com> wrote:

[...]

Other than default access, there is no difference whatsoever between
struct and class.
The exception to that rule is a POD.

In what way? Both:
struct Toto { int x; int y; } ;
and
class Titi { public : int x; int y ; } ;
are PODS. For the compiler, it makes no difference. (For the
human reader, of course, one generally expects the former.)
--
James Kanze (Gabi Software) email:

Conseils en informatique orient=A8=A6e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=A8=A6mard, 78210 St.-Cyr-l'=A8=A6cole, France, +33 (0)1 30 23 00 =
34
.
User: "Old Wolf"

Title: Re: faq; struct and class another difference? 01 Apr 2007 07:44:59 PM
On Apr 1, 10:13 pm, "James Kanze" <james.ka...@gmail.com> wrote:

On Mar 31, 7:01 pm, "Salt_Peter" <pj_h...@yahoo.com> wrote:

Other than default access, there is no difference whatsoever between
struct and class. The exception to that rule is a POD.


In what way? Both:

struct Toto { int x; int y; } ;
and
class Titi { public : int x; int y ; } ;

are PODS. For the compiler, it makes no difference.

There is another difference: the default derivation type
for structs is 'public', but for classes it is 'private'.
.



User: "Rolf Magnus"

Title: Re: faq; struct and class another difference? 31 Mar 2007 11:04:02 AM
fcvcnet wrote:

Hi,
I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
Josée Lajoie, Barbara E. Moo
"If we define a class using the class keyword, then any members
defined before the first access label are implicitly private; ifwe
usethe struct keyword, then those members are public. Whether we define
a class using the class keyword or the struct keyword affects only the
default initial access level."

It also affects the access level of inherited members of base classes for
which it isn't explicitly specified.

Now I defined a struct with overload operators == and != :
...
typedef struct RecordTwoSegmentNo

Lose the typedef.

{
int firstsegmentno;
int secondsegmentno;
bool operator == (const RTSN& rmyrtsn);
bool operator != (const RTSN& rmyrtsn);
}RTSN;

bool RecordTwoSegmentNo::operator ==(const RTSN &rmyrtsn)
{
return (firstsegmentno==rmyrtsn.firstsegmentno &&
secondsegmentno==rmyrtsn.secondsegmentno);

}

bool RecordTwoSegmentNo::operator !=(const RTSN &rmyrtsn)
{
return !(*this==rmyrtsn);
}
...

The compiler (vs2005+winxp) report:
------ Build started: Project: maxborder, Configuration: Debug Win32
------ Compiling...
maxborder.cpp
c:\maxborder\mypoint.h(16) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
c:\maxborder\mypoint.h(16) : error C2143: syntax error : missing ','
before '&'
c:\maxborder\mypoint.h(17) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
c:\maxborder\mypoint.h(17) : error C2143: syntax error : missing ','
before '&'
c:\maxborder\mypoint.h(21) : error C2511: 'bool
RecordTwoSegmentNo::operator ==(const RTSN &)' : overloaded member
function not found in 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(13) : see declaration of
'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(27) : error C2511: 'bool
RecordTwoSegmentNo::operator !=(const RTSN &)' : overloaded member
function not found in 'RecordTwoSegmentNo'
c:\maxborder\mypoint.h(13) : see declaration of
'RecordTwoSegmentNo'
...
What is wrong? Is struct and class has another difference?

No, it doesn't have anything to do with differences between class and
struct. What makes you even believe that?
The reason for your problem is that your typedef name is used inside your
struct definition, but it's not known at that point yet.
.
User: "fcvcnet"

Title: Re: faq; struct and class another difference? 31 Mar 2007 11:19:17 AM
Rolf Magnus 写é“:

No, it doesn't have anything to do with differences between class and
struct. What makes you even believe that?
The reason for your problem is that your typedef name is used inside your
struct definition, but it's not known at that point yet.

Thank you very much. Now I know where I am wrong.
--
Thank you very much! :)
Thank this newsgroup very much! :)
Visual Studio 2005 Professional Edition
Windows XP Professional
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER