I currently have the following two classes
class TOrder
{
public:
private:
.......
};
Now my other class is :
#include "TOrder.h"
namespace namespaceA
{
namespace namespaceB
{
class OrderDis
{
private:
TOrder* frmPointer;
.....
};
}
}
The above works fine the problem starts when I use an object of OrderDis in TOrder as such
#include <QMainWindow>
#include "OrderDis" //Added - Creates Problem
class TimedOrder
{
public:
.......
};
Any suggestion on how I could use forward declaration to resolve my issue ?
You could forward OrderDispatcher in TimeOrder.h
namespaceA
{
namespaceB
{
class OrderDispatcher;
}
}
class TimedOrder
{
//...
};
The forward declaration can be written as:
namespace A{ namespace B{ class OrderDispatcher; } }
As you only use a pointer to TimedOrder in the OrderDispatcher class, it can be solved by simply not including TimedOrder.h in the OrderDispatch.h file. Instead just declare the TimedOrder class:
class TimedOrder;
No need to muck about with namespaces and such then.
Note: You can't declare it in any of the namespaces, declare it instead where you now do your #include.
Related
Is it possible to use class name which declare in namespace without namespace?
I tried like this:
//MyClass.h
namespace MyNamespace {
class MyClass;
}
class MyNamespace::MyClass {
public:
/* ... */
}
//MyOtherClass.cpp
#include "MyClass.h"
using namespace MyNamespace;
void MyOtherClass::MyFunction() {
MyClass *myClass = new MyClass;
}
But it doesn't work.
Complier says "MyClass is ambiguous."
I guess this code would work :
//MyOtherClass.cpp
#include "MyClass.h"
using namespace MyNamespace;
void MyOtherClass::MyFunction() {
MyNamespace::MyClass *myClass = new MyClass;
}
But it is uncomfortable for me.
I wanna use 'MyClass' without "MyNamespace::".
Is it possible?
Thanks for your help.
I got answer : It is not possible.
C++ classes have own default namespace. If I declare MyNamespace, then there is two namespaces associated with MyClass. If I use MyClass without choosing what namespace I want to use, Compiler feel ambiguous what namespace I really wanted to use. So it would tell me "MyClassis ambiguous."
if the below code is all in the same .h file (you did not really clarify)
//MyClass.h
namespace MyNamespace {
class MyClass;
}
class MyNamespace::MyClass {
public:
/* ... */
}
Then what you are doing a forward declaration of MyNamespace::MyClass in the first part.
You should have something like this in MyClass.h
namespace MyNamespace {
class MyClass
{
public:
// constructor, methods,...
}; // end of class declaration
} // end of namespace
and the associated definition in MyClass.cpp
I'm having a compilation error using Apple's Clang 7.0 with the following friendship code, using C++11 standard. I wonder what's really wrong with it since it seems to be valid for me. I'm describing the setting and the error I'm having:
MyInterface
namespace namespace1
{
class MyInterface
{
friend class MyClass;
public:
virtual void some_method(void) = 0;
...
private:
type some_attribute;
...
}
}
MyClass::MyMethod Implementation
namespace namespace2
{
void MyClass::MyMethod(MyInterface* MyConcrete)
{
...
// MyConcrete implements MyInterface
if(MyConcrete->some_attribute == some_value) // Error*
{
...
}
...
}
}
Error
error: 'some_attribute' is a private member of 'namespace1::MyInterface'
I really expected that MyClass would have access to some_attribute in MyConcrete (which implemented MyInterface) regardless of the class access modifier. Any clues why this error is happening? Any suggestions?
Thank you!
MyClass is in namespace2. So you need to use:
friend class namespace2::MyClass;
You may also need to use forward decleration of MyClass before you define MyInterface.
Here's an example that compiles:
// forward decleration
namespace namespace2
{
class MyClass;
}
namespace namespace1
{
class MyInterface
{
friend class namespace2::MyClass; // added missing namespace
public:
virtual void some_method(void) = 0;
private:
int some_attribute;
};
}
namespace namespace2
{
class MyClass
{
void MyMethod(namespace1::MyInterface* MyConcrete)
{
if(MyConcrete->some_attribute == 1)
{
}
}
};
}
int main()
{
}
You can run it here.
friend class MyClass; in the context of ::namespace1::MyInterface refers to the class ::namespace1::MyClass, which is a different class from ::namespace2::MyClass. (That's the whole point of namespaces, right?)
Change the friend declaration to read like this:
friend class ::namespace2::MyClass;
Note that this requires that the type ::namespace2::MyClass has already been declared, so you either need to forward-declare it (namespace namespace2 { class MyClass; }) or you need to make sure that the definition is included prior to the definition of ::namespace1::MyInterface.
(See this demo.)
Not sure if this is possible, but I have two classes of the same name in different levels of a nested namespace and I'd like to make the more shallow class a friend of the deeper class. Example:
In File1.h:
namespace A
{
class Foo
{
//stuff
};
}
In File2.h:
namespace A
{
namespace B
{
class Foo
{
friend class A::Foo; //Visual Studio says "Error: 'Foo' is not a member of 'A'"
};
}
}
Is this possible? If so, what is the proper syntax?
This code compiles ok when placed in one file (except that a ; is necessary after A::B::Foo class): IdeOne example.
So, the issue is in the code not included in the question text. Probably #include "File1.h" was forgotten in File2.h.
If you want to avoid including large header files into others, you need to at least forward declare your classes before using them:
namespace A
{
class Foo;
namespace B
{
class Foo
{
friend class A::Foo;
}
}
}
If the class G is in the namespace GSpace and it needs to be friends with the class M in the global namespace what do you have to do? I thought this would work:
/////////////////////M.h//////////////////////
#include "GFile.h"
class M
{
public:
friend class GSpace::G; // doesn't work, throws error
}
After researching on StackOverflow a bit, I found this answer https://stackoverflow.com/a/3843743/1797424
/////////////////////M.h//////////////////////
namespace GSpace
{
class G;
}
class M
{
public:
friend class GSpace::G; // works, no error
private:
GSpace::G gClassMember; // errors: "M uses undefined class GSpace::G"
};
// Note that G.h includes M.h so I do not include it here,
// instead I have it included in M.cpp
That does work for getting the class friended. However, it creates an issue when I actually declare a class member using that type, because the class is not defined. GFile.h
Am I misunderstanding how #include and forward declaration behaves, or is it some kind of implementation issue on the side of the compiler (not likely, I'm assuming)?
Because your member is not a pointer or reference, the compiler needs to know the size of G. You can't use a forward declaration.
As noted in the comment, you need to qualify G with the namespace.
Here is code which compiles for me:
namespace GSpace
{
class G
{
};
}
class M
{
public:
friend class GSpace::G;
private:
GSpace::G gClassMember;
};
int main() {return 0;}
Try the following
namespace GSpace
{
class G;
}
class M
{
public:
friend class GSpace::G;
}
namespace GSpace
{
class G { /* definition of the class */ };
}
I have 2 classes ,defined in the different namespaces :
//--==file1.hpp==--
namespace n1{
class x1 {
//.....
};
};
//--==file2.hpp==--
namespace n2{
class x1: public n1::x1{
//.....
};
};
//--== file3.hpp ==--
namespace n2 {
class x2 {
private:
n1::x1* data1_;
public:
void func(x1* data2) { data1_ = data2; }
};
};
The compilation of this fails with
error C2440: '=' : cannot convert from `'n2::x1 *' to 'n1::x1 *'`
I can`t understand what could be a problem,Since n2:x1 inherits from n1::x1...?
Thank you
Inheritance from one namespace to another namespace class, should not have any compilation error. It is just that, in the sub-class, if you have to call the method of the parent class (which is in another namespace), you should use the complete name (with namespace).
For you reference:
namespace a
{
class A1 {
public:
void testA1() {...}
};
}
namespace b
{
class B1: public class a::A1
{
public:
void testB1()
{
a::A1::testA1();
...
}
};
}
But looks like, the above problem was just been a typo issue, and has been resolved. However, to clarify on the usage, sample code shall help.
In file2.h: Include file1.h
In file3.h: Include file1.h and file2.h.
And in main include file3.h.
int main()
{
n2::x2 xx22;
n2::x1* xx11;
xx22.func(xx11);
}
This complies just fine.