Ошибка компилятора c2079

I’m getting a compiler error with this header file:

#ifndef GAME1_H  
#define GAME1_H  
#include "GLGraphics.h"  
#include "DrawBatch.h"  
class GameComponent;  
class Game1 {  
private:  
    GLGraphics graphics;  
 GameComponent components;
 void updateDelegates();  
 void Run();  
};  

class GameComponent {  
private:  
 static int index;  
protected:  
 Game1 game;  
public:  
 GameComponent();  
 GameComponent(Game1 game);  
 void Update(int);  
 void Dispose();  
};  

class DrawableGameComponent: public GameComponent  
{  
private:  
 GLGraphics graphics;  
 DrawBatch drawBatch;  
public:   
 DrawableGameComponent();  
 DrawableGameComponent(Game1);  
 void Draw();  
};  

#endif

I see the problem is that Game1 needs the full definition of GameComponent, and that GameComponent needs the full definition of Game1. I had way too much trouble having these in separate headers, so that’s why they’re together. Is there any way I can do this without completely changing the implementation of one of the classes?

Thanks!

configurator's user avatar

configurator

40.9k14 gold badges82 silver badges115 bronze badges

asked Sep 26, 2010 at 4:06

Kalypso's user avatar

Think about the computer’s memory for a second here.

class B;

class A {
    byte aa;
    B ab;
};

class B {
    byte bb;
    A ba;
};

A x;

Now the question the compiler needs to answer is How much space should I reserve for x?

Let’s see. The first byte of x is byte aa;. Easy enough. That’s 1 byte.
Next comes B ab;. Let’s see what’s in there.
The first byte of x.ab is a byte bb;. That’s 2 bytes for x so far.
Next, is a A ba;. Let’s see what’s in there.
The first byte of x.ab.ba is a byte aa;. That’s 3 bytes for x so far.
And so on and so forth ad infinitum.
How big is x? The correct answer is of course *** OUT OF CHEESE ERROR ***.

The compiler doesn’t actually do this because it knows it can’t handle this case — so the syntax doesn’t allow circular containment in the first place.


Here’s a diagram of the contents of x in this example:
Diagram


UPDATE

Apparently, I forgot to include a solution here. Now that you understand what the problem is, the solution should be pretty simple. Use pointers. Either use a pointer from A to B and let B include A as it already does, or vice versa, or use two pointers. Then, you won’t have circular inclusion — if B doesn’t include a copy of A but just a pointer to it, that fixes the entire issue here.

Community's user avatar

answered Sep 26, 2010 at 4:20

configurator's user avatar

configuratorconfigurator

40.9k14 gold badges82 silver badges115 bronze badges

2

To do what you’re trying you’ll need to have a pointer to one of your classes rather than having it as a member.

For example:


class A;
class B;
class A {
   B b;
};

class B {
   A a;
};

This cannot work. It causes a circular structure. The constructor for A will call the constructor for B, which calls the constructor for A, and so on infinitely.

Instead, try:


class A;
class B;
class A {
   B *b;
};

class B {
   A *a;
};

A word of warning, though. If you are setting something up this way, there’s a pretty good chance your design is flawed. You should reconsider how you are solving your problem.

answered Sep 26, 2010 at 4:12

JoshD's user avatar

JoshDJoshD

12.5k3 gold badges43 silver badges53 bronze badges

At the risk of being off-topic: you can do this in C#, but unless the nested members are all load-on-demand you’ll get an immediate crash when you first instantiate either.

answered Sep 26, 2010 at 4:57

egrunin's user avatar

egruninegrunin

24.7k8 gold badges50 silver badges93 bronze badges

  • Remove From My Forums
  • Question

  • Good Morning all,

    I encountered a compiler error c2079 which said «use of undefined class/struc/union name» and am not sure how to solve it. The following is a simplified setup of classes inside my code:

    class a;

    class b {

    int b1;

    public :

    func1();

    }

    b::func1()

    {

    a aclass;

    aclass.a1()     <== this is where error c2079 come from. a1 is a public function of class a

    }

    I search Microsoft C++ compiler error web. It suggests that using pointer to a, instead of instance of a. So I changed

    a aclass      to     a * aclass = NULL;

    and

    aclass.a1()     to     aclass->a1();

    The compiler error changed to c2027 : use of undefined type ‘a’

    Any suggestions? Thanks for your help.

    Tom Lin

