473,625 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I don't know what's wrong with my very simple code

Sam
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;
public:
friend void point::init(pos e P);
};
#endif

////////////////pose.cpp
#include "pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include "pose.h"
class point
{
public:
void init(pose P);
};
#endif

////////////////point.cpp
#include "point.h"
void point::init(pos e P)
{
P.i=1;
}

////////////////main.cpp
#include "pose.h"
#include "point.h"
void main()
{
}
Basically the "point" class has a function to modify the "pose"
object's private data "i". VC++ always gives the error messages like:
'point' : is not a class or namespace name
'i' : cannot access private member declared in class 'pose'
syntax error : identifier 'pose'
nonexistent function 'point::init' specified as friend

I cannot understand it. Could anybody tell me what he thinks of it?
Thanks a lot.
Jul 22 '05 #1
20 2557
Sam writes:
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;
i is, by default private. Which is what the compiler told you. Good
practice says it *should be* private. Use a friend class/function or use an
accessor to fiddle with the value of i. For a real program you should
probably rethink your overall design.

You are on the verge of developing some bad habits.
public:
friend void point::init(pos e P);
};
#endif

////////////////pose.cpp
#include "pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include "pose.h"
class point
{
public:
void init(pose P);
};
#endif

////////////////point.cpp
#include "point.h"
void point::init(pos e P)
P.i=1;
}

////////////////main.cpp
#include "pose.h"
#include "point.h"
void main()
{
}
Basically the "point" class has a function to modify the "pose"
object's private data "i". VC++ always gives the error messages like:
'point' : is not a class or namespace name
'i' : cannot access private member declared in class 'pose'
syntax error : identifier 'pose'
nonexistent function 'point::init' specified as friend


Jul 22 '05 #2
A friend function can not be a member of a class. So point::init can not be
a friend to pose.

Jakob

"Sam" <hc***@nd.edu > wrote in message
news:f0******** *************** **@posting.goog le.com...
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;
public:
friend void point::init(pos e P);
};
#endif

////////////////pose.cpp
#include "pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include "pose.h"
class point
{
public:
void init(pose P);
};
#endif

////////////////point.cpp
#include "point.h"
void point::init(pos e P)
{
P.i=1;
}

////////////////main.cpp
#include "pose.h"
#include "point.h"
void main()
{
}
Basically the "point" class has a function to modify the "pose"
object's private data "i". VC++ always gives the error messages like:
'point' : is not a class or namespace name
'i' : cannot access private member declared in class 'pose'
syntax error : identifier 'pose'
nonexistent function 'point::init' specified as friend

I cannot understand it. Could anybody tell me what he thinks of it?
Thanks a lot.

Jul 22 '05 #3
osmium wrote:
Sam writes:

Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;

i is, by default private. Which is what the compiler told you. Good
practice says it *should be* private. Use a friend class/function or use an
accessor to fiddle with the value of i. For a real program you should
probably rethink your overall design.

You are on the verge of developing some bad habits.

The design issue aside, the OP's question is about why the friend
declaration doesn't work as he thinks it should. He knows that i is
private, and he did have a friend function declared.

To the OP, you could get around your problem by using a combination of a
reference parameter and a forward declaration:

////////////////////////////////////////////////////////////////////////////
// A.hpp
#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED

#include "B.hpp"

class A
{
/* ... */
friend void B:f(A& a); // A forward declaration does not suffice
// here; you must have the full definition
// of B, hence the #include above.
/* ... */
};

#endif // include guard

////////////////////////////////////////////////////////////////////////////
// B.hpp
#ifndef B_HPP_INCLUDED
#define B_HPP_INCLUDED

class A; // forward declaration

class B
{
/* ... */
public:
void f(A& a); // Note that for a reference or pointer
// to A, you only need the forward
// declaration, not the full class
// definition.

/* ... */
};
The reason what you tried earlier didn't work is that your include
directives would have resulted in a circular dependency, which your
include guard prevented. (That's what include guards are for, after all.)

If you can't use a reference parameter, then you will have to try
something else -- like a public accessor method in class A. However,
you should prefer to pass classes by reference in most situations, as
passing by value invokes the class' copy constructor. Note that in that
case, the function would only modify a copy of the parameter anyway,
which is almost certainly not what you want.

HTH,

- Adam

--
Reverse domain name to reply.

Jul 22 '05 #4

"Sam" <hc***@nd.edu > wrote in message
news:f0******** *************** **@posting.goog le.com...
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;
public:
friend void point::init(pos e P);
};
#endif

////////////////pose.cpp
#include "pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include "pose.h"
class point
{
public:
void init(pose P);
};
#endif

////////////////point.cpp
#include "point.h"
void point::init(pos e P)
{
P.i=1;
}

////////////////main.cpp
#include "pose.h"
#include "point.h"
void main()
{
}
Basically the "point" class has a function to modify the "pose"
object's private data "i". VC++ always gives the error messages like:
'point' : is not a class or namespace name
'i' : cannot access private member declared in class 'pose'
syntax error : identifier 'pose'
nonexistent function 'point::init' specified as friend

I cannot understand it. Could anybody tell me what he thinks of it?
Thanks a lot.


//////////////// pose.h
#ifndef pose_h
#define pose_h

#include "point.h"
class pose {
int i;
friend void point::init (pose &P);
};

#endif

//////////////// point.h
#ifndef point_h
#define point_h

class pose;

class point {
public:
void init (pose &);
};

#endif

