473,505 Members | 14,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is wrong with this simple code?

Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?

Nov 29 '06 #1
6 1211
I am a mere student so take what I say with a grain of salt.

I think get might be reserved, but if it isn't then you need to do:

int get(int x){return 1;}

and that should work.

asdf wrote:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?
Nov 29 '06 #2
asdf wrote:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
I think you mean:

int get(int x) { return 1; }

Best regards,

Tom

Nov 29 '06 #3
On 28 Nov 2006 20:01:47 -0800 in comp.lang.c++, "asdf"
<li*********@gmail.comwrote,
>Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
In the above, x would be the type of the unnamed function argument.
However, x is not a type.
Nov 29 '06 #4

asdf wrote:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
The compiler in this context is unable to establish a signature for the
member function. Any parameter specified must therefore have a type in
order to construct the signature. Remember that parameters are objects
from the outside world insofar as the instance of the class is
concerned. get(...) doesn't need to pass x as a parameter since it is
already part of the object.
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?
Presumably, your goal is to use get(...) to "get" x's value all you
need to do is return the private integer. In order to protect x from
being modified through side-effects, get() should be constant.

class A
{
int x;
public:
A(int n = 1) : x(n) { } // or A() : x(1) { }
int get() const { return x; }
};

And we haven't touched copy ctors nor assignment operators.

Nov 29 '06 #5
asdf wrote:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?
class A{
int x;
public:
int get() { return x; };
};

Is this what you meant?
Nov 29 '06 #6
>
class A{
int x;
public:
int get() { return x; };
Better still, make it

int get() const {return x;}
};

Is this what you meant?
Nov 29 '06 #7

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

Similar topics

220
18803
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
5956
by: learningGuy | last post by:
Can someone tell me what is wrong with this simple code? I get an exception every time at the myFile.Open() line. I have included the code that I think is needed to for you to answer this below:...
125
14539
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
9907
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
46
4154
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
2
1792
by: Phil Certain | last post by:
Hi, Relative newbie to .Net but experienced with classic ASP. I am trying to create a simple business object to contain commonly used functions. This is what I have done: 1 - Created a simple...
98
4490
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
2
2560
by: Tarik Monem | last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong casting_registration.php <table> <tr> <td> <form enctype="multipart/form-data" action="thankyou.php"...
20
2795
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
3066
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
0
7216
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
7098
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...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.