forward-declaration

Is there any way to declare mutual friend functions for two classes

折月煮酒 提交于 2021-02-16 15:15:12
问题 class CDB; class CDM { public: friend CDB& CDB::Add(const CDM&); CDM& Add(const CDB&); }; class CDB { public: CDB& Add(const CDM&); friend CDM& CDM::Add(const CDB&); }; This code gives me the error : error C2027: use of undefined type 'CDB'. How to resolve this. 回答1: No, you can't do that. There is no way to remove the cyclic dependency. You should be able to get by with making the class CDB a friend of CDM instead of wanting to making CDB::Add() a friend. class CDB; class CDM { public:

What does “using namespace” do exactly?

核能气质少年 提交于 2021-02-16 05:49:12
问题 The following C++ test code does not link (gcc 4.9.2, binutils 2.25). The error is In function 'main': undefined reference to 'X::test' . 01: #include <string> 02: #include <iostream> 03: 04: namespace X 05: { 06: extern std::string test; 07: }; 08: 09: using namespace X; 10: std::string test = "Test"; 11: 12: int main() 13: { 14: std::cout << X::test << std::endl; 15: } Because of line 09, I was expecting line 10 to define the X::test variable declared on line 06. I believe that instead an

What does “using namespace” do exactly?

╄→гoц情女王★ 提交于 2021-02-16 05:48:05
问题 The following C++ test code does not link (gcc 4.9.2, binutils 2.25). The error is In function 'main': undefined reference to 'X::test' . 01: #include <string> 02: #include <iostream> 03: 04: namespace X 05: { 06: extern std::string test; 07: }; 08: 09: using namespace X; 10: std::string test = "Test"; 11: 12: int main() 13: { 14: std::cout << X::test << std::endl; 15: } Because of line 09, I was expecting line 10 to define the X::test variable declared on line 06. I believe that instead an

Why is this a forward declaration in C++?

若如初见. 提交于 2021-02-08 12:16:40
问题 I will have the following code snippet in utilA.cpp: // utilB.h namespace xm { void zoo(struct tm timeval); //<-----line 0 } // utilA.cpp #include <utilB.h> //<----line 1 #include <time.h> //<----line 2 namespace xm { void foo() { struct tm time1 = {0}; //<----line 3 } } GCC complains when compiling utilA.cpp, error: variable 'xm::tm time1' has initializer but incomplete type It seems this is because the utilA.h is using struct tm in line 0, but without include the time.h , and the compiler

Collect common includes in a single file - good practice?

自闭症网瘾萝莉.ら 提交于 2021-02-07 12:46:13
问题 I am trying to learn how to deal with a lot of includes, and still keep my code tidy. I am programming a Qt application and I have put files commonly used (and that doesn't change) in a file called "Util.h". Util.h #pragma once #include <Core/IObserver.h> #include <Core/Math.h> #include <QAction> #include <QDockWidget.h> #include <QFileDialog> #include <QGraphicsBlurEffect> #include <QLabel.h> #include <QMainWindow.h> #include <QMenu.h> #include <QMessageBox.h> #include <QShortcut.h> #include

Template method accesses forward declared class fails to compile only without this pointer

99封情书 提交于 2021-01-27 06:56:38
问题 When I compile the following code with the latest Visual Studio, it success to compile. class C; class T { public: template<typename A> void f(); private: C* c; }; int main() { T t; t.f<int>(); } template<typename A> void T::f() { this->c->g(); } class C { public: void g() {} }; But when I remove this-> from this->c->g() , compilation fails with C2027: use of undefined type 'C' . When I make the method f non-template, it fails to compile no matter this-> presents or not, so I think it's

Template method accesses forward declared class fails to compile only without this pointer

徘徊边缘 提交于 2021-01-27 06:54:27
问题 When I compile the following code with the latest Visual Studio, it success to compile. class C; class T { public: template<typename A> void f(); private: C* c; }; int main() { T t; t.f<int>(); } template<typename A> void T::f() { this->c->g(); } class C { public: void g() {} }; But when I remove this-> from this->c->g() , compilation fails with C2027: use of undefined type 'C' . When I make the method f non-template, it fails to compile no matter this-> presents or not, so I think it's

Namespace of a function declaration nested in function

ぃ、小莉子 提交于 2021-01-02 18:47:05
问题 For odd reasons, I want to declare a function inside a function scope. So I get the following code : namespace NS { void foo() { void bar(); bar(); } } In another compilation unit, I want to define bar. Depending on the compiler I'm using, I need to put bar in namespace NS or in the global namespace to be able to link: On clang: namespace NS { void bar() {} } On MSVC: void bar() {} What's the good behavior, if any ? As a side question, why is none of the following code compiling (on my 2

Namespace of a function declaration nested in function

∥☆過路亽.° 提交于 2021-01-02 18:36:23
问题 For odd reasons, I want to declare a function inside a function scope. So I get the following code : namespace NS { void foo() { void bar(); bar(); } } In another compilation unit, I want to define bar. Depending on the compiler I'm using, I need to put bar in namespace NS or in the global namespace to be able to link: On clang: namespace NS { void bar() {} } On MSVC: void bar() {} What's the good behavior, if any ? As a side question, why is none of the following code compiling (on my 2

Namespace of a function declaration nested in function

大憨熊 提交于 2021-01-02 18:27:31
问题 For odd reasons, I want to declare a function inside a function scope. So I get the following code : namespace NS { void foo() { void bar(); bar(); } } In another compilation unit, I want to define bar. Depending on the compiler I'm using, I need to put bar in namespace NS or in the global namespace to be able to link: On clang: namespace NS { void bar() {} } On MSVC: void bar() {} What's the good behavior, if any ? As a side question, why is none of the following code compiling (on my 2