为什么命名空间中有"int"是不合法的?"int"是一种类型.对?就像"上课"一样.我知道它们之间存在差异,但有两种数据类型.
namespace Ikco.Crp.UI.Common
{
public int i;
....
....
}
Run Code Online (Sandbox Code Playgroud)
微软的想法是什么?
我有一个不明确的变量,例如:
Class myClass;
blah.h : error C2872: 'Class ' : ambiguous symbol
could be 'foo.h(30) : Class '
or 'foo2.h(106) : MyNamespace::Class '
Run Code Online (Sandbox Code Playgroud)
我如何具体声明变量myClass是foo.h中没有命名空间声明的前一个类?
提前致谢!
我正在尝试编写一个C++库的问题.这是通常的设置,一个cpp文件,一个头文件.我希望头文件只显示要使用的部分(例如,我有一个抽象基类,我不想在头文件中).到目前为止,我只是处理一个文件(我认为这应该没有区别,因为包含是由预处理器完成的,它不关心任何事情).
您会注意到"头文件"分布在头部实现文件之前和之后的两个位置.
#include <stdio.h>
// lib.h
namespace foo {
template <class T> class A;
}
// lib.cpp
namespace foo {
template <class T> class A {
private:
T i;
public:
A(T i) {
this->i = i;
}
T returnT() {
return i;
}
};
};
// lib.h
namespace foo {
template <class T> T A<T>::returnT();
}
// foo.cpp
void main() {
foo::A<int> a = foo::A<int>(42);
printf("a = %d",a.returnT());
}
Run Code Online (Sandbox Code Playgroud)
所以,当然,我希望我的头文件只包含
namespace foo {
template <class T> class A;
template <class …Run Code Online (Sandbox Code Playgroud) 我刚刚进入Javascript,所以我对命名空间的第一次尝试最终看起来像这样:
var myNameSpace = {};
var myNameSpaceProto = myNameSpace.__proto__;
myNameSpaceProto.SomeFunc = function()
{
alert("SomeFunc()");
};
myNameSpaceProto.SomeObject = function()
{
alert("SomeObject constructor");
};
var instance = new myNameSpace.SomeObject();
Run Code Online (Sandbox Code Playgroud)
我收集我可以安全地跳过原型步骤并且简单地拥有myNameSpace.SomeFunc = function...,因为只有一个myNameSpace对象实例,因此原型不会保存任何东西.
问题1: 这是正确的吗?我想从几个单独的.js文件添加到命名空间,所以这种方式似乎很方便.
问题2:使用上面的代码片段,我发现名称空间污染的奇怪副作用,如下所示SomeObject:
myNameSpaceProto.SomeObject = function()
{
// As expected NonexistantFunc is not a member of this and returns "undefined"
alert("typeof this.NonexistantFunc = " + typeof this.NonexistantFunc);
// Returns 'function'. How has SomeFunc made it to this.SomeFunc? It's supposed to be under myNameSpace.SomeFunc …Run Code Online (Sandbox Code Playgroud) 这是错的吗?为什么?我可以知道标准说的是什么吗?
namespace N{
namespace N1{
namespace N2{
struct A{
struct B{
void fun();
};//B
}; //A
} //n2
}//n1
namespace N3{
void N1::N2::A::B::fun(){} //error
}//n3
}//n
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以知道为什么会失败吗?
我正在宣布这样一个类:
#include "BindableInterface.h"
#include "../LuaHelper.h"
#include <map>
#include <string>
namespace utils{
class CLuaHelper;
};
namespace zel{
namespace utils{
class CAPIBindManager
{
typedef std::map<std::string, CBindeableInterface*> t_exposedClassMap;
t_exposedClassMap m_classesToExpose;
utils::CLuaHelper* m_luaHelper;
public:
bool exportClasses(const unsigned char* data);
bool executeLuaChunk(const std::string fileName, const std::string funtionName);
bool addClassToExpose(CBindeableInterface* bindableInterface, const std::string alias);
CAPIBindManager(void);
~CAPIBindManager(void);
};
};
};
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个编译错误,CLuaHelper不是zel :: utils的成员.CLuaHelper声明为utils :: CLuaHelper(没有zel)我如何声明这个类.AFAIK,前瞻性声明可能会解决这个问题
有任何想法吗??
我的C++代码中的#define语句有些问题,但是我不熟悉如何在VC++中处理它:
>filetaint.cpp
1>.\filetaint.cpp(272) : error C2872: 'UINT32' : ambiguous symbol
1> could be 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include\basetsd.h(82) : unsigned int WIND::UINT32'
1> or '..\..\include\gen\types_foundation.TLH(80) : LEVEL_BASE::UINT32'
1>.\filetaint.cpp(275) : error C2872: 'UINT32' : ambiguous symbol
1> could be 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include\basetsd.h(82) : unsigned
[...]
Run Code Online (Sandbox Code Playgroud)
==========构建:0成功,1失败,0最新,0跳过==========
所以问题在于,编译器不知道是否使用我的Windows SDK"basetsh.h"或者types_foundation.h中的定义.我希望它使用后者.在C++命名空间中是否有任何构造来告诉编译器选择什么?我想避免修补SDK或库本身.
#include "filetaint.h"
#include "dift.h"
using namespace WIND;
Run Code Online (Sandbox Code Playgroud)
types_foundation.TLH包含正确的语句.它已经在VS 2008中的VC++路径中了.我在这里有点困惑......但我想我必须找到一种方法告诉我的编译器暂时忽略SDK;).
我的目标是访问作为myFunction内部参数传入的类.
这是我正在尝试做的事情:
void myFunction(string myString)
{
callFunctionOn(OuterType::InnerType::myString);
}
Run Code Online (Sandbox Code Playgroud)
我试图在一个类型的东西上调用一些函数.例如,我在其他文件中的代码可能如下所示:
namespace OuterType {
namespace InnerType {
//stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
但是,以这种方式使用myString不起作用.如果myString持有值"class1",那么我希望将callFunctionOn部分解释为
callFunctionOn(OuterType::InnerType::class1);
Run Code Online (Sandbox Code Playgroud)
我觉得这很简单,但我整天都在编程,我的思绪变得疲惫......
求助:看起来就像这样,我需要一种带反射的语言.为了解决这个问题,我对问题采取了不同的方法,并将指针传递给了类.
我正在阅读有关创建类和嵌套类的内容,以确定哪种方法适合我的需求,但我找不到类似于我需要的东西(或者无法理解它;)).
我会给你们一个(几乎)真实的例子:
假设我拥有一家生产各种车辆的工厂.所以,我的命名空间就是Factory我的身材.
现在,让我们说工厂制造汽车,船只和飞机.所以我将Factory使用这些名称为我的命名空间添加三个类.
这是我理解其他方法的问题所在:
我在三种类型的车辆之间有一些共同点.例如,它们都有一个引擎(可能是不同的HP或我理解的形状是引擎的属性,但它们都有引擎).此外,汽车和飞机有门(有时船也有).另一方面,它们也有一些独特的东西(例如飞机有螺旋桨可能有不同的尺寸或形状).
有人可以描述一下我在代码中所说的内容,以便了解它们之间的区别吗?
在Ruby中必须有一种更有效的方法.我有一个方法列表,可以在多个站点中搜索相同的内容(标题,价格),但根据每个商店中的代码略有不同.例如:
def store1_get_title
def store1_get_price
def store2_get_title
def store2_get_price
def store3_get_title
def store3_get_price
Run Code Online (Sandbox Code Playgroud)
当调用所有这些函数时,我只想要一个带有"namespace"参数的泛型调用来调用这些方法中的任何一个,而不必输入所有这些,例如:
for get_all_stores().each do |store|
store::get_title
store::get_price
end
Run Code Online (Sandbox Code Playgroud)
...会像我想的那样调用store1_get_title,store1_get_price,store2_get_title,store2_get_price.有这样的事情或更好的方法吗?
希望有道理.感谢您的任何意见!
编辑:这些任务是在rake任务代码中.
namespaces ×10
c++ ×6
class ×3
c# ×2
types ×2
.net ×1
arguments ×1
header-files ×1
javascript ×1
methods ×1
nested-class ×1
parameters ×1
prototype ×1
rake-task ×1
ruby ×1
templates ×1
variables ×1