Basic problem with classes

Hi everyone,

Opional read:
At work I am trying to adapt code which I got from the boost website: http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/example/http/client/async_client.cpp

I'm having major trouble with it but I think a lot of that stems from my inexperience of using classes, I've mostly just done C before this (and not very much of it) so I'm in way over my depth.

The Problem:
I've decided to make a very simple program which involves two classes each calling the other a few times and just outputs a few things so that it's obvious what is happening. Here is my code so far.

a.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef A_
#define A_

#include <iostream>
#include "b.h"

class class_a
{
public:
	//class_a();
	void method_a(class_b class_b_object);    //error C2061: syntax error : identifier 'class_b'
};

#endif 


a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "a.h"
#include "b.h"

void class_a::method_a(class_b class_b_object)
{
	::std::cout << "Entered class_a::method_a()" << ::std::endl;
	static int i =0;
	i++;
	if (i<=3){
		class_b_object.method_b();
	}
	::std::cout << "Finished class_a::method_a()" << ::std::endl;
}


int main()
{
	::std::cout << "Entered main()" << ::std::endl;
	class_a class_a_object;
	class_b class_b_object;

	class_a_object.method_a(class_b_object);
	::std::cout << "Finished main()" << ::std::endl;
	return 0;
}


b.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef B_
#define B_

#include <iostream>
#include "a.h"

class class_b
{
public:
//	class_b();
	void method_b(class_a class_a_object);    //error C2061: syntax error : identifier 'class_a'
};

#endif 


b.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "b.h"
#include "a.h"

void class_b::method_b()
{    //error C2511: 'void class_b::method_b(void)' : overloaded member function not found in 'class_b'
	::std::cout << "Entered class_b::method_b()" << ::std::endl;
	static int i =0;
	i++;
	if (i<=3){
		class_a_object.method_a(class_a_object;);
	}
	::std::cout << "Finished class_b::method_b()" << ::std::endl;
}



I am getting this error from visual studio:
syntax error : missing ';' before '.'

I've marked on the code where it says this error occurs.

If someone could tell me how to fix this error that would be great. If they could tell me whether or not I'm going about this whole thing the correct way that would be even better.

Thanks for your time,
Meerkat

EDIT: just to let you know, I had tried class_b.method_b(); both with and without (), and I get the same error. Having another look at the code though, I think () shouldn't be there.
Last edited on
Your question identifies you are a C guy for sure ;-)

C++ doesn't allow you to call a class's non-static member function via class name, you have to call the member function via the class's object. eg:

class_b class_b_object;
class_b_object.method_b();
Thanks, although I still seem to be having problems with it, I've updated the code in the original post to reflect what it currently looks like. Basically I'm getting these errors:

error C2065: 'class_b_object' : undeclared identifier
error C2228: left of '.method_b' must have class/struct/union


Th first I don't understand as I thought class_b class_b_object; was me declaring class_b_object in main.
The second one seems to go against what you told me though so I don't get that either.
Sorry about that I didn't give you a complete code as I thought you could fix your code according to that example.

Th first I don't understand as I thought class_b class_b_object; was me declaring class_b_object in main.
class_b_object is just a local variable, just like a local variable in C, you have to pass it to the function where will use it.

The second error is caused by the first error.
Ah I see. I thought by having the class method's access specifier set as public that would have been enough.

I've added in what I think is correct but I'm getting:

error C2511: 'void class_b::method_b(void)' : overloaded member function not found in 'class_b'
error C2061: syntax error : identifier 'class_a'


I've updated the original post again to make it clear.
error C2511: is caused by inconsistent function prototypes
error C2061: is caused by cross inclusion of a.h and b.h. You cannot design the 2 classes referencing members with each other.
Topic archived. No new replies allowed.