faq; struct and class another difference?

F

fcvcnet

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::eek:perator ==(const RTSN &rmyrtsn)
{
return (firstsegmentno==rmyrtsn.firstsegmentno &&
secondsegmentno==rmyrtsn.secondsegmentno);

}

bool RecordTwoSegmentNo::eek:perator !=(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::eek:perator ==(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::eek:perator !=(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.
 
F

fcvcnet

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

Rolf Magnus

fcvcnet said:
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::eek:perator ==(const RTSN &rmyrtsn)
{
return (firstsegmentno==rmyrtsn.firstsegmentno &&
secondsegmentno==rmyrtsn.secondsegmentno);

}

bool RecordTwoSegmentNo::eek:perator !=(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::eek:perator ==(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::eek:perator !=(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.
 
F

fcvcnet

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

Salt_Peter

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

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

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

}

bool RecordTwoSegmentNo::eek:perator !=(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::eek:perator ==(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::eek:perator !=(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 = 0, int s = 0) : first(f), second(s) { }
bool operator == (const Record& record) const;
bool operator != (const Record& record) const;
private:
int first;
int second;
};

// typedef Record RTSN;

bool Record::eek:perator == (const Record& record) const
{
return ( first == record.first &&
second == record.second );
}

bool Record::eek:perator != (const Record& record) const
{
return !(*this == record);
}

int main()
{
Record a, b; // default intialized
Record c(1,1);

std::cout << "a == b: " << (a == b) << std::endl;
std::cout << "a == c: " << (a == c) << std::endl;
std::cout << "a != b: " << (a != b) << std::endl;
std::cout << "a != c: " << (a != c) << std::endl;
}

/*
a == b: 1
a == c: 0
a != b: 0
a != c: 1
*/
 
J

James Kanze

On Mar 31, 11:57 am, fcvcnet <[email protected]> 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.)
 
O

Old Wolf

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

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,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top