NameSpace Declaration

Hello everyone,
I am new in C++ and I want to learn it in depth. As the result I check internet and just manipulate codes and learn more stuffs.

I want to learn more about namespace and I wrote the code below but I get error.
Could anyone explain how can I have forward declare of a function of a class inside namespace?

Thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
  using namespace std;
  void MYNAMESPACE::myClass::nameSpaceTest();

int main(){
	MYNAMESPACE::myClass c;
	c.nameSpaceTest();
}

namespace MYNAMESPACE {
	class myClass{
	public:
		void nameSpaceTest() {
			cout << "A Message From NameSpace" << endl;
		}
	};
}
Last edited on
Its late where I am so bear with me. :P

You are wanting to declare a namespace and create a class with functions inside it, correct?

The code you have above is largely correct with two minor errors. Line 3 needs to be removed as you are referencing a function, nameSpaceTest();, that has not yet been created thus giving you a compiler error. The other thing is namespace MYNAMESPACE needs to be declared before the int main(); function as the main function is calling for function in myClass before the compiler knows they exist. With all these corrections made code looks like this and runs fine for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
  using namespace std;



namespace MYNAMESPACE {
	class myClass{
	public:
		void nameSpaceTest() {
			cout << "A Message From NameSpace" << endl;
		}
	};
}

int main(){
	MYNAMESPACE::myClass c;
	c.nameSpaceTest();
}




Thought I would go an look for some reading material for you since you said you wanted to learn more about namespaces. This a good breakdown of the subject: http://en.cppreference.com/w/cpp/language/namespace#Using-declarations
Thanks CoffeePoweredComputers !
So I could never define Namespaces in C++ after main then? (No way to declare namespace members if we want to define them after main function?)

Thanks
That is not a namespace-related problem. Your main() needs to know the size of myClass objects. Therefore, your main needs to know the definition of myClass, be it in namespace or not.

You can forward declare in some situations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace foo {
  class bar; // mere declaration
}

int main() {
  foo::bar * dummy = nullptr; // a pointer
}
// no foo::bar objects were used within main

namespace foo {
  class bar{ // definition
    int gaz;
  };
}


For more:
https://herbsutter.com/2013/08/19/gotw-7a-solution-minimizing-compile-time-dependencies-part-1/
http://www.gotw.ca/gotw/024.htm
http://www.gotw.ca/gotw/053.htm
http://www.gotw.ca/publications/mill08.htm
Last edited on
Hello Keskiveto,
Thanks man but I could not exactly get what you mean.
I see you that you defined two name space with the same name. The first one has empty body class to declare foo namespace?

What I am looking is to define namespace after main but have possibility to have forward declare in order to call its function members.

By the way I changed some part of your code to see something in screen but I have compile error.
Could you please explain it?

Thabnks

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 <iostream>
using namespace std;
namespace foo {
  class bar;   
}

int main() {
  foo::bar * dummy; // a pointer
  dummy->test();

}
// no foo::bar objects were used within main

namespace foo {
  class bar{ // definition
  public:
  bar(){
     cout<<"Class constractor is called"<<endl;
  }
  void test(){
      cout<<"Class test function is called"<<endl;
  }
    int gaz;
  };
}
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
using namespace std;

namespace foo 
{
  class bar{ // definition
  public:
  bar(){
     cout<<"Class constructor is called"<<endl;
  }
  void test()
  {
      cout<<"Class test function is called"<<endl;
  }
    int gaz;
  };
}

using namespace foo; // goes here if at all

int main() {
  foo::bar bar2;
  foo::bar * dummy; // a pointer
  foo::bar* bar3 = new foo::bar;
  bar3 -> foo::bar::test();
  dummy->test();
}
Class constructor is called
Class constructor is called
Class test function is called
Class test function is called

https://msdn.microsoft.com/en-AU/library/5cb46ksf.aspx
Last edited on
Hello Kemo,
Thanks for the reply but I believe you even did not read my posts :)

I am trying to have forward declare ( not implementation) of a name space and define ( implement) the name space after the main function.
I want to see how is it possible. So far no one came with an answer that we can compile it.
Check my first post here please :)

Thanks
closed account (48T7M4Gy)
I want to learn more about namespace and I wrote the code below but I get error.


