forward-declaration

Resolving a Forward Declaration Issue Involving a State Machine in C++

I've recently returned to C++ development after a hiatus, and have a question regarding implementation of the State Design Pattern. I'm using the vanilla pattern, exactly as per the GoF book. My problem is that the state machine itself is based on some hardware used as part of an embedded system - so the design is fixed and can't be ch...

When do I have to declare a function used in a template?

Hi @all, I've got (probably) a simple question. When do I have to declare a function used in a template? The following code prints out (using gcc >=4.1): init my A object no init Using gcc 4.0 the following code prints out: init my A object init my string object #include <iostream> #include <string> template<typen...

C++ Forward declaration , friend function problem

#include <iostream> class B; class A{ int a; public: friend void B::frndA(); }; class B{ int b; public: void frndA(); }; void B::frndA(){ A obj; std::cout << "A.a = " << obj.a << std::endl; } int main() { return 0; } When I am trying to compile this code some error occurred. Can anyone explain what are the problems in this ...

Incomplete type as function parameter?

I have that template class that uses a policy for it's output and another template argument to determine the type for it's data members. Furthermore the constructor takes pointers to base classes which are stored in private pointers. Functions of this objects shall take a this pointer to the template class to give them access to the data...

How does a C/C++ compiler find the definitions of prototypes in header files?

When I declare a function in a header file, and put the definition of that function in some other file, how does the compiler/linker find the definition? Does it systematically search every file in its path for it, or is there a more elegant solution? This has been bugging me for the past few days, and I've been unable to find an explana...

C++ boost forward declaration question

Hello! I spend some time examining boost:: libraries architecture and was interested with the following fact: In some parts of the libraries a yyy_fwd.hpp idea is used pretty common (see boost/detail or boost/flyweight for examples). These files obviously contain only forward declarations of some template-based classes and as far as I...

C++ Forward declaration and pure virtual functions

Hi, I have a problem using forward declaration and virtual functions. I got the following error message during compilation. main.cpp:131: error: cannot allocate an object of abstract type ‘Database::MySQL’ database_mysql.h:31: note: because the following virtual functions are pure within ‘Database::MySQL’: database.h:28: note: vir...

forward declaration in c++

I want to forward declare: namespace boost { namespace property_tree { template<typename Key, typename Data, typename KeyCompare = std::less<Key> > class basic_ptree; typedef basic_ptree< std::string, std::string > ptree; } } but my g++ cribs about redefinition due to default template argument. How can ...

Understanding forward declaration warning

I am writing my first lines in objective-c for an iPhone app. This is the code: /* ViewController.h */ @protocol ImageFlowScrollViewDelegate; @interface ViewController : UIViewController<ImageFlowScrollViewDelegate> { NSMutableArray *characters; UILabel *actorName; } /* ViewController.m */ #import "ImageFlowScrollView.h" @imp...

C++ templated function and forward declarations

I'm working on some code that compiles and links (and even has released commercial products) on Windows using MSVC. It doesn't compile with GCC though, I get the following errors: .../CBaseValue.h: In member function 'bool CBaseValue::InstanceOf()': .../CBaseValue.h:90:18: error: invalid use of incomplete type 'struct CValueType' .../CB...

Forward Declaration vs Include

Consider the following two scenarios (Edited just to complete the whole question and make it clearer) Case 1: (doesnt compile as rightly mentioned below) //B.h #ifndef B_H #define B_H #include "B.h" class A; class B { A obj; public: void printA_thruB(); }; #endif //B.cpp #include "B.h" #include <...

C++ Forward Declaring a class?

In a .h if I have: #pragma once #include <xxxx.h> #include "yyyy.h" class AAAAAA; class BBBBBB; class ZZZZZZ { public: // etc }; using class AAAAAA; is forward declaring a class right? Why would one do this? Is it needed? What are the benefits? Drawbacks? ...

Forward declarations for variables?

I have some C code that I have to port to C++. The code has a structure struct A { ... struct A * myPtr; } And now two global arrays are declared and initialized like this: //Forward declaration of Unit struct A Unit[10]; struct A* ptrUnit[2] = { Unit, Unit+7 }; struct A Unit[10] = { { .., &ptrUnit[0] }, ...

What can I "forward declare" in C++?

I know I can do class Foo; and probably struct Bar; and global functions bool IsValid(int iVal); What about a typed enum? What about a typed enum within an undeclared class? What about a function with an undeclared class? What about a static member within an undeclared class? What about these within an unknown namespace? Am...

When is an empty namespace definition needed?

Namespaces aren't declared and defined like most other things, but the namespace equivalent of a forward declaration would be: namespace X {} // empty body Normally, you define a namespace by placing other declarations inside it. But is there a problem for which this "namespace forward declaration" is the easiest solution? Of what ...

android forward declarations not working in 1.6

Hello, according to the official site, Android supports forward declarations from version 1.6 onwards. Having adjusted the min SDK and target SDK requirements both to '4' in manifest.xml, the layout editor from eclipse is still complaining about unknown declarations in a relative layout: <xml> <CheckBox android:layout_width="wrap...

Forward declare FILE *

How do I forward declare FILE * in C? I normally do this using struct MyType;, but naturally this doesn't appear to be possible. If behaviour differs between C standards or compilers and with C++, this is also of interest. Update0 Why I want to do this aside: What I'm asking is how to forward declare a non-struct/"typedef'd struct" ty...

Forward declaration include, on top of declaration include (ClassFwd.h + Class.h)

Hi all, In Effective C++ (3rd edition), Scott Meyers, in Item 31, suggests that classes should have, on top of their classic Declaration (.h) and Definition (.cpp) files, a Forward Declaration Include File (fwd.h), which class that do not need the full definition can use, instead of forward declaring themselves. I somewhat see the case...

C++ circular reference problem

Hello, I have 2 classes: DataObject and DataElement. DataObject holds pointers to (only) DataElements, and a DataElement contains pointers to several types, among which a DataObject. This used to be no problem, since I only use pointers to DataObjects in DataElement, so a forward declaration of DataObject in the header of DataElement i...

Forward declaration just won't do.

Below are two fragments (ready to compile) of code. In first fragment in which I'm using only forward declaration for a struct while deleting pointer to this struct from a Base class dtor for a Guest class isn't invoked. In the second fragment when instead of forward declaration I use full definition of this Guest class using delete in B...