这个问题与这个问题有关.以下代码编译精细的VC9编译器,但在与Comeau在线编译时给出错误.任何人都可以告诉我哪一个是正确的,错误是什么意思?
错误:暧昧"?" 操作:类型"TypesafeBool"的第二个操作数可以转换为第三个操作数类型"bool",反之亦然TypesafeBool b =(1 == 1)?f():false;
class TypesafeBool
{
private:
bool m_bValue;
struct Bool_ {
int m_nValue;
};
typedef int Bool_::* bool_;
inline bool_ True() const { return &Bool_::m_nValue; }
inline bool_ False() const { return 0; }
public:
TypesafeBool( const bool bValue ) : m_bValue( bValue ){}
operator bool_() const { return m_bValue ? True() : False(); }
};
TypesafeBool f()
{
return TypesafeBool(true);
}
int main()
{
TypesafeBool b = (1==1) ? f() : false; …Run Code Online (Sandbox Code Playgroud) 如何为双向迭代器定义operator <?(list :: iterator)
(我想用list而不是vector.)
我是C#的新手,并试图理解lambda表达式和委托.这是我正在运行的代码:
delegate bool D1();
delegate bool D2(int i);
namespace Console
{
class Program
{
D1 d1;
D2 d2;
public void testMethod(int input)
{
int j = 0;
d1 = () => { j = 10; return j < input; };
d2 = (x) => { return x == j; };
System.Console.WriteLine("j = {0}", j);
bool res = d1();
System.Console.WriteLine("res={0}, j ={1}", res, j);
}
static void Main(string[] args)
{
Program p = new Program();
p.testMethod(10);
System.Console.WriteLine(p.d2(10));
System.Console.ReadKey();
}
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个类,其对象必须在堆上创建.除此之外,还有更好的方法吗?
class A
{
public:
static A* createInstance(); //Allocate using new and return
static void deleteInstance(A*); //Free the memory using delete
private:
//Constructor and destructor are private so that the object can not be created on stack
A();
~A();
};
Run Code Online (Sandbox Code Playgroud) 我不确定我是否遗漏了一些基本的东西.但我无法理解为什么编译器会为此代码生成错误:
class A
{
};
class B
{
public:
B();
A* get() const;
private:
A* m_p;
};
B::B()
{
m_p = new A;
}
A* B::get() const
{
//This is compiling fine
return m_p;
}
class C
{
public:
A* get() const;
private:
A m_a;
};
A* C::get() const
{
//Compiler generates an error for this. Why?
return &m_a;
}
Run Code Online (Sandbox Code Playgroud)
编辑:编译器错误是:错误C2440:'return':无法从'const class A*'转换为'class A*'转换失去限定符
我只是在Win32中使用一些低值(如10ms作为超时时间)尝试SetTimer方法.我计算了获得500个计时器事件所需的时间,并预计它将在5秒左右.令人惊讶的是,我发现这需要大约7.5秒才能获得这么多事件,这意味着它可以在大约16ms时超时.我们可以为超时时间设置的值是否有任何限制(我在MSDN上找不到任何内容)?此外,我系统中运行的其他进程是否会影响这些计时器消息?
如果您应该将所有内容封装在类定义中,那么如何在类中使用枚举数据类型呢?例如,我刚刚编写了以下代码......
enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};
enum PizzaSize {SMALL, MEDIUM, LARGE};
class Pizza {
public:
Pizza();
void setPizzaType(PizzaType type);
PizzaType getPizzaType();
void setPizzaSize(PizzaSize size);
PizzaSize getPizzaSize();
void setToppings(int toppings);
int getToppings();
void outputDescription();
double computePrice();
private:
PizzaType pizzaType;
PizzaSize pizzaSize;
int totalToppings;
};
Run Code Online (Sandbox Code Playgroud)
有没有办法在类本身中包含枚举数据类型,但仍允许从外部访问mutator/accessor函数?
我有一个关于std :: sort算法的问题.这是我的测试代码:
struct MyTest
{
int m_first;
int m_second;
MyTest(int first = 0, int second = 0) : m_first(first), m_second(second)
{
}
};
int main(int argc,char *argv[])
{
std::vector<MyTest> myVec;
for(int i = 0; i < 10; ++i)
{
myVec.push_back(MyTest(i, i + 1));
}
//Sort the vector in descending order on m_first without using stand alone function or functors
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否可以在m_first不使用任何独立函数或仿函数的情况下对变量进行排序?另外,请注意我没有使用提升.
我有这样的代码
string xml_path(conf("CONFIG"));
xml_path+=FILE_NAME;
Run Code Online (Sandbox Code Playgroud)
其中,conf函数返回char *和FILE名称是const char *
我想将它组合成一行
xml_path(conf("CONFIG")).append(FILE_NAME)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
有什么建议 ??
我正在开发一个通用应用程序,我使用UISplitViewController做iPad应用程序.我遵循通用应用程序指南,即我将基本SDK保持为3.2,iPhone目标OS广告iPhone OS 3.1.3,Taget设备作为iPhone/iPad.我为UISplitViewController和UIPopOverController使用了"为新符号添加运行时检查".
Class splitVCClass = NSClassFromString(@"UISplitViewController");
if (splitVC)
{
UISplitViewController* mySplitViewController = [[splitVCClass alloc] init];
// Configure the split view controller.
}
Run Code Online (Sandbox Code Playgroud)
我在.m文件中使用了这个,我也在.h文件中声明了UIPopOverController
"dyld:未找到符号:_OBJC_CLASS _ $ _ UIPopoverController参考自:/var/mobile/Applications/9E0CE75F-D2A9-4132-AE56-1780928BCF21/UniversalTasks.app/UniversalTasks预期:/System/Library/Frameworks/UIKit.framework/UIKit在/var/mobile/Applications/9E0CE75F-D2A9-4132-AE56-1780928BCF21/UniversalTasks.app/UniversalTasks"
我必须要做的任何人都可以帮助我