Error C2079 Forward Declaration
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this
Error C2079 Uses Undefined Struct
site About Us Learn more about Stack Overflow the company Business Learn more c++ error c2079 about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Error C2079 Uses Undefined Class
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up struct forward c++ template uses undefined class declaration fails compile up vote 13 down vote favorite 1 I have the following code, but the compiler says sender_wrapper is undefined, even though I forward declared it. Can I not do a forward declare of a struct? (compiled with VS2003) struct send_wrapper; struct IPSend { IPSend::IPSend(const send_wrapper& sender) : _sender(sender) {} void IPSend::operator()(const std::string& msg) { if (!msg.empty()) _sender.send(msg); } send_wrapper _sender; //error c++ undefined class C2079: 'IPSend::_sender' uses undefined struct 'send_wrapper' }; struct send_wrapper { std::auto_ptr
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring
C++ Forward Declaration Vs Include Header
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question c++ forward declare class x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them;
Class Type Redefinition C++
it only takes a minute: Sign up How to avoid “'identifier' uses undefined class/struct/union 'name'” error when forward declaration is not enough? up vote 5 down vote favorite According to http://msdn.microsoft.com/en-us/library/9ekhdcxs(v=vs.80).aspx, C2079 can also occur if you attempt to http://stackoverflow.com/questions/5026655/struct-forward-declaration-fails-compile declare an object on the stack of a type whose forward declaration is only in scope. class A; class B { A a; // C2079 }; class A {}; Possible resolution: class A; class C {}; class B { A * a; C c; }; class A {}; My question is how do I eliminate this error when I have the following situation: class A; // Object class B // Container { public: typedef int SomeTypedef; private: A a; // http://stackoverflow.com/questions/10356737/how-to-avoid-identifier-uses-undefined-class-struct-union-name-error-when C2079 }; class A { void Foo(B::SomeTypedef); }; I can't declare A before declaring B because A needs to use B's typedef, and I can't declare B before A because of this error. One possible solution is to use a pointer to A instead of a stack variable, but I don't want a pointer (in this case). Another solution is to not use typedef, or not to put it inside class B. But what if it belongs in B and I want not to pollute my project's namespace, as in B::SomeTypedef is a more appropriate name than SomeTypedef? c++ share|improve this question edited Apr 27 '12 at 19:42 asked Apr 27 '12 at 17:44 Dmitri Shuralyov 452616 migrated from programmers.stackexchange.com Apr 27 '12 at 19:30 This question came from our site for professional programmers interested in conceptual questions about software development. 1 Can you use a namespace to handle scope for SomeTypedef? –chrisaycock Apr 27 '12 at 17:52 Are you suggesting to put the typedef in a separate namespace, something like B'? This is my current solution. I'd like to know if there's something better... How is this problem typically handled? –Dmitri Shuralyov Apr 27 '12 at 17:52 Well, if you have a namespace for your library/project that is separate from global. –chrisaycock Apr 27 '12 at 17:53 add a comment| 3 Answers 3 active oldest votes up vote 2 do
Windows Desktop Development > C++ Standards, Extensions, and Interop Question 0 Sign in to vote Good Morning all, I encountered a compiler error c2079 which said "use of undefined https://social.msdn.microsoft.com/Forums/vstudio/en-US/1654902a-a022-4885-93d6-23ab6709b635/how-to-fix-compiler-error-c2079?forum=vclanguage class/struc/union name" and am not sure how to solve it. The following is a simplifiedsetup of classes inside my code: class a; class b { int b1; public : func1(); } b::func1() { http://www.cplusplus.com/forum/beginner/31788/ 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, error c2079 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 Thursday, May 06, 2010 4:09 PM Reply | Quote Answers 0 Sign in to vote I would suggest to split class interface (to be put in .h error c2079 uses 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 Tom C. Lin Thursday, May 06, 2010 6:41 PM Thursday, May 06, 2010 4:44 PM Reply | Quote All replies 0 Sign in to vote 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
Class "A" have a pointer to the original "creator object" of class "B" so they can access the original instance. The file looks a bit like this: 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
class B; class A { public: A() : _p(NULL) {} A(B* p) : _p(p) {} void TestFunc(int val) { _p->SetVal(val); } private: B* _p; }; class B { public: B(int v) : val(v) { s =A(this); } void SetVal(int v) { val = v; } void TestFunc(int val) { s.TestFunc(val); } private: A s; int val; }; However this gives me a compile error: 1>X\main.cpp(9) : error C2027: use of undefined type 'B' 1> X\main.cpp(2) : see declaration of 'B' Causes/how to create this? Nov 19, 2010 at 2:02pm UTC filipe (1165) _p->SetVal(val); You can't inline this just with a forward declaration. Remember, a forward declaration knows nothing about the class, it only allows you to use pointers or references, not methods. EDIT: -Objects of Class "A" have a pointer to the original "creator object" of class "B" so they can access the original instance. Also notice this shouldn't be necessary at all. If A has a pointer to an object of type B, A should know how to manipulate B, but B shouldn't know anything about A. Last edited on Nov 19, 2010 at 2:04pm UTC Nov 19, 2010 at 2:15pm UTC paul23 (22) @filipe: I'm afraid I don't fully understand what you are saying there (in the edit). As you can see in the "example": "A" can manipulate "B", however "A" ought to be accessed only from "B".. Let's put it more into english words: Suppose I have a "Car" (object "B").. That care has a certain "state" (object A) which defines what it is doing (parking, driving, etc).. Then what I would like to do is: let