forward declaration with vector of class type - pointer to incomplete class type not allowed
You can't forward declare members.
Instead, bar.cpp
should #include
both foo.h
and bar.h
. Problem solved.
In general, if you use the sequence:
- Forward declare all class types
- Define all class types
- Bodies of class members
everything will be fine.
Pointer to incomplete class type is not allowed
An "incomplete class" is one declared but not defined. E.g.
class Wielrenner;
as opposed to
class Wielrenner
{
/* class members */
};
You need to #include "wielrenner.h"
in dokter.ccp
pointer to incomplete class type is not allowed in a partial template class
You probably forgot to include a header file with definition of a parameter class somewhere. This is the most common cause of the error.
Forward declaration works when you do not use fields from the declared class. Once you start using fields, you need a definition of this class.
C++ pointer to incomplete class type is not allowed
You need to add this to Dog.cpp:
#include "AnimalCare.h"
Also, you are missing the ; after your class declaration for Dog, so it should be:
class Dog
{
public:
Dog(AnimalCare* parent);
std::string GetParentName();
void Feed(void*);
private:
AnimalCare* g_parent;
};
Error: Pointer to incomplete class type is not allowed. How do I go about this?
You are misreading the error. It is not complaining about having a pointer to an incomplete type, but about dereferencing it.
if(foo->foobar()==true)
At this point the type of foo
is an incomplete type, so the compiler cannot check whether it will have a foobar
member function (or member functor).
Basically for an incomplete type you can declare and define pointers or references, declare interfaces (functions that take or return the type). But other than that you cannot create objects of the type, or use the pointers/references for anything other than copying the pointer/reference.
Regarding what you are doing wrong, you need to look to your real files in more details. Either you are not including the header that defines Foo
, or you have multiple Foo
types (different namespaces? one defines the type, another has the forward declaration) or your include guards are wrong and even if you include the header the guard discards the contents of the header. Note that after inclusion of the header that defines Foo
you don't need to (and should not) provide a forward declaration of the type, as that can easily lead to multiple declarations of Foo
in different contexts. If removing the forward declaration fails to compile, figure out why and fix that.
c++ Incomplete type not allowed error accessing class reference information (Circular dependency with forward declaration)
If you will place your definitions in this order then the code will be compiled
class Ball;
class Player {
public:
void doSomething(Ball& ball);
private:
};
class Ball {
public:
Player& PlayerB;
float ballPosX = 800;
private:
};
void Player::doSomething(Ball& ball) {
ball.ballPosX += 10; // incomplete type error occurs here.
}
int main()
{
}
The definition of function doSomething requires the complete definition of class Ball because it access its data member.
In your code example module Player.cpp has no access to the definition of class Ball so the compiler issues an error.
forward declarations, Incomplete type
Include in file Foo.cpp
header Bar.h
Foo.cpp:
#include "Foo.h"
#include "Bar.h"
Foo::Foo(const Bar &bar) : mBar(bar) {}
int Foo::getVal() const {
return mBar.getVal();
}
Or include header Bar.h
in header Foo.h
Foo.h:
#pragma once
#include "Bar.h"
class Foo {
const Bar &mBar;
public:
Foo(const Bar &bar);
int getVal() const;
};
Take into account that function Bar::getVal
must have qualifier const
int getVal() const;
Otherwise you will get one more compilation error because this non-const function is called from a const function of class Foo.
int Foo::getVal() const {
return mBar.getVal();
// ^^^^^^^^^^^
}
incomplete type not allowed, function dec/def and no classes
This:
int main(){
void out( fib( input() ) ); // error here
}
is interpreted to something like an instantiation of a variable of type void
calling the constructor that accepts a int
as first parameter,
instead you probably want this:
int main(){
out( fib( input() ) );
}
that is just a function call
Related Topics
Does Std::Cout Have a Return Value
What Does the Gcc Warning "Project Parameter Passing for X Changed in Gcc 7.1" Mean
Does Copy List Initialization Invoke Copy Ctor Conceptually
When Would You Use an Array Rather Than a Vector/String
Load 32-Bit Shared Library from 64-Bit Application
Parsing Complete Messages from Serial Port
Calling a Random Number Generating Member Function Doesn't Produce Entirely Random Numbers
How to Legally Reinterpret_Cast Between Layout-Compatible Standard-Layout Types
Abstract Class VS Interface in C++
Enumdisplaydevices VS Wmi Win32_Desktopmonitor, How to Detect Active Monitors
C++ Multi-Line Comments Using Backslash
Variable or Field Declared Void
C++ Templates: Conditionally Enabled Member Function
C++ Static Const Access Through a Null Pointer
C++ Type of Enclosing Class in Static Member Function