编译器报:未调用原型函数(是有意用变量定义的吗?)(本文为原创,转载清注明出外)...

本文探讨了Visual Studio编译器在处理函数原型时可能出现的误报问题,并提供了解决方案。通过示例代码说明如何正确声明及调用函数,避免编译错误。
MSDN解释:编译器检测到未使用的函数原型。如果有意将该原型作为变量声明,则移除左/右括号。什么意思,简单来说,就是编译无法分辨你当前的代码是在声明一个函数原型,还是在调用一个函数.因为在VS编译器里这样声明一个函数是正确的:test(int(a),int(b)),但我们经常用他做为函数调用来使用。
ContractedBlock.gifExpandedBlockStart.gifCode
// 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的一个例程:

 

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

 

 

转载于:https://www.cnblogs.com/anran_guojianjun/archive/2009/01/05/1369699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值