I'm new to C++ but I do have some experience in Java. While coding, I stumbled across an error that confused me. Here's my code (simplified, but the errors are the same):
A.h:
#pragma once #include "B.h" class A { public: A(); void foo(); void sayHello(); B b; }; A.cpp:
#include "A.h" #include <iostream> A::A() {} void A::foo() { b.bar(this); } void A::sayHello() { std::cout << "Hello" << std::endl; } B.h:
#pragma once #include "A.h" class B { public: B(); void bar(A *a); }; B.cpp:
#include "B.h" B::B(){} void B::bar(A *a) { a->sayHello(); } I want to pass a pointer of the a object to the bar function in B, so that I'll be able to modify and access a's fields in bar. Strangely, I get these errors when I call foo through an instance of A from another class:
1>------ Build started: Project: Test, Configuration: Debug Win32 ------ 1> main.cpp 1>d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A' 1> B.cpp 1>d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C3646: 'b': unknown override specifier 1>d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1> A.cpp 1>d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A' 1>d:\stuff\visual studio 2015\projects\test\test\a.cpp(5): error C2660: 'B::bar': function does not take 1 arguments 1> Generating Code... ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== The code works fine if I don't include A.h in B.h and I don't pass anything to the bar function.
I tried to google what could cause these errors but I wasn't able to resolve the issue by myself becuase I don't understand what's causing these errors. What am I doing wrong?
cyclic includesandforward declaration