读书人

编译器报:未调用原形函数(是有意用变量

发布时间: 2012-11-14 10:12:19 作者: rapoo

编译器报:未调用原型函数(是有意用变量定义的吗?)(本文为原创,转载清注明出外)
MSDN解释:编译器检测到未使用的函数原型。如果有意将该原型作为变量声明,则移除左/右括号。什么意思,简单来说,就是编译无法分辨你当前的代码是在声明一个函数原型,还是在调用一个函数.因为在VS编译器里这样声明一个函数是正确的:test(int(a),int(b)),但我们经常用他做为函数调用来使用。
Code
// compile with: /W1
class Lock {
public:
int i;
};

void f() {
Lock theLock();
// try the following line instead
// Lock theLock;
}

int main() {
}
当然你的意图是调用一个无参构造函数,然而编译器却认为你在声明一个函数.所以报错了.

解决方法是这样调用:test((int(a)),(int(b)));编译通过!!

附MSDN的一个例程:



Code
class BooleanException
{
bool _result;

public:
BooleanException(bool result)
: _result(result)
{
}

bool GetResult() const
{
return _result;
}
};

template<class T = BooleanException>
class IfFailedThrow
{
public:
IfFailedThrow(bool result)
{
if (!result)
{
throw T(result);
}
}
};

class MyClass
{
public:
bool Foo()
{
try
{
IfFailedThrow<>(MyMethod()); //error

// try one of the following lines instead
// IfFailedThrow<> ift(MyMethod());
// IfFailedThrow<>(this->MyMethod());
// IfFailedThrow<>((*this).MyMethod());

return true;
}
catch (BooleanException e)
{
return e.GetResult();
}
}

private:
bool MyMethod()
{
return true;
}
};

int main()
{
MyClass myClass;
myClass.Foo();
}
//在上面的示例中,不含参数的方法的结果作为参数传递给未命名本地类变量的构造函数。该调用会产生歧义:既可以是命名本地变量,也可以是使用对象实例以及相应的指向成员的指针运算符给方法调用加前缀。



读书人网 >其他相关

热点推荐