struct within namespace

Hi, I have a header file
"common.h" where I have declared
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace A
{
	namespace B
	{
		struct AB
		{
			AB(int i, int j)
			{
				a=i;
				b=j;
			}
			int a;
			int b;
			void add()
			{
				printf("\n value: %d",a+b);
				printf("\n");
			}
			
		};
	}
}


But When I call the same in main.cpp as
1
2
A::B::AB obj(7,9);
obj.add();


I get the error as
"AB is not a member of A::B"

May I know where I am going wrong
Last edited on
I was able to compile it just fine:

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
26
27
28
29
30
#include <stdio.h>
#include <conio.h>
namespace A
{
    namespace B
    {
        struct AB
        {
            AB (int i, int j)
            {
                a=i;
                b=j;
            }
            int a;
            int b;
            void add()
            {
                printf ("\n value: %d",a+b);
                printf ("\n"); //Why not just change the above line to printf ("\n value: %d\n",a+b); ?
            }
        };
    }
}
int main ()
{
    A::B::AB Text (3, 5);
    Text.add (); //I got 8 here...
    getch ();
    return 0;
}


I doubt that it will matter with this, but what compiler are you using?
It should work, what compiler are you using?

Please use [code][/code] tags
I am using Microsoft visual studio V9 with target machine: x86
Are you sure you are compiling it as (plain) C++?
Yes , its plain cpp with output file as Test.exe
Topic archived. No new replies allowed.