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

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
Mar 31 '07 #1
6 2242
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
Mar 31 '07 #2
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.

Mar 31 '07 #3
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
Mar 31 '07 #4
On Mar 31, 11:57 am, fcvcnet <fcvc...@163.comwrote:
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::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.

--
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::operator == (const Record& record) const
{
return ( first == record.first &&
second == record.second );
}

bool Record::operator != (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
*/

Mar 31 '07 #5
On Mar 31, 7:01 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
On Mar 31, 11:57 am, fcvcnet <fcvc...@163.comwrote:
[...]
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: ja*********@gmail.com
Conseils en informatique orient¨¦e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S¨¦mard, 78210 St.-Cyr-l'¨¦cole, France, +33 (0)1 30 23 00 34

Apr 1 '07 #6
On Apr 1, 10:13 pm, "James Kanze" <james.ka...@gmail.comwrote:
On Mar 31, 7:01 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
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'.
Apr 2 '07 #7

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

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
0
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/authoring-faq Posting-Frequency: twice a week (Mondays and Thursdays) Last-modified: April 10, 2003 Version: 1.16 URL: http://css.nu/faq/ciwas-aFAQ.html Maintainer:...
22
by: Ook | last post by:
We have had a discussion on the differences between a class and a structure, and no one is in agreement. As I understand it, a structure defaults to public, a class defaults to private. There are...
13
by: Ook | last post by:
I have been told that the only difference between a class and a struct is the default interface - private for class, public for struct. My instructor made this statement: Actually there are only...
21
by: Christopher Benson-Manica | last post by:
"I came across some code that declared a structure like this: struct name { int namelen; char namestr; }; and then did some tricky allocation to make the namestr array act like it had...
28
by: Jon Skeet [C# MVP] | last post by:
A while ago (too long ago) I agreed to start up a FAQ for this newsgroup. Today, I've finally got it into a sufficiently reasonable form that it's worth putting up. It's at...
17
by: Richard A. Lowe | last post by:
Hi, here are answers for some of the questions I suggested a short time ago for the m.p.d.l.c FAQ Jon Skeet is organizing. It was Jon's intention that these be 'honed' by the group before being...
5
by: dis_is_eagle | last post by:
Hi.I would like to know the difference between a C struct and a C++ struct.I think in a C++ struct member variables and functions are by default private whether in a C struct they are public by...
6
by: subramanian | last post by:
What is the difference between a struct and a class ? When should a struct be used and when should a class be used ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.