I have found the following example in one of my C++ courses. When I try to compile it I get the following error:
'B::operator A' uses undefined class 'A'
Why does it say that class A is undefined?
#include<iostream>
using namespace std;
class A;
class B
{
int x;
public: B(int i = 107) { x = i; }
operator A();
};
B::operator A() { return x; }
class A
{
int x;
public: A(int i = 6) { x = i; }
int get_x() { return x; }
};
int main()
{
B b;
A a = b;
cout << a.get_x();
system("Pause");
}
The compiler needs to know what A is here:
B::operator A() { return x; }
But you only have a forward declaration. You need to move the declaration of class A above B
You are only allowed to use pointers to or references of incomplete types which is what have when you forward declare a type
You need to declare A above B, so that the definition of A is visible to B.
#include<iostream>
using namespace std;
class A
{
int x;
public: A(int i = 6) { x = i; }
int get_x() { return x; }
};
class B
{
int x;
public: B(int i = 107) { x = i; }
operator A();
};
B::operator A() { return x; }
int main()
{
B b;
A a = b;
cout << a.get_x();
}
This should work.
Related
You can redefine operator << in class by overload it.
However, how do you code it so that it would operates specific to a certain class member?
for example
class C
{
int a;
double b;
}
// I would like something like
void main ()
{
C c;
c.a << 1; // sets class member a to value 1;
}
I want a operator defined in Class C that operates specifically to class member a.
a pesudo-code would be
class C
{
int a;
double b;
void operator << (istream & fin)
{
... fin.get()... some code
}
}
Stating the obvious for a moment, assuming the variable is public, you'd use:
class C
{
int a;
double b;
}
// I would like something like
void main ()
{
C c;
c.a = 1; // sets class member a to value 1;
}
The << and >> operators are bit shifts, which have their own meaning. Overloading those for your own purpose is probably a bad idea.
The C++ way of doing things is to avoid setting member variables externally where possible (e.g. using RAII approaches, to set data at initialisation)....
class C
{
public:
C(int a, double b) : a(a), b(b) {}
int getA() const { return a; }
double getB() const { return b; }
private:
int a;
double b;
};
.... Or by adding a setter method if you really need it, e.g.
class C
{
public:
C(int a, double b) : a(a), b(b) {}
int getA() const { return a; }
double getB() const { return b; }
void setA(int v) { a = v; }
void setB(double v) { b = v; }
private:
int a;
double b;
};
You could in theory generate a new type, and overload the operators for that type, but it's not something I'd recommend (because changing the meaning of an operator is almost always a bad idea)
struct MyIntType {
int i;
// overload cast operator
operator int () {
return i;
}
// assign
MyIntType& operator = (const int& v) {
i = v;
return *this;
}
// not recommended :(
MyIntType& operator << (const int& v) {
i = v;
return *this;
}
};
class C
{
public:
MyIntType a;
double b;
};
void main ()
{
C c;
c.a << 1;
}
Having read your comment above, it sounds like you want to do this:
class C
{
public:
// I'm still not recommending this :(
C& operator << (const int& v) {
a = v;
return *this;
}
private:
int a;
double b;
};
void main ()
{
C c;
c << 1; //< now sets c.a
}
I can't figure it out why the Derived class remains Abstract after overriding function fun().
Here is the error message:
error: invalid new-expression of abstract class type 'Derived' Base *t = new Derived(a);
error: no matching function for call to 'Base::fun(int&)'int i = t->fun(b);
#include <iostream>
using namespace std;
class Base
{
protected:
int s;
public:
Base(int i = 0) : s(i) {}
virtual ~Base() {}
virtual int fun() = 0;
};
class Derived: public Base
{
public:
Derived(int i) : Base(i) {}
~Derived() { cout << --s << " "; }
int fun(int x) { return s * x; }
};
class Wrapper
{
public:
void fun(int a, int b)
{
Base *t = new Derived(a);
int i = t->fun(b);
cout << i << " ";
delete t;
}
};
int main()
{
int i, j;
cin >> i >> j;
Wrapper w;
w.fun(i, j);
return 0;
}
The function has two different signatures between the base class and derived class
virtual int fun() = 0;
but then the derived class
int fun(int x) { return s * x; }
if you would add override it would alert you to this mistake
int fun(int x) override { return s * x; }
The problem is that
int fun(int x) { return s * x; }
does not override
virtual int fun() = 0;
because the argument list is different (no arguments vs. a single int). If you'd written
int fun(int x) override { return s * x; }
as you should since C++11, then the compiler would have given you an error about this.
I provide to you a MWE:
#include <iostream>
class C;
class A
{
public:
A(C &cc)
: c(cc)
{
}
int functionA()
{
return 0;
}
C &c;
};
class B
{
public:
B(C &cc)
: c(cc)
{
}
int functionB()
{
return c.a.functionA();
}
C &c;
};
class C
{
public:
C()
: a(*this)
, b(*this)
{
}
int functionC()
{
return b.functionB();
}
A a;
B b;
};
int main()
{
C c;
std::cout << c.functionC() << std::endl;
}
And associated compiler error:
main.cpp: In member function ‘int B::functionB()’:
main.cpp:37:16: error: invalid use of incomplete type ‘class C’
37 | return c.a.functionA();
| ^
main.cpp:5:7: note: forward declaration of ‘class C’
5 | class C;
| ^
Further explanation is probably not required, however, the class C is not fully defined by the time we reach line return c.a.functionA().
What is the most appropriate way to break this interdependence problem?
If it helps to guide, then consider the following context
C = Host
A = CPU
B = RAM
and the actual code where this problem occurs in my project is
void CPU::MemFetchByte(byte &ret, const addr_t addr)
{
HardwareDevice::host.memory.GetByte(ret, addr);
}
perhaps this is useful additional info, perhaps it is not.
Additionally, I tried to invert the problem as follows
#include <iostream>
//class C;
class A;
class B;
class C
{
public:
C()
: a(new A(*this))
, b(new B(*this))
{
}
~C()
{
delete b;
delete a;
}
int functionC()
{
return b->functionB();
}
A *a;
B *b;
};
class A
{
public:
A(C &cc)
: c(cc)
{
}
int functionA()
{
return 0;
}
C &c;
};
class B
{
public:
B(C &cc)
: c(cc)
{
}
int functionB()
{
return c.a->functionA();
}
C &c;
};
int main()
{
C c;
std::cout << c.functionC() << std::endl;
}
however this (as expected) makes things worse not better:
main.cpp:16:24: error: invalid use of incomplete type ‘class A’
16 | : a(new A(*this))
| ^
main.cpp:6:7: note: forward declaration of ‘class A’
6 | class A;
| ^
main.cpp:17:24: error: invalid use of incomplete type ‘class B’
17 | , b(new B(*this))
| ^
main.cpp:7:7: note: forward declaration of ‘class B’
7 | class B;
| ^
Work with complete types by moving the constructors and member function implementations out of the class declarations.
#include <iostream>
class A;
class B;
class C;
class A {
public:
A(C &cc);
int functionA();
C &c;
};
class B {
public:
B(C &cc);
int functionB();
C &c;
};
class C {
public:
C();
int functionC();
A a;
B b;
};
int main()
{
C c;
std::cout << c.functionC() << std::endl;
}
A::A(C &cc) : c(cc)
{ }
int A::functionA() {
return 0;
}
B::B(C &cc) : c(cc)
{ }
int B::functionB() {
return c.a.functionA();
}
C::C() : a(*this), b(*this)
{ }
int C::functionC() {
return b.functionB();
}
int functionB()
{
return c.a.functionA();
}
This function needs to be defined outside of the class body, after class C is defined.
You can apply the same idea to your second attempt (the definitions for C(), ~C(), and functionC() will need to be moved). But this is worse than your first attempt, because you're using heap allocation for no good reason (and class C doesn't follow the rule of three).
It looks like classes A and B are parts of system C, so that another option is to compose C from A and B using derivation. This allows A and B to get to C with a simple downcast, no C& c member is required:
struct C;
struct A {
void fa();
};
struct B {
void fb();
};
struct C : A, B {
void fc();
};
void A::fa() {
static_cast<C&>(*this).fc();
}
void B::fb() {
static_cast<C&>(*this).fc();
}
Looking for how to best access class B's queue through A but I am receiving a segmentation fault. Also I am looking for the best way to communicate between these two classes. Are accessor methods ok in this scenario? What design pattern could work? Thanks
class B {
public:
int get_int() { return qi.front(); }
void put_int(int i) { qi.push(i); }
private:
queue<int> qi;
};
class A
{
public:
void get_event() { cout << b->get_int() << endl; }
void put_event(int a) { b->put_int(a); }
private:
B *b;
};
int main() {
A a;
a.put_event(1);
return 0;
}
As mentioned in comment problem is undefined initialization
you can fix that by using constructor for initialization
#include<iostream>
#include<queue>
using namespace std;
class B {
public:
int get_int() { return qi.front(); }
void put_int(int i)
{
qi.push(i);
}
private:
queue<int> qi;
};
class A
{
public:
void get_event() { cout << b->get_int() << endl; }
void put_event(int a) { b->put_int(a); }
A()
{
b = new B();
}
~A() { delete b; }
private:
B *b;
};
int main() {
A a;
a.put_event(1);
a.get_event();
return 0;
}
Output
1
Program ended with exit code: 0
A a;
is an undefined reference, you have to initialize it with a costructor and since you didn't defined any, you must use the default one
A a=new A();
or better, write the costructors of the two classes as you prefer and use them.
Is it possible to set multiple different class members in one statement? Just an example of how this would be done:
class Animal
{
public:
int x;
int y;
int z;
};
void main()
{
Animal anml;
anml = { x = 5, y = 10, z = 15 };
}
To "convert" Barry's comment into an answer, yes, under the conditions here:
An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members
(clause 11), no base classes (clause 10), and no virtual functions
(10.3).
Example:
class Animal
{
public:
int x;
int y;
int z;
};
int main() {
Animal anml;
anml = { 5, 10, 15 };
return 0;
}
(This Community Wiki answer was added in accordance with this meta post.)
You can always overload constructors or create methods that "set multiple different object properties in one statement":
class Animal {
public:
Animal() {
};
Animal(int a, int b, int c) {
x = a;
y = b;
z = c;
}
void setMembers(int a, int b, int c) {
x = a;
y = b;
z = c;
}
private:
int x;
int y;
int z;
};
int main() {
// set x, y, z in one statement
Animal a(1, 2, 3);
// set x, y, z in one statement
a.setMembers(4, 5, 6);
return 0;
}
Solution 1 for Animal (http://ideone.com/N3RXXx)
#include <iostream>
class Animal
{
public:
int x;
int y;
int z;
Animal & setx(int v) { x = v; return *this;}
Animal & sety(int v) { y = v; return *this;}
Animal & setz(int v) { z = v; return *this;}
};
int main() {
Animal anml;
anml.setx(5).sety(6).setz(7);
std::cout << anml.x << ", " << anml.y << ", " << anml.z << std::endl;
return 0;
}
Solution 2 for any class with x, y (https://ideone.com/xIYqZY)
#include <iostream>
class Animal
{
public:
int x;
int y;
int z;
};
template<class T, class R> T& setx(T & obj, R x) { obj.x = x; return obj;}
template<class T, class R> T& sety(T & obj, R y) { obj.y = y; return obj;}
int main() {
Animal anml;
sety(setx(anml, 5), 6);
std::cout << anml.x << ", " << anml.y << std::endl;
return 0;
}