I read your OP almost exclusively as it didn't appear there was too much to see with the rest despite the valuable contributions from others. I'm only addressing the bit I have quoted knowing full well about the second. I was driven by self-interest more than anything. :)

http://stackoverflow.com/questions/2059665/why-cant-i-forward-declare-a-class-in-a-namespace-like-this

Maybe Cubbi will drop in to amplify?
Last edited on
Thanks Kemort.
It was an interesting point but I tried to implement in this code and I failed. Do you know where am I wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
namespace foo {
  class bar;  
}

int main() {
  foo::bar dummy; 
  dummy.test();

}
// no foo::bar objects were used within main

  class foo::bar { // definition
  public:
  bar(){
     cout<<"Class constractor is called"<<endl;
  }
  void test(){
      cout<<"Class test function is called"<<endl;
  }
    int gaz;
};
closed account (48T7M4Gy)
My guess is you probably won't get your idea to work even though it's clear to me what you are trying to do.

I know it's not a positive answer but wouldn't there be more to gain if you adopted separate files for the classes and included them in your own separate namespace? :)
It was an interesting point but I tried to implement in this code and I failed. Do you know where am I wrong?

You failed because you are using the bar class in main() you can't just forward declare the class, main() must know how the class was defined.

As has already been pointed out this is not a namespace issue. Your program would fail even if you removed the namespace.

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 <iostream>

using namespace std;

class bar;   

int main() 
{
  bar * dummy; // a pointer
  dummy->test();  //error: invalid use of incomplete type 'class bar' 

}

class bar{ // definition
  public:
     bar()
     {
        cout<<"Class constractor is called"<<endl;
     }
     void test()
     {
         cout<<"Class test function is called"<<endl;
     }
     int gaz;
 };


jib,
my first code was

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
  using namespace std;
// somehow forward declare namespace here that I do not know how!

int main(){
	MYNAMESPACE::myClass c;
	c.nameSpaceTest();
}

namespace MYNAMESPACE {
	class myClass{
	public:
		void nameSpaceTest() {
			cout << "A Message From NameSpace" << endl;
		}
	};
}


and people came just talked different things and no straightforward answer.

If you know the answer please just change the above code in a way that I do not define the namespace before main. I want to define the name space after main and have some forward declaration.
Thanks!
Last edited on
people came just talked different things and no straightforward answer.

You got several straight forward answers already, but let's try again.

This is not a namespace issue. Even if you remove the namespace the code will fail to compile.


The problem is that in order to use the class the definition (implementation) of the class must be known to the compiler. Since you defined the class after you tried to use the class in main() the program failed to compile. Remember that C++ processes the file from the top down. Just declaring the class before main() is not going to work.
closed account (48T7M4Gy)
Yes, it has already been said a couple of times - the problem is quite simple to explain.
( http://stackoverflow.com/questions/4341198/forward-declaration-of-class-function)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class Foo;              // forward declaration of Foo

class Bar {
public:
    Foo *myFoo;      // Foo can only be referred to by a pointer
};

class Foo {             // must be implemented before it is used
public:
    int value;
    void print(){ std::cout << "Foo is here\n"; }
};

int main(){
	Bar b;
	b.myFoo -> print(); // used here
}
Foo is here


Last edited on
There is no forward declaration of namespace.
You can forward declare a function.
You can forward declare a class.
(You can forward/extern declare a global variable.)
But no namespace. Namespace is prefix to the identifiers.

Contents of same namespace can be listed in separate places. For example, many header and implementation files contain bits of the std namespace.

This is legal; both bar and gaz are within the same namespace:
1
2
3
4
5
6
7
namespace foo {
  class bar { /*code*/ };
}

namespace foo {
  class gaz { /*code*/ };
}



This is legal and common too -- class definition and implementation are in different files:
foo.h
1
2
3
4
5
6
7
8
#ifndef FOO_H
#define FOO_H
namespace bar {
  class foo {
    int gaz();
  };
}
#endif 


foo.cpp
1
2
3
4
5
6
7
#include "foo.h"

namespace bar {
  int foo::gaz() {
    // code
  }
}
closed account (48T7M4Gy)
bar::foo::gaz()
Topic archived. No new replies allowed.