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;
}
Re: Error C2079: uses undefined class
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
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();
Re: Error C2079: uses undefined class
And like that, my problem is solved. Thanks alot
Re: Error C2079: uses undefined class
Quote:
Originally Posted by
Momentum
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
Re: Error C2079: uses undefined class
Quote:
Originally Posted by
VictorN
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
Re: Error C2079: uses undefined class
Quote:
Originally Posted by
Paul McKenzie
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
Re: Error C2079: uses undefined class
Quote:
Originally Posted by
Momentum
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
Re: Error C2079: uses undefined class
Quote:
Originally Posted by
Momentum
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.