namespaces

How to use class defined in a separate header within a namespace

那年仲夏 提交于 2020-01-06 04:03:27
问题 I have a namespace in which I'd like to define a class. The class is rather complex so I'd rather define it in a separate header file, but even the simplest code gives me an "undefined reference" error. main.cpp #include <iostream> namespace A { #include "C.hpp" } int main() { A::C foo; std::cout << foo.member << std::endl; return 0; } C.hpp class C { public: C(); int member; } C.cpp C::C() { this->member = 10; } When I run g++ C.cpp main.cpp I get "main.cpp:(.text+0x10): undefined reference

What is a good alternative to namespaces within classes?

无人久伴 提交于 2020-01-06 03:45:08
问题 The problem is C++ does not allow namespace declarations within classes.(I searched on internet and found this; if it's not true, please say it) So, what's the best way to cheat this problem? Context: My class have an enumeration within it. class GameCharacter { public: enum MovingState { Running, Walking, Stopped }; ... }; OBS: This example is not real, it's totally hypothetical. C++ defines that the enumeration names are inside the class scope, then to use these states I have to use the

Writing to locals() works in contrast to documentation saying its not

心不动则不痛 提交于 2020-01-05 08:18:12
问题 I am currently tinkering with the variable scopes and how they can be modified / copied, as I would like to postprocess some results dynamically in IPython. The confusion about locals(), vars() and globals() is real for me right now. Especially because the output of this piece of code: Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> locals()["test"] = 5 >>>

java namespace collisions

梦想的初衷 提交于 2020-01-05 07:42:12
问题 I find myself frequently making the following error. public class Foo{ Bar bar; public void doSomething(){ Bar bar = makeNewBar(); // bla bla } } where the error is that I want to set Foo's bar, not a local Bar. I.e, I should say bar = makeNewBar() , not Bar bar = makeNewBar() . Is there a compiler warning flag or something that can detect "namespace collisions" like this? If not, what's a good way to avoid this (short of not making the error, which is obviously the best option =p)? 回答1: That

Scope and Namespace questions

[亡魂溺海] 提交于 2020-01-05 07:29:33
问题 I was examining various JavaScript libraries for learning purposes. Basically I want to find the best way to initiliaze a namespace and see how pros load up all the associated files in their libraries. I came across this a few times in the main file (for example lets call it myNameSpace.js) that the library user would call in a few libraries: (function() { var jsFiles = window.MyNameSpace; window.MyNameSpace = { _getScriptLocation: (function() { /* some code here */ }) }; if(!jsFiles) {

Variables auto-initialized to 0 in unnamed namespace?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-05 04:50:36
问题 My understanding is that static variables get put in the uninitialized variable section of the binary (the BSS section) and so those are safe to assume as being initialized to 0. But I have a function defined in an unnamed namespace. Inside the function, there is a char array declared without being explicitly initialized to 0. Will this be auto-initialized to 0? What about variables not declared as static but defined in an unnamed namespace? And what about local variables of static functions?

Context of using declaration and ambiguous declaration

混江龙づ霸主 提交于 2020-01-05 04:25:09
问题 There is a quote from the sec. 3.4.3.2/3: Given X::m (where X is a user-declared namespace), or given ::m (where X is the global namespace), if S(X, m) is the empty set, the program is ill-formed. Otherwise, if S(X, m) has exactly one member, or if the context of the reference is a using-declaration (7.3.3), S(X, m) is the required set of declarations of m. Definition of S(X,m) is the following sec. 3.4.3.2/2: For a namespace X and name m, the namespace-qualified lookup set S(X, m) is defined

Why should i include the namespace in each aspx page?

橙三吉。 提交于 2020-01-05 03:16:27
问题 I usually include my namespaces e.g. using myProject.Model; to my .cs files but when i want to iterate an object derived from myProject.Model in my .aspx file i must always include the directive <%@ Import Namespace="myProject.Model" %> Why is that? I mean if it is declared in code behind file (in default.aspx.cs) of default.aspx Why should i add it again? Why it is not available? Just wondering but i would like to know why is that. Thank you in advance! 回答1: You can add a namespaces element

Python: include entries of a dictionary in the local namespace of a function

若如初见. 提交于 2020-01-04 14:35:50
问题 This has probably been asked before, but a quick search didn't give me an answer. Suppose there is a dictionary containing all variables. How can I pass this dictionary on to the local namespace of a function? For example: data = dict(a=1,b=2,c=3,d=4) def f(data): return data['a'] + data['d'] requires to write data[' '] around each variable you would like to access. How can you add all entries of the dictionary to the local namespace of the function? For objects you can use self.__dict__

Polluting the global namespace

一个人想着一个人 提交于 2020-01-04 05:52:21
问题 I think most C++ programmers here would agree that polluting the global namespace is a bad idea, but are there times when this rule can be ignored? For example, I have a type that I need to use all over a particular application - should I define it thus: mytypes.h typedef int MY_TYPE; foo.cpp MY_TYPE myType; Or use a namespace: mytypes.h namespace ns { typedef int MY_TYPE; } foo.cpp ns::MY_TYPE myType; ... using namespace ns; MY_TYPE myType; Which do you prefer? Are there times when it is