//////////////// pose.cpp
#include "pose.h"

//////////////// point.cpp

#include "point.h"
#include "pose.h"

void point::init (pose &P) {
P.i = 1;
}

//////////////// main.cpp

#include "pose.h"
#include "point.h"
int main () {
pose p;
point pt;
pt.init (p);
}

I think you mean to pass the 'pose' by reference? As others have mentioned,
there are probably better ways of achieving your goal here.

Regards

Brian


Jul 22 '05 #5

"Jakob B. Olsen" <ja***********@ hotmail.com> wrote in message
news:3f******** **************@ dread14.news.te le.dk...
A friend function can not be a member of a class. So point::init can not be a friend to pose.


This is total nonesense.
Jul 22 '05 #6
Sam
"Jakob B. Olsen" <ja***********@ hotmail.com> wrote in message news:<3f******* *************** @dread14.news.t ele.dk>...
A friend function can not be a member of a class. So point::init can not be
a friend to pose.

Jakob

I checked it out from the book "thinking in c++" finding that a friend
fn can be a member of a class.

Now I think the problem might be, whichever class is compiled first,
it needs the info of the the other class. When it goes to the other
class, it finds that it needs the previous class. That's the way I
understand it because I switched the order of compiling classes but
always found the compiler thinks the class (or class member fn) is not
defined.

Sam
"Sam" <hc***@nd.edu > wrote in message
news:f0******** *************** **@posting.goog le.com...
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;
public:
friend void point::init(pos e P);
};
#endif

////////////////pose.cpp
#include "pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include "pose.h"
class point
{
public:
void init(pose P);
};
#endif

////////////////point.cpp
#include "point.h"
void point::init(pos e P)
{
P.i=1;
}

////////////////main.cpp
#include "pose.h"
#include "point.h"
void main()
{
}
Basically the "point" class has a function to modify the "pose"
object's private data "i". VC++ always gives the error messages like:
'point' : is not a class or namespace name
'i' : cannot access private member declared in class 'pose'
syntax error : identifier 'pose'
nonexistent function 'point::init' specified as friend

I cannot understand it. Could anybody tell me what he thinks of it?
Thanks a lot.

Jul 22 '05 #7
In file point.cpp, the first line of code is:

#include "point.h"

Try changing it to:

#include "pose.h"
Jul 22 '05 #8
On 6 Jan 2004 06:41:20 -0800, hc***@nd.edu (Sam) wrote:
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.

The code include 5 files as following, delimited by //////:

////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
class pose
{
int i;
public:
friend void point::init(pos e P);
};
#endif

////////////////pose.cpp
#include "pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include "pose.h"
The above line shouldn't be there - it introduces a circular
dependency (pose.h depends on point.h which in turn depends on pose.h
and so on). Instead, just use a forward declaration:
class pose;
class point
{
public:
void init(pose P);
I think you intended to pass by reference:

void init(pose& P);
};
#endif

////////////////point.cpp
#include "point.h"
and here you now need:
#include "pose.h" //need complete definition of pose.
void point::init(pos e P)
{
P.i=1;
}

////////////////main.cpp
#include "pose.h"
#include "point.h"
void main()
{
}


Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9
#ifndef point_h
#define point_h

#include "pose.h"

class pose; // <--------- ADD THIS forward decleration

class point
{
public:
void init(pose P);
};

#endif
Jul 22 '05 #10

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

Similar topics

6
3538
by: BadOmen | last post by:
I have a text file that I want to save from my program. But I don't want to save the empty lines. I want to delete everything after the last character, Is that possible? Then when I read the text file i don't want to read empty lines(If the user as edit the file in notpad..), sense it makes commas at all new lines and then split the text at every comma in to an array. The array will contain empty "slot's" if there is a lot of commas in a...
303
17572
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
0
2289
by: DP | last post by:
(2nd post, I think my first may have been to the wrong group - sorry) Hello Perl-ers - I´m hoping I can get some help here, because I'm very lost. Don't know Perl, I'm not a programmer. And this is something that has worked for me earlier, only now it doesn't anymore. --------------------------------- The ¨problem¨ script -
3
1964
by: Jacques Koorts | last post by:
Hi I really don't know anymore how to solve this problem. I bought .NET thinking i can debug my way out of it but still no cigar. This very simple piece of code does not work: Now here some things before looking at the code: 1. In .NET and IE and IIS I've set all possible debugging options
10
1770
by: Ricola ! | last post by:
Two c# dll's were compiled and added to the GAC. They appear in the .NET Configuration tool. However, when trying to add a reference to a new project, the dlls do not appear in the .NET property sheet. Why not?
2
2488
by: Alan Silver | last post by:
Hello, I'm having rather a problem with user control. It is a fairly simple affair (see my other threads for more details) that shows a date and time in five drop down controls. I had private member variables for the day, month, year, hour and minute, and the public property that sets the DateTime simply stored the relevant numbers in these variables.
4
1964
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory location? Does synclocking a reference type put an entry in a catalog somewhere in the system, and it's removed on the "end synclock" statement? Are there any guidelines for creating synclock objects? I've found a surprisingly small amount of...
3
2246
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I have a VB app, and I have been working at it for a while, and I am now at the stage where I want to create a search function. Now don't be scared! It is in the .Net compact framework, and uses SQL Server CE as the database (This seems to scare off people trying to help! lol) but the connection and reading of data etc is all handled, and I think it is going to be a "relatively" simple function. My database has a number of fields...
2
1765
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7182
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.