Answers

  • I would suggest to split class interface (to be put in .h files) from class implementation (to be put in .cpp file).

    So, you should have 4 files: 

    ClassA.h (header containing interface of class A)

    ClassA.cpp (implementation of class A)

    ClassB.h (header containing interface of class B)

    ClassB.cpp (implementation of class B)

    The implementation of B::func1 method should be put in ClassB.cpp file.

    The implementation of A::a1 method should be put in ClassA.cpp file.

    At the beginning of ClassB.cpp file, you should add a #include «ClassA.h» because you are using class A.

    See if this helps…

    Giovanni

    • Marked as answer by

      Thursday, May 6, 2010 6:41 PM

  • Remove From My Forums
  • Question

  • Good Morning all,

    I encountered a compiler error c2079 which said «use of undefined class/struc/union name» and am not sure how to solve it. The following is a simplified setup of classes inside my code:

    class a;

    class b {

    int b1;

    public :

    func1();

    }

    b::func1()

    {

    a aclass;

    aclass.a1()     <== this is where error c2079 come from. a1 is a public function of class a

    }

    I search Microsoft C++ compiler error web. It suggests that using pointer to a, instead of instance of a. So I changed

    a aclass      to     a * aclass = NULL;

    and

    aclass.a1()     to     aclass->a1();

    The compiler error changed to c2027 : use of undefined type ‘a’

    Any suggestions? Thanks for your help.

    Tom Lin

Answers

  • I would suggest to split class interface (to be put in .h files) from class implementation (to be put in .cpp file).

    So, you should have 4 files: 

    ClassA.h (header containing interface of class A)

    ClassA.cpp (implementation of class A)

    ClassB.h (header containing interface of class B)

    ClassB.cpp (implementation of class B)

    The implementation of B::func1 method should be put in ClassB.cpp file.

    The implementation of A::a1 method should be put in ClassA.cpp file.

    At the beginning of ClassB.cpp file, you should add a #include «ClassA.h» because you are using class A.

    See if this helps…

    Giovanni

    • Marked as answer by

      Thursday, May 6, 2010 6:41 PM

кто-то может помочь решить эту ошибку:

test.cpp(14): ошибка C2079: ‘x1’ использует undefined struct ‘x’
test.cpp(16): ошибка C2228: слева от «.x_x1» должен быть класс/структура/тип объединения

вот часть кода:

  struct  x  x1;
  ...
  x1.x_x1=y_x1;    

В unix программа компилирует и связывает без ошибок.
Благодарим за помощь,

Поделиться

Источник

1 ответ

С помощью только форвардной декларации вы можете определить указатель или ссылку на struct, вы не можете получить доступ к элементам (x1.x_x1) a struct.

Включить полное определение структуры, если вы хотите получить доступ к элементам.

например. следующее будет работать:

struct x {
  int x_x1;
};

struct  x  x1;
  ...
x1.x_x1=y_x1;

//или

#include "struct_x.h"
struct  x  x1;
  ...
x1.x_x1=y_x1;

где struct_x.h имеет:

struct x {
  int x_x1;
};

Kashyap

Поделиться

