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

Цитата
Сообщение от 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);

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

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

Поделиться

Ещё вопросы

  • 0Jquery меню не прячется при отпускании мыши
  • 0Mysql: ЗАКАЗАТЬ дважды
  • 0Хранение двух отдельных изображений в двух отдельных ячейках в таблице MySQL
  • 0Возвышенный текст 3 работает локальный файл на локальном сервере с помощью плагина SideBarEnhancements?
  • 0Записи заказов по определенному заказу с определенными значениями строк
  • 0Как получить список предметов из одной таблицы и общее количество связанных с ними предметов из пыльника в mySql?
  • 1Использование свойства text = {binding var} для проверки значения введите в элемент управления textbox
  • 0Кто-нибудь сталкивался с какими-либо проблемами при использовании jQuery.getJSON () со строкой запроса URL-адреса API PetFinder?
  • 1matplotlib измененная карта цветов с белым как ноль
  • 0PHP заменить все предложение из строки
  • 0угловой ремешок в сторону не убивает наблюдателя при закрытии
  • 0C ++ числа в файле в массив
  • 0Почему передача одного запроса даты через Mysql2 из Ruby работает, а другой нет?
  • 0JQuery Select2 и JS плагин Struts 2
  • 1Как я могу проверить наличие ключа в Python
  • 0Альтернативные цвета для заголовков на основе селектора nth-child
  • 0получение данных с сервера в формате xml
  • 0Замена липкого заголовка вторым заголовком, когда он достигает текущего застрявшего заголовка
  • 0как создать директиву загрузки, которая не уничтожается и отменяет загрузку
  • 1Мой код не дает мне такого исключения
  • 1прекратите зарядку, когда батарея полностью заряжена
  • 0как найти элемент в тесте транспортира?
  • 0типинезависимый объект класса шаблона
  • 1Прочитать тег NFC от Xamarin
  • 1Как превратить TimePicker Джонатаса Уокера в плагин класса JS или jQuery
  • 0Рекурсивно сканировать файлы и каталоги и возвращать многомерный массив
  • 1Ошибка при преобразовании кадра данных pandas в спарк Dataframe
  • 1Unity вращает камеру с помощью сенсорного экрана — мобильный
  • 1Система оценки фигуристов
  • 0JQuery, нажмите, чтобы увеличить изображение, затем нажмите Agian, чтобы вернуться к предыдущей ширине
  • 0Почему регистры процессора действуют как корни для сборщика мусора?
  • 0Выражение с участием модульных возведения в степень в C ++
  • 0как отобразить моих пользователей из firebase с помощью angularjs
  • 1Проблема при компиляции привет мирового класса с использованием Javassist
  • 0Ошибка загрузки JS с использованием Backbone с помощью requireJS
  • 0простая клиентская программа на с ++ не работает
  • 1предложения по разработке JFrame
  • 0Форма jQuery вылетает при щелчке переключателя
  • 1Как выбрать тот же вариант, который был выбран на другой странице
  • 0обслуживать несколько каталогов в ngnix
  • 1pyshark: доступ к сырым данным udp
  • 1Threejs применяет отсечение к определенной области объекта
  • 0Блокировка / замораживание таблицы в ListView
  • 1Вызов await () для функций приостановки работает неправильно
  • 1Асинхронное многопоточное значение bool в C #
  • 1Как использовать ресурс, ссылающийся на родительское представление?
  • 0Как рассчитать с помощью php сумму с процентом НДС в PHP
  • 0Smarty strpos и substr для получения текста после последней косой черты в URL
  • 1Регулярный текст
  • 0вычисление выражений в C ++ с использованием LinkedList

Сообщество Overcoder

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

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

  • Ошибка кия р0650
  • Ошибка компилятора c2065
  • Ошибка кия р0302
  • Ошибка компилятора c2061
  • Ошибка кондиционера e13

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

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