当呼叫不在函数内时,键入not found错误

时间:2015-05-28 02:42:15

标签: c++ oop

我正在试图弄清楚为什么会出现这种错误,但我没有成功。

当我尝试编译此代码时

using namespace std;
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>

class MyRand
{
    gmp_randclass randGen(gmp_randinit_default);
};


int main()
{
    MyRand s();
    gmp_randclass gmpRand(gmp_randinit_default);

    return 0;
}

使用此命令g++ Random.cpp -lgmpxx -lgmp,我收到以下消息:

  

包含在Random.cpp中的文件:3:0:Random.cpp:8:27:错误:   '__gmp_randinit_default'不是一种类型        gmp_randclass randGen(gmp_randinit_default);

但请注意,这一行

gmp_randclass randGen(gmp_randinit_default);

与此相同(在主要功能内)

gmp_randclass gmpRand(gmp_randinit_default);

并且只有第一个产生错误。

另外,如果我将类MyRand定义为如下(初始化函数内的mpz_randclass)

class MyRand
{
    void func()
    { 
         gmp_randclass randGen(gmp_randinit_default);
    }
};

我可以编译它没有错误。

有人知道发生了什么事吗?

非常感谢。

2 个答案:

答案 0 :(得分:3)

您无法初始化定义它们的类成员(至少在C ++ 11之前)。你可以把它放在构造函数中。

class MyRand
{
public:
    MyRand() : randGen(gmp_randinit_default) {
    }
private:
    gmp_randclass randGen;
};

答案 1 :(得分:1)

使用{}大括号初始值设定项或=初始化程序

<script>

   // Called automatically when the software has loaded
   function qzReady() {
      qz.findPrinter("epson");
   }

   // Called automatically when the software has finished searching for the printer
   function qzDoneFinding() {
       // append first file
       qz.appendHTMLFile("first.html");
   }

   var secondHasAppended= false;

   // Called automatically when a file is done appending
   function qzDoneAppending() {
       qz.print();
   }

   // Called automatically when document has been sent to the printer
   function qzDonePrinting() {
       if (!secondHasAppended) {
           qz.appendHTMLFile("second.html");
           secondHasAppended = true;
           // qzDoneAppending and qzDonePrinting will take care of the rest
       } else {
           alert("Done!");
       }
   }

</script>

然后尝试使用c ++ 11支持进行编译

class MyRand
{
    gmp_randclass randGen{gmp_randinit_default};
};