Error C2079: uses undefined class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Error C2079: uses undefined class

  1. #1
    Join Date
    Oct 2009
    Posts
    37

    Error C2079: uses undefined class

    Hi,

    I have two classes, each having its own header and implementation files. I'm trying to use one object in another object, but I'm constantly getting this error. Any suggestions? thanks

    actual error
    Code:
    Error C2079: 'Waiter::curentTable' uses undefined class 'Table'

    waiter.h
    Code:
    #ifndef WAITER_H
    #define WAITER_H
    
    #include <string>
    #include "Table.h"
    
    using namespace std;
    
    class Waiter{
    public:
    	Waiter();
    	string getID();
    private:
    	Table currentTable;
    	string ID;
    };
    
    #endif
    waiter.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Waiter.h"
    
    Waiter::Waiter(){}
    
    string Waiter::getID(){
    	return ID;		
    }

    table.h
    Code:
    #ifndef TABLE_H
    #define TABLE_H
    
    #include <string>
    #include "Waiter.h"
    
    using namespace std;
    
    class Table{
    public:
    	void assignWaiterToTable(Waiter waiter);
    	string myWaiter();
    
    private:
    	string id;
    	string waiterID;
    };
    
    #endif


    table.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Table.h"
    
    using namespace std;
    
    void Table::assignWaiterToTable(Waiter waiter)
    {
    	waiterID = waiter.getID();
    }
    
    string Table::myWaiter(){
    	return waiterID;
    }

  2. #2
    Join Date
    May 2007
    Posts
    798

    Re: Error C2079: uses undefined class

    Lookup circular header inclusion and forward declaration.

    http://www.cplusplus.com/forum/general/125/
    http://en.wikipedia.org/wiki/Forward_declaration

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Error C2079: uses undefined class

    AS STLDude already pointed out you should use forward declarations in the header files rather than #ncludes.
    And #ncludes should be mostly used in the .cpp files.

    Besides, this passing Waiter object by value looks bad:
    Quote Originally Posted by Momentum View Post
    Code:
    class Table{
    public:
    	void assignWaiterToTable(Waiter waiter);
    	string myWaiter();
    
    private:
    	string id;
    	string waiterID;
    };
    
    #endif
    Better would be to pass it by cont reference:
    Code:
    class Table{
    public:
    	void assignWaiterToTable(const Waiter &waiter);
    	string myWaiter();
    Victor Nijegorodov

  4. #4
    Join Date
    Oct 2009
    Posts
    37

    Re: Error C2079: uses undefined class

    And like that, my problem is solved. Thanks alot

  5. #5
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Error C2079: uses undefined class

    Quote Originally Posted by Momentum View Post
    Hi,

    I have two classes, each having its own header and implementation files. I'm trying to use one object in another object, but I'm constantly getting this error. Any suggestions? thanks
    1) Use forward declaration:

    2) Learn to use reference parameters, and quit passing objects by value.
    Code:
    void assignWaiterToTable(Waiter waiter);
    The sign of a new, or bad, or lazy, or unattentive C++ programmer is the line above. Objects such as Waiter should be passed by reference or const reference, not by value.
    Code:
    void assignWaiterToTable(const Waiter& waiter);
    The reason is three-fold:

    1) Passing objects by value incurs an unnecessary copy, the reason being that the compiler has to create a temporary, and the function uses the temporary and not the original object. Passing by reference makes the function use the original object, as well as not incurring a copy.

    2) Forward declaration only works with pointers or references to the type that is forwarded. Passing by value requires the compiler to know about the entire object, so forward declaration would not have worked if you continued to pass by value.

    3) Passing by value can lead to bugs, especially if you're expecting the function you're calling to change some aspect of the parameter you're passing. The only way a function can change the actual parameter passed is either passing by reference, or passing a pointer to the object.

    If you pass by value, the function is working with a temporary copy of your object, and once that function returns, that temporary goes poof into the night and disappears (along with all the changes you made) -- the end result is that you're stuck with the same values before you called the function.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 21st, 2012 at 11:15 AM.

  6. #6
    Join Date
    Oct 2009
    Posts
    37

    Re: Error C2079: uses undefined class

    Quote Originally Posted by VictorN View Post
    AS STLDude already pointed out you should use forward declarations in the header files rather than #ncludes.
    And #ncludes should be mostly used in the .cpp files.

    Besides, this passing Waiter object by value looks bad:Better would be to pass it by cont reference:
    Code:
    class Table{
    public:
    	void assignWaiterToTable(const Waiter &waiter);
    	string myWaiter();

    thanks for the reply. While trying to troubleshoot the problem, I stripped each class to the bare minimum functions and variables, removed all pointers and references, to hopefully figure out the cause of the problem

  7. #7
    Join Date
    Oct 2009
    Posts
    37

    Re: Error C2079: uses undefined class

    Quote Originally Posted by Paul McKenzie View Post
    1) Use forward declaration:

    2) Learn to use reference parameters, and quit passing objects by value.
    Code:
    void assignWaiterToTable(Waiter waiter);
    The sign of a new, or bad, or lazy, or unattentive C++ programmer is the line above. Objects such as Waiter should be passed by reference or const reference, not by value.
    Code:
    void assignWaiterToTable(const Waiter& waiter);
    The reason is three-fold:

    1) Passing objects by value incurs an unnecessary copy, the reason being that the compiler has to create a temporary, and the function uses the temporary and not the original object. Passing by reference makes the function use the original object, as well as not incurring a copy.

    2) Forward declaration only works with pointers or references to the type that is forwarded. Passing by value requires the compiler to know about the entire object, so forward declaration would not have worked if you continued to pass by value.

    3) Passing by value can lead to bugs, especially if you're expecting the function you're calling to change some aspect of the parameter you're passing. The only way a function can change the actual parameter passed is either passing by reference, or passing a pointer to the object.

    If you pass by value, the function is working with a temporary copy of your object, and once that function returns, that temporary goes poof into the night and disappears (along with all the changes you made) -- the end result is that you're stuck with the same values before you called the function.

    Regards,

    Paul McKenzie
    thanks for the info and for adding perspective

  8. #8
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Error C2079: uses undefined class

    Quote Originally Posted by Momentum View Post
    thanks for the reply. While trying to troubleshoot the problem, I stripped each class to the bare minimum functions and variables, removed all pointers and references,
    Taking away parameter types is not "stripping to the bare minimum" -- it leads to the compiler behaving differently when it comes to errors, and in the worse case scenario, if the compiler was successful, your program will behave differently.

    The way you "strip" a function is to comment out the function bodies, not change parameter types.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: Error C2079: uses undefined class

    Quote Originally Posted by Momentum View Post
    thanks for the reply. While trying to troubleshoot the problem, I stripped each class to the bare minimum functions and variables, removed all pointers and references, to hopefully figure out the cause of the problem
    Your Waiter design directly requires for mandatory association between instances of waiter and table: waiter has/owns his table. While in real life he never does. There might be waiters serving no tables as well as tables currently not served at all. By removing "pointers and references" you just broke the relations you tried to model. This is not the way how problems get resolved.
    Last edited by Igor Vartanov; August 23rd, 2012 at 05:52 AM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center