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>
usingnamespace 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;
}
};
}
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:
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?)
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;
};
}
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?
#include <iostream>
usingnamespace 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;
};
}
#include <iostream>
usingnamespace 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;
};
}
usingnamespace 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
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 :)
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. :)
#include <iostream>
usingnamespace 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;
};
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? :)
#include <iostream>
usingnamespace 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;
};
#include <iostream>
usingnamespace 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!
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.
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
}
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
}
}