1,085 questions
8
votes
1
answer
132
views
Forward declaration in global module fragment
I have a template<int d> class Monomial and a template<int d> class Polynomial. Polynomial<d> owns a std::map<Monomial<d>, double>. I would like to create a member ...
Advice
2
votes
4
replies
253
views
Can function-local class be defined in the global scope?
Any C++ class can be first forward declared, and defined only later in the program. Are function-local classes an exception from this rule?
Please consider a simplified program:
auto f() {
struct ...
7
votes
1
answer
205
views
Visibility of C++ nested class complete definition in the presence of forward declarations
In C++, forward declarations introduce an incomplete type. Incomplete types can be used to declare pointers, but they cannot be used to initialize values or access members because the complete ...
2
votes
1
answer
232
views
What are good practice for forward declaration of nlohmann::json in C++?
I am working on a project in my spare time, and have been told to forward declare whenever possible. My project includes nlohmann::json across several files, so I thought to try and forward declare it ...
3
votes
1
answer
133
views
Reduce boilerplate for explicit templates instantiations in .cpp file
In order to hide template function definition in .cpp file, while having the forward declaration in a header file (function is used by a class template), the template has to be instantiated in order ...
5
votes
1
answer
86
views
Error when repeating forward declaration with default template argument
When repeating the declaration of a template class with defaulted argument, I get an error:
<source>:12:23: error: redefinition of default argument for 'class<template-parameter-1-2>'
...
1
vote
1
answer
56
views
Confusion on Struct forward declaration
I have this code on ADT specification of a node
typedef int ElType;
typedef node* Address;
typedef struct node{
ElType info;
Address next;
} Node;
Does it going to be the same if i do this?
...
2
votes
2
answers
92
views
Elfutils library forward declaration error
I'm currently working in C++ with the elfutils library and I'm struggling with something that should be obvious.
Here is a MCVE that shows my problem:
file.cpp
struct Elf64_Sym;
#include <elf.h>...
5
votes
2
answers
161
views
Incorrect return type allowed for forward declared function: Why is there no linker error here?
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version ...
-1
votes
1
answer
681
views
Forward Class Declaration in Python in a Dictionary
This is a question that has been debated many times in this forum. There is however a case that i have not seen a good way to solve.
from __future__ import annotations
class ClassA:
pass
class ...
1
vote
1
answer
94
views
Python avoid mypy fails when redefining a class from another class in mutual reference
Consider a pair of classes which represent the same thing in Python and each implements a method that converts one to the other. As an example consider transforming from cartesian to polar coordinates ...
3
votes
1
answer
117
views
is a class declaration before base-clause a valid forward declaration?
I'm not sure if my question title is the most appropriate and I will change it happily to something clearer if I get adequate suggestion.
In a C++ video whose subject is not the one here, I saw this ...
0
votes
1
answer
165
views
Correct way to forward declare structs? [duplicate]
Let's suppose I want to declare structs A and B
struct A{
B toB(){
return B();
}
};
struct B{
A toA(){
return A();
}
};
I'll get an error that type B is undefined
main.cpp:2:2: error: ...
0
votes
0
answers
47
views
Error when using data-member of a class inside a friend function defined in another class
So, here I have two classes, one has the friend function definition and the other a private data member. I want to access the private data member with the friend member function but it gives me an ...
0
votes
1
answer
283
views
Declare procedures prototypes in nim
I am trying understand how I can declare, in a nim source, different procedures that call each other, as it sounds like the compiler expects all calls to come after the declaration, but the procedures ...
0
votes
0
answers
193
views
I get a undefined reference in function main with code::blocks [duplicate]
I'm using code::bloks with this simple project:
a.cpp :
int get() {return 0;}
main.cpp :
int get();
int main()
{
int x = get();
return 0;
}
at compile I get : undefined reference to 'get()' ...
-1
votes
1
answer
191
views
Forward Declaration (PIMPL) for External Libraries also used in Method Declarations?
I have looked at a significant number of posts regarding forward declarations/PIMPL, but haven't quite managed to get it to work with the external libraries I'm using. I want to create a shared ...
11
votes
4
answers
1k
views
Can one forward-declare a function taking a vector of incomplete type with a default value?
The code snippet below demonstrates a real issue I faced recently in my program:
#include<vector>
class A;
void f( const std::vector<A> & = {} );
There is an incomplete class A, and ...
0
votes
0
answers
160
views
Simple forward declaration compiles with g++ but not clang
As for a minimally reproducible example, say I have the following class A defined in a header file a.h:
// file a.h
#include <cstdio>
class A {
private:
int val;
public:
A(int aVal) { ...
1
vote
1
answer
511
views
Include What You Use wants a forward declaration of a type stored in an array in a template class which results in a field has incomplete type error
eI have a template class, similar to the following class.
The behaviour is not that relevant, important is, that it has an array as attribute which stores T and its size depends on the template ...
4
votes
0
answers
203
views
If I forward declare a function template, may I put the definition after the calling site and not explicit instantiate it at all?
In a header file of a large project, I have to forward declare a function template before the calling site. The code boils down to this:
//H1.h
#pragma once
template <typename>
void f();
...
0
votes
1
answer
921
views
forward declaration of std::ostream
The files are organized as following:
//MyClass.h
#pragma once
namespace std {
class ostream;
};
class MyClass
{
private:
int a;
public:
friend std::ostream& operator<<(std::...
-2
votes
2
answers
142
views
std::unique_ptr<incomplete_type> without custom deleter
The following https://godbolt.org/z/5qd8WbYz9 doesn't work and after reading the duplicate I thought I would have to define an custom deleter
However, I managed to get it working https://godbolt.org/z/...
0
votes
1
answer
97
views
How to access forward declaration in C++ class?
The following code works, but it is not very readable to have methods separated from each state of this state machine:
struct Base {
virtual Base* next() = 0;
};
struct StateA : public Base {
...
3
votes
3
answers
544
views
Forward declaring a static variable in C++
I run into the following problem and idk if it can be solved in an elegant way:
I need to statically initialize an interface wifi and sntp in main. I can't do it in global space because they are ...
1
vote
1
answer
79
views
Is there a way to have two (or more) classes which each can create an object of the other in c++?
So I have two classes inherites from a base class, and I want each of them to have members that call the constructor for the other. Something like;
class BaseClass {
protected:
BaseClass* a;
...
3
votes
1
answer
112
views
Forward declaring a specific template instance
I have a template that is just that - a very basic class template; something like:
Tmpl.h
template <typename Base>
class Tmpl: public Base
{
public:
Tmpl(): Base()
{
this->...
1
vote
1
answer
43
views
boost::gil::rgba8_image_t forward declaration
Tried to create a forward declaration for boost::gil::rgba8_image_t:
namespace boost::gil {
class rgba8_image_t;
}
And got this:
... error: definition of type 'rgba8_image_t' conflicts with type ...
1
vote
1
answer
170
views
How to initialize a forward-declared class in C++ [duplicate]
I have two classes, A and B, which depend on each other:
class A {
public:
B* b;
A() {
b = new B();
}
};
class B {
public:
A* a;
B() = default;
};
This code will not ...
0
votes
3
answers
256
views
Why do we need return type and parameter type in funtion declarations? [closed]
I've been told we somehow need them so the compiler can continue onwards without having read the definition yet.
Somehow we need them in order for the program to work properly, to avoid conflicts ...
-1
votes
3
answers
424
views
Defining a nested class out of line in C++
Sorry if this has been asked before; I found similarly titled questions, but none of them focused on the same aspects I'm having trouble with.
There's a class A, and a class B that should only be ...
1
vote
1
answer
262
views
Friend declares a non-template function even though it resides in templated class (only gcc)
The following example compiles BUT gcc warns me that the friend declaration is in fact not a templated function. I don't understand what exactly I'm supposed to change. Clang and MSVC accept this code ...
0
votes
1
answer
86
views
Two files including each other causes errors that cannot be fixed by forward declaring
So, read clearly...
// scene.h
#include "Entity.h"
class Scene
{
public:
Entity createEntity() {
return Entity(this);
}
};
So, that was the Scene class, then we've got the ...
0
votes
1
answer
75
views
In C++, do the header files of virtual function return types have to be included in the class declaration?
Consider the following code:
class I_Clipboard
{
public:
virtual ~I_Clipboard () = default;
virtual Type_A copied_ta() const = 0;
virtual void set_copied_ta(const Type_A & ta) = 0;
...
2
votes
1
answer
195
views
Why can I reference a member of an enum that is only forward declared [closed]
For example, given the following:
// enum.h
enum class TestEnum: int {
ONE,
TWO,
THREE
};
// function.h
enum class TestEnum: int;
int TestFunction(TestEnum te = TestEnum::THREE);
// ...
0
votes
3
answers
226
views
C++ another unique_ptr incomplete type question
Have looked at various similar questions here but still can't figure out why the following code does not compile:
// these three are defined somewhere
class A;
std::unique_ptr<A> make_a();
void ...
1
vote
1
answer
310
views
Forward declaration in namespace
Consider the following scenario:
"A.cpp":
void fun() { std::cout << "fun() global\n"; }
"B.cpp":
namespace N
{
void f()
{
std::cout << "...
2
votes
2
answers
2k
views
Is forward declaration of classes in python bad design? [duplicate]
I have encountered a reference problem like this Example 1;
@dataclass
class Book:
book_id:int
book_name:str
book_library: Library #The object where book is stored
@dataclass
class Library:...
3
votes
1
answer
262
views
C++: struct forward declaration of anonymous struct causes "conflicting declaration"
I have got this hpp file:
struct rte_spinlock_t;
class A {
public:
void init();
private:
rte_spinlock_t* spinlock;
};
and the corresponding cpp file:
#include "A.hpp&...
0
votes
1
answer
111
views
Forward declare a templated typedef
In my public header, I am trying to add a pointer to an object which is only defined privately. Normally that would be simple. I just forward-declare. However, the object is a bit complicated and I'...
2
votes
1
answer
201
views
Is it legal to use a different class-key in a declaration and the definition of a class-name?
Does the standard (as of C++20) explicitly or implicitly allow using a different class-key when (forward-)declaring a class-name than when defining it?
For the purpose of this question class-key shall ...
0
votes
1
answer
86
views
Forward declaration of structure pattern
I am forced to use the architecture which will be presented below. Forward declaration is the pattern I'm trying to implement to counter the issue.
Here is what I have so far :
class_with_config.h :
#...
0
votes
0
answers
1k
views
C++ forward declaration not working as expected in class [duplicate]
I am doing a project where I recreate Pokémon in C++ and currently I am in the process of implementing moves.
My implementation is currently:
#pragma once
class Pokemon;
class MoveBase
{
public:
...
1
vote
1
answer
130
views
Why doesn't this class forward declaration compile in C++? [duplicate]
I'm sure that this has been asked, but I cannot find the question or answer, so here is the minimal code I tried to compile.
// goof4.cpp : This file contains the 'main' function. Program execution ...
0
votes
1
answer
148
views
How to provide type annotations in case of circular dependency in different files in Python?
Please consider the following working code
from __future__ import annotations
class A(object):
def __init__(self, val: int):
self.val = val
@property
def b(self) -> B:
...
0
votes
0
answers
33
views
Errors with forward declarations in cpp
I am trying to use a visitor 'BehaviorVisitor' which has a method for each type of 'Flower' such as 'Sunflower'. I keep getting this error from BehaviorVisitor:
error: variable has incomplete type '...
1
vote
1
answer
618
views
Forward declaring a template type parameter
I see this question has been discussed in various places, e.g. here,here,here and here .But, i have still not been able to relate to the questions aforementioned.
My situation:
I am trying to ...
2
votes
0
answers
182
views
Removing Dependencies from Middle Layer DLL in C# Project Loading Compiled C++ and LabVIEW Code
I'm looking for a way to remove some header file dependencies. I'm working on a test project which runs a Windows UI in C# on a backend written in LabVIEW compiled to a DLL. Right now, I compile the ...
2
votes
2
answers
139
views
If I declare a function with extern "C", should I also define it that way?
In my header file, foo.h, I have:
#ifdef __cplusplus
extern "C" {
#endif
int foo(int x);
#ifdef __cplusplus
}
#endif
Now, in foo.cpp, should I also use extern "C", and define:
#...
0
votes
2
answers
951
views
Using macros to define forward declarations
I am writing a pretty huge program with lots of templated functions. Naturally, the program has a long compile-time, which is why I wanted to use forward declaration. Now there are a lot of functions ...