Ещё вопросы

  • 1проблема преобразования скрипта bash в python
  • 0json из js в php — не удалось открыть поток: сбой http-запроса
  • 0запустить команду exec () столько раз, сколько строк в текстовом файле
  • 1Добавить JS вызов на Rails form_for кнопка отправки
  • 0Кнопка поиска внутри поля поиска
  • 1Как получить данные из базы данных в реальном времени в Firebase?
  • 1Оптимальный способ использовать tkinter и openpyxl для перебора электронной таблицы?
  • 0Как добавить IMG в таблицу динамически?
  • 1Как добавить группировку и зависимость в @BeforeMethod On TestNG
  • 1запустить команду python с псевдонимом в командной строке, например, npm
  • 0MySQL: как отсортировать некоторые записи в ASC, а некоторые в порядке DESC, основываясь на значении другого поля
  • 0Получите img src, а затем измените контейнер div
  • 1Приложение работает нормально при отладке на тестовом устройстве, но не в apk релиза, оно не получает переменные post.class из firebase
  • 1Строки в pandas.Series всегда меньше 0?
  • 0GLSL OpenGL полигоны не рендеринг
  • 0Вертикально выровнять div относительно родного брата
  • 0Подменю CSS под его родителем
  • 1Как сбросить анимацию AnimatedVectorDrawable в Android, когда активность возвращается в версию 23?
  • 1Ось Y в формате JFreeChart для отображения значений в Power
  • 1Как протестировать веб-сервис с Linq
  • 0обеспечение единого #defines среди всех объектных файлов
  • 0Как изменить функцию, чтобы она передавалась по ссылке для моей переменной y из данных, которые она получает из входного файла?
  • 1Как распечатать (или предупредить) значение из магазина ExtJS?
  • 0CodeIgniter Войти через Google OpenID
  • 1Чтение определенного столбца из CSV с DictReader
  • 0Нет пробела между кнопками «на следующей строке»
  • 0PHP найти похожие ключи массива и сумму значений
  • 0Как написать запрос для сопоставления по всем таблицам?
  • 0Полиморфизм и сериализация с помощью Marmalade SDK
  • 0Ant генерирует html href список из набора файлов, добавляет тег для каждой строки
  • 0Как ускорить запрос PDO
  • 0Может ли str_shuffle вернуть не случайный результат?
  • 1Определите, был ли сделан запрос на подпись сертификата с использованием алгоритма SHA1 или SHA2
  • 1Не удается найти сертификат ни в хранилище LocalMachine, ни в хранилище CurrentUser — несоответствие имени удаленного сертификата
  • 0Добавьте div ниже определенного div, используя Javascript
  • 1Создайте ссылку на раскадровку в рабочем элементе TFS программно
  • 0Двоичное дерево JavaScript с поиском по ширине
  • 0Могу ли я использовать! = И == в C ++ для сравнения строк без написания собственного?
  • 0Перетаскивание по требованию. Как включить, запустить и продолжить перетаскивание всего в одном событии mousedown?
  • 1Невозможно получить какой-либо ответ от API (залп)
  • 1Как называется этот шаблон? (многоразовый вес)
  • 1«Неизвестный слой: лямбда» в tenorflowjs в браузере
  • 1Временно хранить данные в приложении с несколькими активностями, пока приложение не будет остановлено?
  • 0Невозможно подключиться к базе данных MySQL из локального RStudio
  • 1Пропуск рабочего до готового?
  • 0HTML несколько уровней развернуть свернуть для таблицы с большим количеством строк медленно
  • 0Фоны SVG не будут чрезмерно растягиваться, в отличие от других файлов изображений, таких как png
  • 1чтение файла xml и переупорядочение элементов на основе схемы xsd
  • 0параметр функции обратного вызова glutKeyboardfunc ()
  • 0Выводите родные элементы рядом друг с другом, используя jQuery

Сообщество Overcoder

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Compiler Error C2079

Compiler Error C2079

11/04/2016

C2079

C2079

ca58d6d5-eccd-40b7-ba14-c003223c5bc7

Compiler Error C2079

‘identifier’ uses undefined class/struct/union ‘name’

The specified identifier is an undefined class, structure, or union.

This error can be caused by initializing an anonymous union.

The following sample generates C2079:

// C2079.cpp
// compile with: /EHsc
#include <iostream>
int main() {
   std::ifstream g;   // C2079
}

Possible resolution:

// C2079b.cpp
// compile with: /EHsc
#include <fstream>
int main( ) {
   std::ifstream g;
}

C2079 can also occur if you attempt to declare an object on the stack of a type whose forward declaration is only in scope.

// C2079c.cpp
class A;

