field 'obj' has incomplete type note: forward declaration of 'class B' - class

I have seen before in stack overflow classes can friend each other. Here I am doing class A and class B friend each other. But when I create an object in A class for B its throws an error which is "field 'obj' has incomplete type note: forward declaration of 'class B'". I also declared class B with forward declaration. But it's not working.
How can I solve it? Thanks for your Help!
#include<iostream>
class B;
class A{
friend class B;
B obj;
public:
int x = 199;
};
class B{
friend class A;
A obj;
};
int main(){
}

If you had said B * obj;, it would work. The problem is that, without having the full declaration before hand, the compiler does not know how large B is, and thus cannot establish a size for A.

Related

Create Class Object within Method of Another Class C++

I am new to C++. How do you create a method within one class which initializes an object within another class with specified parameters? Something like the following.
class A {
public:
double X;
double Y;
A(double a, double b) {
X = a;
Y = b;
};
class B {
public:
A f(double a, double b) {
//Initialize an object of type A using parameters specified.
};
};
I want to use the object of type A later so, presumably, I would need to use the new operator within f. Thanks in advance.
Try this:
class B {
public:
A* f(double a, double b) { return new A(a, b); };
};
Although you could just as easily do new A(a,b) anywhere you wanted to do B.f(a,b).

In Swift, can Types (not instances) conform to protocols?

I have a ViewController that uses a class (call it A) from a framework outside of my control. A provides a class function I need (call it f). I want to make A easy to mock out in tests.
So my idea was to create a protocol P that has the same signature as A, extend A to implement P, and then create a mock class M that also implements P and has a dummy implementation of f. Then in my tests, I could just do viewController.dependency = M and everything should be dandy.
This is easier said than done, for reasons I'm hoping you'll help me understand.
Inside my viewController, it's easy to declare a variable that contains a protocol-conforming instance and then reassign the variable to another Protocol-conforming instance:
// works
var dependency: P = A()
dependency.f()
dependency = M()
dependency.f()
But it doesn't work to do the same thing with just the types:
// doesn't work
var dependency = A.self
dependency = M.self // cannot assign value of M.Type to a value of type A.Type
// also doesn't work
var dependency: P = A.self // type A.Type does not conform to protocol P
Is there a way to make this work? I thought maybe I could use a generic type for dependency but I can't figure out a syntax to declare a generic type for a variable assignment.
If you really want to store the type itself, you can use P.Type. The following works in Swift 2:
protocol P {
static func foo()
}
class A: P {
static func foo() { print("A foo") }
}
class M: P {
static func foo() { print("M foo") }
}
var dependency: P.Type = A.self
dependency = M.self
dependency.foo()
It doesn't work because when you say var dependency = A.self, the compiler infers, from the right-hand side, that dependency's type is A. That means that if dependency ever gets reassigned, it can be assigned only to some other instance of an A class type. When you created protocol P with the same signature as A, then a a class M that implements P, you still haven't (and can't, since it's out of your control) made A implement protocol P. The way the Swift and Objective-C runtimes work is that each object contains a pointer to its type class. This type is inspected when assigning, and if the right-hand value is being assigned to a left-hand declaration, then the right-hand value has to have the left-hand's type somewhere in its inheritance hierarchy.
Some languages (e.g. Ruby) feature so-called duck typing, where if an object or class walks like a duck, then it is a duck, even if it's not really an instance of a duck. That's what you're looking for here, but Swift (and Objective-C) don't work that way. Duck typing is a type of pseudo-polymorphism. It's not really polymorphic.

Pointer to a derived class C++

I'm having some trouble with deriving a pointer to a derived class. I think it has something to do with a constructor. Do I have to create a new constructor in my derived class? How to I create a pointer from a derived class? Thanks
Class Base
{
public:
int myfunction(int a,int b,int c)
{
return a+b+c;
}
};
Class Derived: public Base
{
int newfunction(int a, int b, int c)
{
return a*b*c;
};
};
int main()
{
// this doesn't work at all.. I get all errors every time I try to refer to the object
//instantiated from my derived class.
//I know it's my lack of understanding.
Derived *NewObject = new Derived;
//Why wont this work?
}
C++ is case sensitive, and the keyword class is lowercase. You wrote Class for both classes, and it looks like it's the only issue with your code.

why class B throws an error

please explain me how this code is working and why it will trough an error in class B
public class A
{
protected int x;
static void F(A a, B b) {
a.x = 1; // Ok
b.x = 1; // Ok
}
}
public class B: A
{
static void F(A a, B b) {
a.x = 1; // Error, must access through instance of B
b.x = 1; // Ok
}
}
Code in B can only access a protected variable through an expression which has a compile-time type of B or some type derived from B. That's basically how protected access works.
From section 3.5.3 of the C# 4 language specification:
When a protected instance member is accessed outside the program text of the class in which it is declared, and when a protected internal instance member is accessed outside the program text of the program in which it is declared, the access must take place within a class declaration that derives from the class in which it is declared. Furthermore, the access is required to take place through an instance of that derived class type or a class type constructed from it. This restriction prevents one derived class from accessing protected members of other derived classes, even when the members are inherited from the same base class.
The protected keyword is a member
access modifier. A protected member
is accessible from within the class in
which it is declared, and from within
any class derived from the class
that declared this member.
public class A
{
public int x;
public static void F(A a, B b)
{
a.x = 1;
b.x = 1;
}
}
public class B : A
{
public static void F(A a, B b)
{
a.x = 1;
b.x = 1;
}
}
Why I redefined it with public access modifier. protected modifier has restricted access to inherit class blocks.
Class A {
protected int x = 0;
}
Class B : A {
private void SomeFunc() {
Console.WriteLine(this.x.ToString()); // This will work!
}
}
But if you try to access x you'll get nothing in B.
B b = new B();
b.x; // Got nothing in IntelliSense
See we got the access of x in a function of B but it's instance has no access of x.

c++ defining a static member of a template class with type inner class pointer

I have a template class like here (in a header) with a inner class and a static member of type pointer to inner class
template <class t> class outer {
class inner {
int a;
};
static inner *m;
};
template <class t> outer <t>::inner *outer <t>::m;
when i want to define that static member i says "error: expected constructor, destructor, or type conversion before '*' token" on the last line (mingw32-g++ 3.4.5)
You need to qualify that the inner class is a typename, since it’s dependent on a template parameter and the C++ compiler assumes that the name inner in this context is not a type:
template <class t> typename outer<t>::inner* outer<t>::m;
Rationale: the name inner in the above line depends on a type name, t. The C++ compiler at this point doesn’t know what inner is, because the meaning of the name inner can differ depending on t. For example, suppose that, somewhere else in the code, there is a specialized version of the outer class for int:
template <>
class outer<int> {
int inner;
};
Now, outer<int>::inner no longer names a type; it names a member variable.
Thus, in the general case the meaning of outer<t>::inner would be ambiguous and C++ resolves this ambiguity assuming that inner does not name a type. Unless you say it does, by prefixing it with typename: typename outer<t>::inner. (In this context, inner is called a dependent name since it depends on the exact type of t.)