小编Ava*_*car的帖子

编译器错误代码与GCC在Windows上工作

我有一段简单的代码,它给了我一个编译器错误.我在Visual Studio下的Windows环境中编译和运行它没有任何问题,但现在在linux下,使用gcc,我遇到了问题.注意我使用的是gcc 4.4.5,并使用-std = c ++ 0x指令.

此代码段位于头文件file_handling.h中,其中包含所有必需的库(vector,string,fstream等).变量'output_file'是LogFile对象的成员,并在其他地方正确检查/实例化/ etc.代码本身很简单,这就是我难倒的原因:

template <typename T> void LogFile::put(std::string const & header, std::vector<T> const & data) {

  output_file << header << " " << std::scientific << data[0] << std::endl;

  for (std::vector<T>::const_iterator value = (data.begin()+1); value < data.end(); ++value) {
           output_file << *value << std::endl;
  }

}
Run Code Online (Sandbox Code Playgroud)

编译器声明:

In file included from file_handling.cpp:2:
file_handling.h: In member function 'void LogFile::put(const std::string&, const std::vector<T, std::allocator<_Tp1> >&)':
file_handling.h:132: error: expected ';' before 'value'
file_handling.h:132: error: …
Run Code Online (Sandbox Code Playgroud)

c++ gcc for-loop compiler-errors

3
推荐指数
1
解决办法
511
查看次数

使用C++中的非默认构造函数初始化对象的成员类

我有一个特定的情况,我有一个对象,我想使用增强随机数生成器,它导致一个更大的问题,我似乎无法回答.这是我正在尝试制作的示例代码.

首先,我的标题:

Class MyObject {

 protected:
    double some variable;
    boost::random::mt19937 rgenerator;
    boost::uniform_real<double> dist_0_1;
    boost::variate_generator< boost::mt19937&, boost::uniform_real<double> > rand01
}
Run Code Online (Sandbox Code Playgroud)

现在我想做的是:

Class MyObject {

 protected:
    double some variable;

    boost::random::mt19937 rgenerator(std::time(0)); //initialize to a "random" seed
    boost::uniform_real<double> dist_0_1(0,1); //set the distribution to 0-1
    boost::variate_generator< boost::mt19937&, boost::uniform_real<double> > rand01(rgenerator, dist_0_1);//tell it to use the above two objects
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为它在标题中.我以为我可以使用MyObject的构造函数以某种方式调用各个子对象上的构造函数(分布,生成器,但我无法弄清楚如何.当调用MyObject的构造函数时,子对象的默认值已经调用了构造函数,我还没有发现它们有成员方法来重置这些属性......除此之外,这不是我感到困惑的地方.现在也许有太多事情发生了,我令人困惑的问题,但据我所知,我的问题减少到以下,幼稚的例子:

Class Tree {

    Tree();
    Tree(int);

    protected: 

        fruit apples(int);
}

Tree::Tree() {
    apples(0); //won't work because we can't call the constructor again?
}

Tree::Tree(int fruit_num) …
Run Code Online (Sandbox Code Playgroud)

c++ class boost-random class-members

1
推荐指数
1
解决办法
2290
查看次数