class B {
   A a;   // C2079
};

class A {};

Possible resolution:

// C2079d.cpp
// compile with: /c
class A;
class C {};

class B {
   A * a;
   C c;
};

class A {};

Think about the computer’s memory for a second here.

class B;

class A {
    byte aa;
    B ab;
};

class B {
    byte bb;
    A ba;
};

A x;

Now the question the compiler needs to answer is How much space should I reserve for x?

Let’s see. The first byte of x is byte aa;. Easy enough. That’s 1 byte.
Next comes B ab;. Let’s see what’s in there.
The first byte of x.ab is a byte bb;. That’s 2 bytes for x so far.
Next, is a A ba;. Let’s see what’s in there.
The first byte of x.ab.ba is a byte aa;. That’s 3 bytes for x so far.
And so on and so forth ad infinitum.
How big is x? The correct answer is of course *** OUT OF CHEESE ERROR ***.

The compiler doesn’t actually do this because it knows it can’t handle this case — so the syntax doesn’t allow circular containment in the first place.


Here’s a diagram of the contents of x in this example:
Diagram


UPDATE

Apparently, I forgot to include a solution here. Now that you understand what the problem is, the solution should be pretty simple. Use pointers. Either use a pointer from A to B and let B include A as it already does, or vice versa, or use two pointers. Then, you won’t have circular inclusion — if B doesn’t include a copy of A but just a pointer to it, that fixes the entire issue here.

  • Remove From My Forums
  • Question

  • Good Morning all,

    I encountered a compiler error c2079 which said «use of undefined class/struc/union name» and am not sure how to solve it. The following is a simplified setup of classes inside my code:

    class a;

    class b {

    int b1;

    public :

    func1();

    }

    b::func1()

    {

    a aclass;

    aclass.a1()     <== this is where error c2079 come from. a1 is a public function of class a

    }

    I search Microsoft C++ compiler error web. It suggests that using pointer to a, instead of instance of a. So I changed

    a aclass      to     a * aclass = NULL;

    and

    aclass.a1()     to     aclass->a1();

    The compiler error changed to c2027 : use of undefined type ‘a’

    Any suggestions? Thanks for your help.

    Tom Lin

Answers

  • I would suggest to split class interface (to be put in .h files) from class implementation (to be put in .cpp file).

    So, you should have 4 files: 

    ClassA.h (header containing interface of class A)

    ClassA.cpp (implementation of class A)

    ClassB.h (header containing interface of class B)

    ClassB.cpp (implementation of class B)

    The implementation of B::func1 method should be put in ClassB.cpp file.

    The implementation of A::a1 method should be put in ClassA.cpp file.

    At the beginning of ClassB.cpp file, you should add a #include «ClassA.h» because you are using class A.

    See if this helps…

    Giovanni

    • Marked as answer by

      Thursday, May 6, 2010 6:41 PM

Цитата
Сообщение от kuroiryuu
Посмотреть сообщение

имеется описание классов в h-файле

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <map>
#include <set>
 
class Node;
class EdgeToNode;
class AdjacencyList;
 
typedef std::set<EdgeToNode> EdgesList;
typedef std::map<size_t, Node> NodesList;
 
class Node
{
public:
    Node(Node & node);
    Node(size_t key, std::wstring name) : key_(key), name_(name) {}
    ~Node();
    void AddDirected(Node node);
    void AddDirected(Node node, double cost);
    void AddDirected(EdgeToNode* edge);
    bool HasNeighbor(size_t key);
 
    inline size_t GetKey() const { return key_; }
    inline std::wstring GetName() const { return name_; }
private:
    size_t key_;
    std::wstring name_;
    AdjacencyList neighbors_; //ОШИБКА C2079!!!
};
 
class EdgeToNode
{
public:
    EdgeToNode(Node neighbor) : neighbor_(neighbor), cost_(1.0) {}
    EdgeToNode(Node neighbor, double cost) : neighbor_(neighbor), cost_(cost) {}
 
    inline double Cost() {return cost_;}
    inline Node Neighbor() const {return neighbor_;} //ОШИБКА C2558!!!
private:
    double cost_;
    Node neighbor_;
};
 
class AdjacencyList
{
public:
    virtual void Add(EdgeToNode e);
    virtual bool Contains(Node node);
    virtual bool Contains(EdgeToNode edge);
    inline EdgesList::iterator GetBeginEdge() {return list_.begin();}
    inline EdgesList::iterator GetEndEdge() {return list_.end();}
private:
    EdgesList list_;
};

при компиляции вылазит две ошибки
error C2558: class ‘Node’ : no copy constructor available or copy constructor is declared ‘explicit’
error C2079: ‘Node::neighbors_’ uses undefined class ‘AdjacencyList’

подскажите как бороться с этими ошибками?

по C2079 пробовал описать переменную Node::neighbors_ как

C++
1
AdjacencyList* neighbors_;

ошибка исчезла, но такое использование указателя не самый лучший способ, хотелось бы узнать как правильно в данной ситуации применить std::auto_prt<AdjacencyList>

У вас возникает ошибка потому, что в классе Node, когда вы определяете член класса AdjacencyList neighbors_;, компилятору не известно определение этого типа, то есть AdjacencyList Вы лишь предварительно объявили этот класс, но не определили, а компилятору, чтобы правильно определить класс Node, нужно знать размер класса AdjacencyList . То есть в этом месте тип AdjacencyList является неполным типом: компилятор не имеет информации о его размере. Когда же вы это поле заменили указателем, то компилятору не нужно само строение класса AdjacencyList , так как размер указателя компилятору в любом случае известен.

Во-втором случае при объявлении функции

C++
1
inline Node Neighbor() const {return neighbor_;}

проблема заключается в том, что в классе Node вы объявили конструктор копирования следующим образом

то есть вы передаете не константную ссылку в качестве аргумента. А на временные объекты можно ссылаться только по константной ссылке. То есть компилятор не нашел подходящего конструктора копирования и выдал сообщение об ошибке.

То есть чтобы было понятно, ваша функция

C++
1
inline Node Neighbor() const {return neighbor_;}

в качестве возвращаемого значения создает временный объект Node, а соответствующий конструктор копирования вида

C#
1
Node(const Node & node);

отсутствует!

  • Remove From My Forums
  • Question

  • Good Morning all,

    I encountered a compiler error c2079 which said «use of undefined class/struc/union name» and am not sure how to solve it. The following is a simplified setup of classes inside my code:

    class a;

    class b {

    int b1;

    public :

    func1();

    }

    b::func1()

    {

    a aclass;

    aclass.a1()     <== this is where error c2079 come from. a1 is a public function of class a

    }

    I search Microsoft C++ compiler error web. It suggests that using pointer to a, instead of instance of a. So I changed

    a aclass      to     a * aclass = NULL;

    and

    aclass.a1()     to     aclass->a1();

    The compiler error changed to c2027 : use of undefined type ‘a’

    Any suggestions? Thanks for your help.

    Tom Lin

Answers

  • I would suggest to split class interface (to be put in .h files) from class implementation (to be put in .cpp file).

    So, you should have 4 files: 

    ClassA.h (header containing interface of class A)

    ClassA.cpp (implementation of class A)

    ClassB.h (header containing interface of class B)

    ClassB.cpp (implementation of class B)

    The implementation of B::func1 method should be put in ClassB.cpp file.

    The implementation of A::a1 method should be put in ClassA.cpp file.

    At the beginning of ClassB.cpp file, you should add a #include «ClassA.h» because you are using class A.

    See if this helps…

    Giovanni

    • Marked as answer by

      Thursday, May 6, 2010 6:41 PM

Понравилась статья? Поделить с друзьями:

Интересное по теме:

  • Ошибка компилятора c2065
  • Ошибка ккт 4453 атол при регистрации
  • Ошибка контрольной суммы crc возможно этот файл поврежден
  • Ошибка клапана егр дэу матиз
  • Ошибка компилятора c2061

  • Добавить комментарий

    ;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: